master
icechen 2022-03-02 20:07:12 +08:00
parent b55af04082
commit fdc5516388
5 changed files with 63 additions and 10 deletions

View File

@ -1,5 +1,5 @@
import style from "./style.module.css"; import style from "./style.module.css";
import { useEffect, useRef, useState } from "react"; import { useEffect, useMemo, useRef, useState } from "react";
import { Close } from "@mui/icons-material"; import { Close } from "@mui/icons-material";
import { import {
Button, Button,
@ -33,6 +33,22 @@ export function HomeButton(props: Props) {
let [waitedFileList, setWaitedFileList] = useState<any[]>([]); let [waitedFileList, setWaitedFileList] = useState<any[]>([]);
let [uploadRatio, setUploadRatio] = useState(0); let [uploadRatio, setUploadRatio] = useState(0);
let [isFinish, setIsFinish] = useState(false); let [isFinish, setIsFinish] = useState(false);
let [startUploadTime, setStartUploadTime] = useState(0);
let uploadSpeed = useMemo(() => {
if (waitedFileList.length === 0) {
return "0/s";
}
let size = waitedFileList[0].size;
if (uploadRatio === 0) {
return "0/s";
}
return (
fileSize((size * uploadRatio * 10) / (Date.now() - startUploadTime)) +
"/s"
);
}, [startUploadTime, uploadRatio, waitedFileList]);
const dispatch = useDispatch(); const dispatch = useDispatch();
let onNormal = () => { let onNormal = () => {
@ -67,6 +83,7 @@ export function HomeButton(props: Props) {
let handleFileUpload = () => { let handleFileUpload = () => {
setUploadRatio(1); setUploadRatio(1);
setStartUploadTime(Date.now());
const reader = new FileReader(); const reader = new FileReader();
reader.onload = function (event) { reader.onload = function (event) {
if (event.target && event.target.result) { if (event.target && event.target.result) {
@ -160,7 +177,13 @@ export function HomeButton(props: Props) {
:{" "} :{" "}
{waitedFileList.length > 0 ? fileSize(waitedFileList[0].size) : ""} {waitedFileList.length > 0 ? fileSize(waitedFileList[0].size) : ""}
<br /> <br />
{uploadRatio !== 0 && <>: {uploadRatio}%</>} {uploadRatio !== 0 && <>: {uploadRatio.toFixed(2)}%</>}
<br />
{uploadRatio !== 0 && (
<>: {(Date.now() - startUploadTime) / 1000}s</>
)}
<br />
{uploadRatio !== 0 && <>: {uploadSpeed}</>}
<br /> <br />
{isFinish && <></>} {isFinish && <></>}
</DialogContent> </DialogContent>

View File

@ -1,13 +1,19 @@
import { Button, Paper, TextField } from "@mui/material"; import { Button, Paper, Snackbar, TextField } from "@mui/material";
import style from "./style.module.css"; import style from "./style.module.css";
import CountriesInput, { Country } from "../signin/countriesInput"; import CountriesInput, { Country } from "../signin/countriesInput";
import { useMemo, useState } from "react"; import { useMemo, useState } from "react";
import telegram, { sendSignInCode, signIn } from "../../telegram/telegram"; import { sendSignInCode, signIn } from "../../telegram/telegram";
import { getMe } from "../../telegram/user";
import { login } from "../../store/user";
import { useDispatch } from "react-redux";
export function HomeSignIn() { export function HomeSignIn() {
const dispatch = useDispatch();
let [country, setCountry] = useState({} as Country | null); let [country, setCountry] = useState({} as Country | null);
let [phone, setPhone] = useState(""); let [phone, setPhone] = useState("");
let [phoneCode, setPhoneCode] = useState(""); let [phoneCode, setPhoneCode] = useState("");
let [openSuccess, setOpenSuccess] = useState(false);
const phoneNumber = useMemo( const phoneNumber = useMemo(
() => (country ? country.country_code : "") + phone, () => (country ? country.country_code : "") + phone,
[country, phone] [country, phone]
@ -29,6 +35,18 @@ export function HomeSignIn() {
signIn(phoneNumber, phoneCode, codeHash) signIn(phoneNumber, phoneCode, codeHash)
.then((user: any) => { .then((user: any) => {
console.log("user", user); console.log("user", user);
setOpenSuccess(true);
getMe()
.then(function (user) {
// 已登录
dispatch(login(user));
})
.catch(function (error) {
if (error.code === 401) {
// Unauthorized
}
console.log(error);
});
}) })
.catch((error: any) => { .catch((error: any) => {
console.log("error", error); console.log("error", error);
@ -124,6 +142,15 @@ export function HomeSignIn() {
<br /> <br />
</div> </div>
<Snackbar
anchorOrigin={{ vertical: "top", horizontal: "right" }}
open={openSuccess}
onClose={() => {
setOpenSuccess(false);
}}
message="登录成功"
/>
</Paper> </Paper>
); );
} }

View File

@ -12,6 +12,7 @@ import { updateOfStorage } from "./store/fileList";
store.dispatch(updateOfStorage()); store.dispatch(updateOfStorage());
// eslint-disable-next-line no-extend-native
Date.prototype.Format = function (fmt) { Date.prototype.Format = function (fmt) {
var o = { var o = {
"M+": this.getMonth() + 1, //月份 "M+": this.getMonth() + 1, //月份
@ -31,7 +32,7 @@ Date.prototype.Format = function (fmt) {
if (new RegExp("(" + k + ")").test(fmt)) if (new RegExp("(" + k + ")").test(fmt))
fmt = fmt.replace( fmt = fmt.replace(
RegExp.$1, RegExp.$1,
RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length) RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
); );
return fmt; return fmt;
}; };

View File

@ -76,8 +76,6 @@ function File() {
}; };
let Download = () => { let Download = () => {
// @ts-ignore
console.log("waitDownloadFile", waitDownloadFile?.file_name);
downloadFile(waitDownloadFile) downloadFile(waitDownloadFile)
.then((res) => { .then((res) => {
console.log("res", res); console.log("res", res);

View File

@ -12,7 +12,7 @@ import getLocalStorage from "mtproton/envs/browser/get-local-storage";
class TelegramHelper { class TelegramHelper {
private client: any; private client: any;
constructor(appID: number, appHash: string, custom: boolean = false) { constructor(appID: number, appHash: string, custom: boolean = false) {
if (custom) { if (!custom) {
this.client = new Client({ this.client = new Client({
api_id: appID, api_id: appID,
api_hash: appHash, api_hash: appHash,
@ -38,7 +38,7 @@ class TelegramHelper {
test: true, test: true,
}); });
} }
this.client.setDefaultDc(1); // this.client.setDefaultDc(2);
} }
async call( async call(
@ -49,6 +49,9 @@ class TelegramHelper {
): Promise<any> { ): Promise<any> {
try { try {
!hideLog && console.dir(`${method} req\n${obj(params)}`); !hideLog && console.dir(`${method} req\n${obj(params)}`);
options = {
...options,
};
let resp = await this.client.call(method, params, options); let resp = await this.client.call(method, params, options);
!hideLog && console.dir(`${method} resp\n${obj(resp)}`); !hideLog && console.dir(`${method} resp\n${obj(resp)}`);
return resp; return resp;
@ -68,7 +71,7 @@ class TelegramHelper {
// } // }
if (error_code === 303) { if (error_code === 303) {
const [type, dcIdAsString] = error_message.split("_MIGRATE_"); const [, dcIdAsString] = error_message.split("_MIGRATE_");
const dcId = Number(dcIdAsString); const dcId = Number(dcIdAsString);
@ -110,6 +113,7 @@ async function uploadBigFile(
file_total_parts: file_total_parts, file_total_parts: file_total_parts,
bytes: tempBytes, bytes: tempBytes,
}); });
console.log("finished", finished);
if (callback) { if (callback) {
callback(((i + 1) / file_total_parts) * 100); callback(((i + 1) / file_total_parts) * 100);
} }