fix: 请求响应码常量,身份信息更换逻辑优化

This commit is contained in:
TsMask
2025-06-07 16:28:18 +08:00
parent d84a6626c1
commit 2f8ed1d821
3 changed files with 54 additions and 25 deletions

View File

@@ -19,6 +19,18 @@ export const RESULT_MSG_SUCCESS: Record<string, string> = {
/**响应-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;

View File

@@ -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<any> {
['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<any> {
// 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();
}
// 响应数据解密

View File

@@ -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);
}