fix: 请求响应码常量,身份信息更换逻辑优化
This commit is contained in:
@@ -19,6 +19,18 @@ export const RESULT_MSG_SUCCESS: Record<string, string> = {
|
|||||||
/**响应-code错误失败 */
|
/**响应-code错误失败 */
|
||||||
export const RESULT_CODE_ERROR = 400001;
|
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错误异常 */
|
/**响应-code错误异常 */
|
||||||
export const RESULT_CODE_EXCEPTION = 500001;
|
export const RESULT_CODE_EXCEPTION = 500001;
|
||||||
|
|
||||||
|
|||||||
@@ -22,6 +22,10 @@ import {
|
|||||||
APP_DATA_API_KEY,
|
APP_DATA_API_KEY,
|
||||||
} from '@/constants/app-constants';
|
} from '@/constants/app-constants';
|
||||||
import {
|
import {
|
||||||
|
RESULT_CODE_AUTH,
|
||||||
|
RESULT_CODE_AUTH_DEVICE,
|
||||||
|
RESULT_CODE_AUTH_INVALID,
|
||||||
|
RESULT_CODE_AUTH_NOTOKEN,
|
||||||
RESULT_CODE_ENCRYPT,
|
RESULT_CODE_ENCRYPT,
|
||||||
RESULT_CODE_ERROR,
|
RESULT_CODE_ERROR,
|
||||||
RESULT_CODE_EXCEPTION,
|
RESULT_CODE_EXCEPTION,
|
||||||
@@ -34,7 +38,7 @@ import {
|
|||||||
RESULT_MSG_TIMEOUT,
|
RESULT_MSG_TIMEOUT,
|
||||||
RESULT_MSG_URL_RESUBMIT,
|
RESULT_MSG_URL_RESUBMIT,
|
||||||
} from '@/constants/result-constants';
|
} 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 { localGet } from '@/utils/cache-local-utils';
|
||||||
import { refreshToken } from '@/api/auth';
|
import { refreshToken } from '@/api/auth';
|
||||||
|
|
||||||
@@ -52,9 +56,7 @@ export type ResultType = {
|
|||||||
|
|
||||||
/**防止重复提交类型 */
|
/**防止重复提交类型 */
|
||||||
type RepeatSubmitType = {
|
type RepeatSubmitType = {
|
||||||
/**请求地址 */
|
/**请求数据MD5 */
|
||||||
url: string;
|
|
||||||
/**请求数据 */
|
|
||||||
data: string;
|
data: string;
|
||||||
/**请求时间 */
|
/**请求时间 */
|
||||||
time: number;
|
time: number;
|
||||||
@@ -158,19 +160,19 @@ function beforeRequest(options: OptionsType): OptionsType | Promise<any> {
|
|||||||
['POST', 'PUT'].includes(options.method)
|
['POST', 'PUT'].includes(options.method)
|
||||||
) {
|
) {
|
||||||
const requestObj: RepeatSubmitType = {
|
const requestObj: RepeatSubmitType = {
|
||||||
url: options.url,
|
data: hexMD5(
|
||||||
data: JSON.stringify(options.data) || '',
|
JSON.stringify({
|
||||||
|
url: options.url,
|
||||||
|
data: JSON.stringify(options.data) || '',
|
||||||
|
})
|
||||||
|
),
|
||||||
time: Date.now(),
|
time: Date.now(),
|
||||||
};
|
};
|
||||||
const sessionObj: RepeatSubmitType = sessionGetJSON(CACHE_SESSION_FATCH);
|
const sessionObj: RepeatSubmitType = sessionGetJSON(CACHE_SESSION_FATCH);
|
||||||
if (sessionObj) {
|
if (sessionObj) {
|
||||||
const { url, data, time } = sessionObj;
|
const { data, time } = sessionObj;
|
||||||
const interval = 3000; // 间隔时间(ms),小于此时间视为重复提交
|
const interval = 3000; // 间隔时间(ms),小于此时间视为重复提交
|
||||||
if (
|
if (requestObj.data === data && requestObj.time - time < interval) {
|
||||||
requestObj.url === url &&
|
|
||||||
requestObj.data === data &&
|
|
||||||
requestObj.time - time < interval
|
|
||||||
) {
|
|
||||||
const message = RESULT_MSG_URL_RESUBMIT[language];
|
const message = RESULT_MSG_URL_RESUBMIT[language];
|
||||||
return Promise.resolve({
|
return Promise.resolve({
|
||||||
code: RESULT_CODE_ERROR,
|
code: RESULT_CODE_ERROR,
|
||||||
@@ -232,27 +234,33 @@ async function beforeResponse(
|
|||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
// console.log('请求后的拦截', res);
|
// console.log('请求后的拦截', res);
|
||||||
|
|
||||||
// 登录失效时,移除授权令牌并重新刷新页面
|
// 移除授权令牌并重新刷新页面
|
||||||
// 登录失效时,移除访问令牌并重新请求
|
function clearToken() {
|
||||||
if (res.code === 401001) {
|
delAccessToken();
|
||||||
const result = await refreshToken(getRefreshToken());
|
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) {
|
if (result.code === RESULT_CODE_SUCCESS) {
|
||||||
setAccessToken(result.data.accessToken, result.data.refreshExpiresIn);
|
setAccessToken(result.data.accessToken, result.data.refreshExpiresIn);
|
||||||
setRefreshToken(result.data.refreshToken, result.data.refreshExpiresIn);
|
setRefreshToken(result.data.refreshToken, result.data.refreshExpiresIn);
|
||||||
return await request(options);
|
return await request(options);
|
||||||
|
} else if (result.code === RESULT_CODE_AUTH_DEVICE) {
|
||||||
|
clearToken();
|
||||||
} else {
|
} else {
|
||||||
debugger
|
// clearToken();
|
||||||
console.warn(result)
|
|
||||||
// delAccessToken();
|
|
||||||
// delRefreshToken();
|
|
||||||
window.location.reload();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ([401002, 401003].includes(res.code)) {
|
// 令牌解析错误
|
||||||
delAccessToken();
|
if ([RESULT_CODE_AUTH_INVALID, RESULT_CODE_AUTH_NOTOKEN].includes(res.code)) {
|
||||||
delRefreshToken();
|
clearToken();
|
||||||
window.location.reload();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 响应数据解密
|
// 响应数据解密
|
||||||
|
|||||||
@@ -48,3 +48,12 @@ export function decryptAES(ciphertext: string, aeskey: string): string {
|
|||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MD5 编码
|
||||||
|
* @param message 字符串信息
|
||||||
|
* @returns hax码
|
||||||
|
*/
|
||||||
|
export function hexMD5(message: string): string {
|
||||||
|
return CryptoJS.MD5(message).toString(CryptoJS.enc.Hex);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user