From 2f8ed1d82140c686410eeeba771af5e087ecdc96 Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Sat, 7 Jun 2025 16:28:18 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=AF=B7=E6=B1=82=E5=93=8D=E5=BA=94?= =?UTF-8?q?=E7=A0=81=E5=B8=B8=E9=87=8F=EF=BC=8C=E8=BA=AB=E4=BB=BD=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E6=9B=B4=E6=8D=A2=E9=80=BB=E8=BE=91=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/constants/result-constants.ts | 12 +++++++ src/plugins/http-fetch.ts | 58 ++++++++++++++++++------------- src/utils/encrypt-utils.ts | 9 +++++ 3 files changed, 54 insertions(+), 25 deletions(-) diff --git a/src/constants/result-constants.ts b/src/constants/result-constants.ts index 76332a40..ffc7af24 100644 --- a/src/constants/result-constants.ts +++ b/src/constants/result-constants.ts @@ -19,6 +19,18 @@ export const RESULT_MSG_SUCCESS: Record = { /**响应-code错误失败 */ export const RESULT_CODE_ERROR = 400001; +/**响应-code身份认证失败或者过期 */ +export const RESULT_CODE_AUTH = 401001; + +/**响应-code无效身份信息 */ +export const RESULT_CODE_AUTH_INVALID = 401002; + +/**响应-code令牌字符为空 */ +export const RESULT_CODE_AUTH_NOTOKEN = 401003; + +/**响应-code设备指纹信息不匹配 */ +export const RESULT_CODE_AUTH_DEVICE = 401004; + /**响应-code错误异常 */ export const RESULT_CODE_EXCEPTION = 500001; diff --git a/src/plugins/http-fetch.ts b/src/plugins/http-fetch.ts index df8c2f28..d1d8ca5c 100644 --- a/src/plugins/http-fetch.ts +++ b/src/plugins/http-fetch.ts @@ -22,6 +22,10 @@ import { APP_DATA_API_KEY, } from '@/constants/app-constants'; import { + RESULT_CODE_AUTH, + RESULT_CODE_AUTH_DEVICE, + RESULT_CODE_AUTH_INVALID, + RESULT_CODE_AUTH_NOTOKEN, RESULT_CODE_ENCRYPT, RESULT_CODE_ERROR, RESULT_CODE_EXCEPTION, @@ -34,7 +38,7 @@ import { RESULT_MSG_TIMEOUT, RESULT_MSG_URL_RESUBMIT, } from '@/constants/result-constants'; -import { decryptAES, encryptAES } from '@/utils/encrypt-utils'; +import { decryptAES, encryptAES, hexMD5 } from '@/utils/encrypt-utils'; import { localGet } from '@/utils/cache-local-utils'; import { refreshToken } from '@/api/auth'; @@ -52,9 +56,7 @@ export type ResultType = { /**防止重复提交类型 */ type RepeatSubmitType = { - /**请求地址 */ - url: string; - /**请求数据 */ + /**请求数据MD5 */ data: string; /**请求时间 */ time: number; @@ -158,19 +160,19 @@ function beforeRequest(options: OptionsType): OptionsType | Promise { ['POST', 'PUT'].includes(options.method) ) { const requestObj: RepeatSubmitType = { - url: options.url, - data: JSON.stringify(options.data) || '', + data: hexMD5( + JSON.stringify({ + url: options.url, + data: JSON.stringify(options.data) || '', + }) + ), time: Date.now(), }; const sessionObj: RepeatSubmitType = sessionGetJSON(CACHE_SESSION_FATCH); if (sessionObj) { - const { url, data, time } = sessionObj; + const { data, time } = sessionObj; const interval = 3000; // 间隔时间(ms),小于此时间视为重复提交 - if ( - requestObj.url === url && - requestObj.data === data && - requestObj.time - time < interval - ) { + if (requestObj.data === data && requestObj.time - time < interval) { const message = RESULT_MSG_URL_RESUBMIT[language]; return Promise.resolve({ code: RESULT_CODE_ERROR, @@ -232,27 +234,33 @@ async function beforeResponse( ): Promise { // console.log('请求后的拦截', res); - // 登录失效时,移除授权令牌并重新刷新页面 - // 登录失效时,移除访问令牌并重新请求 - if (res.code === 401001) { - const result = await refreshToken(getRefreshToken()); + // 移除授权令牌并重新刷新页面 + function clearToken() { + delAccessToken(); + delRefreshToken(); + window.location.reload(); + } + // 令牌失效时 + if (res.code === RESULT_CODE_AUTH) { + const refreshTokenStr = getRefreshToken(); + if (!refreshTokenStr) { + clearToken(); + } + const result = await refreshToken(refreshTokenStr); // 更新访问令牌和刷新令牌 if (result.code === RESULT_CODE_SUCCESS) { setAccessToken(result.data.accessToken, result.data.refreshExpiresIn); setRefreshToken(result.data.refreshToken, result.data.refreshExpiresIn); return await request(options); + } else if (result.code === RESULT_CODE_AUTH_DEVICE) { + clearToken(); } else { - debugger - console.warn(result) - // delAccessToken(); - // delRefreshToken(); - window.location.reload(); + // clearToken(); } } - if ([401002, 401003].includes(res.code)) { - delAccessToken(); - delRefreshToken(); - window.location.reload(); + // 令牌解析错误 + if ([RESULT_CODE_AUTH_INVALID, RESULT_CODE_AUTH_NOTOKEN].includes(res.code)) { + clearToken(); } // 响应数据解密 diff --git a/src/utils/encrypt-utils.ts b/src/utils/encrypt-utils.ts index 351d125c..f1395f48 100644 --- a/src/utils/encrypt-utils.ts +++ b/src/utils/encrypt-utils.ts @@ -48,3 +48,12 @@ export function decryptAES(ciphertext: string, aeskey: string): string { } return ''; } + +/** + * MD5 编码 + * @param message 字符串信息 + * @returns hax码 + */ +export function hexMD5(message: string): string { + return CryptoJS.MD5(message).toString(CryptoJS.enc.Hex); +}