From 160e9678fe23eccd1adeedc6edbbfa1ec0efe9aa Mon Sep 17 00:00:00 2001 From: Tristan Yang Date: Thu, 15 Dec 2022 19:38:21 +0800 Subject: [PATCH] fix: upload size too large tip --- src/app/services/base.query.ts | 11 +++++++++-- src/common/hook/useUploadFile.ts | 1 + src/common/utils.tsx | 9 ++++++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/app/services/base.query.ts b/src/app/services/base.query.ts index 811ddbdf..8a272dab 100644 --- a/src/app/services/base.query.ts +++ b/src/app/services/base.query.ts @@ -4,6 +4,7 @@ import dayjs from "dayjs"; import { updateToken, resetAuthData } from "../slices/auth.data"; import BASE_URL, { tokenHeader } from "../config"; import { RootState } from "../store"; +import { getLocalAuthData } from "../../common/utils"; const whiteList = [ "guestLogin", @@ -43,8 +44,9 @@ const baseQueryWithTokenCheck = async (args, api, extraOptions) => { if (waitingForRenew) { await waitingForRenew; } - // 先检查token是否过期,过期则renew - const { token, refreshToken, expireTime = +new Date() } = api.getState().authData; + // 先检查token是否过期,过期则renew [从localstorage取] + const { token, refreshToken, expireTime } = getLocalAuthData(); + let result = null; // console.log("base check", whiteList.includes(api.endpoint), api.endpoint); if (!whiteList.includes(api.endpoint) && dayjs().isAfter(new Date(expireTime - 20 * 1000))) { @@ -102,6 +104,11 @@ const baseQueryWithTokenCheck = async (args, api, extraOptions) => { } } break; + case 413: + { + toast.error("File size too large"); + } + break; case 451: { // 证书错误 diff --git a/src/common/hook/useUploadFile.ts b/src/common/hook/useUploadFile.ts index bce62870..819357a8 100644 --- a/src/common/hook/useUploadFile.ts +++ b/src/common/hook/useUploadFile.ts @@ -90,6 +90,7 @@ const useUploadFile = (props?: IProps) => { sliceUploadedCountRef.current++; } catch (error) { console.error("upload file error", error); + canceledRef.current = true; return; } } diff --git a/src/common/utils.tsx b/src/common/utils.tsx index e4ad7fa4..83740424 100644 --- a/src/common/utils.tsx +++ b/src/common/utils.tsx @@ -1,4 +1,4 @@ -import BASE_URL, { FILE_IMAGE_SIZE, ContentTypes } from "../app/config"; +import BASE_URL, { FILE_IMAGE_SIZE, ContentTypes, KEY_TOKEN, KEY_REFRESH_TOKEN, KEY_EXPIRE } from "../app/config"; import IconPdf from "../assets/icons/file.pdf.svg"; import IconAudio from "../assets/icons/file.audio.svg"; import IconVideo from "../assets/icons/file.video.svg"; @@ -8,6 +8,13 @@ import IconCode from "../assets/icons/file.code.svg"; import IconImage from "../assets/icons/file.image.svg"; import { Archive, ArchiveMessage } from "../types/resource"; +export const getLocalAuthData = () => { + return { + token: localStorage.getItem(KEY_TOKEN) || "", + refreshToken: localStorage.getItem(KEY_REFRESH_TOKEN) || "", + expireTime: Number(localStorage.getItem(KEY_EXPIRE) || +new Date()) + }; +}; export const isImage = (file_type = "", size = 0) => { return file_type.startsWith("image") && size <= FILE_IMAGE_SIZE; };