2024-06-02 20:50:51 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 该文件可自行根据业务逻辑进行调整
|
|
|
|
|
|
*/
|
2024-09-25 23:09:48 +08:00
|
|
|
|
import type { HttpResponse } from '@vben/request';
|
|
|
|
|
|
|
2024-07-28 14:29:05 +08:00
|
|
|
|
import { useAppConfig } from '@vben/hooks';
|
2024-07-23 00:03:59 +08:00
|
|
|
|
import { preferences } from '@vben/preferences';
|
2024-08-19 22:59:42 +08:00
|
|
|
|
import {
|
|
|
|
|
|
authenticateResponseInterceptor,
|
|
|
|
|
|
errorMessageResponseInterceptor,
|
|
|
|
|
|
RequestClient,
|
|
|
|
|
|
} from '@vben/request';
|
2024-07-30 21:10:28 +08:00
|
|
|
|
import { useAccessStore } from '@vben/stores';
|
2024-06-02 20:50:51 +08:00
|
|
|
|
|
|
|
|
|
|
import { message } from 'ant-design-vue';
|
2025-04-11 17:06:02 +08:00
|
|
|
|
// eslint-disable-next-line n/no-extraneous-import
|
|
|
|
|
|
import { Base64 } from 'js-base64';
|
2024-06-02 20:50:51 +08:00
|
|
|
|
|
2024-07-30 21:10:28 +08:00
|
|
|
|
import { useAuthStore } from '#/store';
|
2024-06-02 20:50:51 +08:00
|
|
|
|
|
2024-08-19 22:59:42 +08:00
|
|
|
|
import { refreshTokenApi } from './core';
|
|
|
|
|
|
|
2024-07-28 14:29:05 +08:00
|
|
|
|
const { apiURL } = useAppConfig(import.meta.env, import.meta.env.PROD);
|
|
|
|
|
|
|
2025-03-07 08:18:45 +08:00
|
|
|
|
const arr = new Set([
|
|
|
|
|
|
'accept_tel',
|
|
|
|
|
|
'doctor',
|
|
|
|
|
|
'express_mobile',
|
|
|
|
|
|
'id_card',
|
|
|
|
|
|
'idcard',
|
|
|
|
|
|
'mobile',
|
|
|
|
|
|
'password',
|
|
|
|
|
|
'patient_mobile',
|
|
|
|
|
|
'phone',
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
// 敏感数据
|
|
|
|
|
|
const sensitiveData = new Set(['id_card', 'idcard']);
|
|
|
|
|
|
|
2024-07-28 14:29:05 +08:00
|
|
|
|
function createRequestClient(baseURL: string) {
|
2024-06-02 20:50:51 +08:00
|
|
|
|
const client = new RequestClient({
|
2024-07-28 14:29:05 +08:00
|
|
|
|
baseURL,
|
2024-08-19 22:59:42 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 重新认证逻辑
|
|
|
|
|
|
*/
|
|
|
|
|
|
async function doReAuthenticate() {
|
|
|
|
|
|
const accessStore = useAccessStore();
|
|
|
|
|
|
const authStore = useAuthStore();
|
|
|
|
|
|
accessStore.setAccessToken(null);
|
2024-08-20 22:33:25 +08:00
|
|
|
|
if (
|
|
|
|
|
|
preferences.app.loginExpiredMode === 'modal' &&
|
|
|
|
|
|
accessStore.isAccessChecked
|
|
|
|
|
|
) {
|
2024-08-19 22:59:42 +08:00
|
|
|
|
accessStore.setLoginExpired(true);
|
|
|
|
|
|
} else {
|
2025-04-11 17:06:02 +08:00
|
|
|
|
await authStore.logout(true);
|
2024-08-19 22:59:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 刷新token逻辑
|
|
|
|
|
|
*/
|
|
|
|
|
|
async function doRefreshToken() {
|
|
|
|
|
|
const accessStore = useAccessStore();
|
|
|
|
|
|
const resp = await refreshTokenApi();
|
|
|
|
|
|
const newToken = resp.data;
|
|
|
|
|
|
accessStore.setAccessToken(newToken);
|
|
|
|
|
|
return newToken;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function formatToken(token: null | string) {
|
|
|
|
|
|
return token ? `Bearer ${token}` : null;
|
|
|
|
|
|
}
|
2024-07-28 14:29:05 +08:00
|
|
|
|
|
2024-08-19 22:59:42 +08:00
|
|
|
|
// 请求头处理
|
|
|
|
|
|
client.addRequestInterceptor({
|
|
|
|
|
|
fulfilled: async (config) => {
|
|
|
|
|
|
const accessStore = useAccessStore();
|
|
|
|
|
|
|
|
|
|
|
|
config.headers.Authorization = formatToken(accessStore.accessToken);
|
|
|
|
|
|
config.headers['Accept-Language'] = preferences.app.locale;
|
2025-03-07 08:18:45 +08:00
|
|
|
|
// 加密
|
|
|
|
|
|
// eslint-disable-next-line eqeqeq
|
|
|
|
|
|
if (config.data != null) {
|
|
|
|
|
|
config.data = getRes(config.data, false);
|
|
|
|
|
|
}
|
|
|
|
|
|
// eslint-disable-next-line eqeqeq
|
|
|
|
|
|
if (config.params != null) {
|
|
|
|
|
|
config.params = getRes(config.params, false);
|
|
|
|
|
|
}
|
2024-08-19 22:59:42 +08:00
|
|
|
|
return config;
|
2024-07-28 14:29:05 +08:00
|
|
|
|
},
|
2024-06-02 20:50:51 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
2024-08-19 22:59:42 +08:00
|
|
|
|
// response数据解构
|
2024-09-25 23:09:48 +08:00
|
|
|
|
client.addResponseInterceptor<HttpResponse>({
|
2024-08-19 22:59:42 +08:00
|
|
|
|
fulfilled: (response) => {
|
|
|
|
|
|
const { data: responseData, status } = response;
|
|
|
|
|
|
|
2025-03-07 08:18:45 +08:00
|
|
|
|
// 这里吧 data 换成了 result
|
|
|
|
|
|
const { code, result } = responseData;
|
|
|
|
|
|
// 判断返回状态码
|
2025-04-11 17:06:02 +08:00
|
|
|
|
if (status >= 200 && status <= 500) {
|
2025-03-07 08:18:45 +08:00
|
|
|
|
switch (code) {
|
|
|
|
|
|
case 0: {
|
|
|
|
|
|
// 解密
|
|
|
|
|
|
// eslint-disable-next-line eqeqeq
|
|
|
|
|
|
if (result != null) {
|
|
|
|
|
|
return getRes(result);
|
|
|
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
case 401: {
|
2025-04-11 17:06:02 +08:00
|
|
|
|
response.status = 401;
|
2025-03-07 08:18:45 +08:00
|
|
|
|
// 401 错误,需要重新登录
|
2025-04-11 17:06:02 +08:00
|
|
|
|
authenticateResponseInterceptor({
|
|
|
|
|
|
client,
|
|
|
|
|
|
doReAuthenticate,
|
|
|
|
|
|
doRefreshToken,
|
|
|
|
|
|
enableRefreshToken: preferences.app.enableRefreshToken,
|
|
|
|
|
|
formatToken,
|
|
|
|
|
|
});
|
|
|
|
|
|
break;
|
2025-03-07 08:18:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
case 403:
|
|
|
|
|
|
case 404:
|
|
|
|
|
|
case 500: {
|
|
|
|
|
|
throw Object.assign({}, response, { response });
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-08-19 22:59:42 +08:00
|
|
|
|
}
|
2024-10-18 22:00:41 +08:00
|
|
|
|
|
2024-10-19 19:50:23 +08:00
|
|
|
|
throw Object.assign({}, response, { response });
|
2024-08-19 22:59:42 +08:00
|
|
|
|
},
|
2024-07-11 20:11:11 +08:00
|
|
|
|
});
|
2024-08-19 22:59:42 +08:00
|
|
|
|
|
|
|
|
|
|
// token过期的处理
|
|
|
|
|
|
client.addResponseInterceptor(
|
|
|
|
|
|
authenticateResponseInterceptor({
|
|
|
|
|
|
client,
|
|
|
|
|
|
doReAuthenticate,
|
|
|
|
|
|
doRefreshToken,
|
|
|
|
|
|
enableRefreshToken: preferences.app.enableRefreshToken,
|
|
|
|
|
|
formatToken,
|
|
|
|
|
|
}),
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// 通用的错误处理,如果没有进入上面的错误处理逻辑,就会进入这里
|
|
|
|
|
|
client.addResponseInterceptor(
|
2024-10-09 22:08:55 +08:00
|
|
|
|
errorMessageResponseInterceptor((msg: string, error) => {
|
2024-09-23 22:59:58 +08:00
|
|
|
|
// 这里可以根据业务进行定制,你可以拿到 error 内的信息进行定制化处理,根据不同的 code 做不同的提示,而不是直接使用 message.error 提示 msg
|
2024-10-09 22:08:55 +08:00
|
|
|
|
// 当前mock接口返回的错误字段是 error 或者 message
|
|
|
|
|
|
const responseData = error?.response?.data ?? {};
|
|
|
|
|
|
const errorMessage = responseData?.error ?? responseData?.message ?? '';
|
|
|
|
|
|
// 如果没有错误信息,则会根据状态码进行提示
|
|
|
|
|
|
message.error(errorMessage || msg);
|
2024-09-23 22:59:58 +08:00
|
|
|
|
}),
|
2024-08-19 22:59:42 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
2024-07-11 20:11:11 +08:00
|
|
|
|
return client;
|
2024-06-02 20:50:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-28 14:29:05 +08:00
|
|
|
|
export const requestClient = createRequestClient(apiURL);
|
2024-08-19 22:59:42 +08:00
|
|
|
|
|
|
|
|
|
|
export const baseRequestClient = new RequestClient({ baseURL: apiURL });
|
2025-03-07 08:18:45 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 加解密方法
|
|
|
|
|
|
* @param obj
|
|
|
|
|
|
* @param isDecode
|
|
|
|
|
|
*/
|
|
|
|
|
|
function getRes(obj: any, isDecode = true) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
for (const key in obj) {
|
|
|
|
|
|
if (Array.isArray(obj[key])) {
|
|
|
|
|
|
obj[key] = getRes(obj[key]);
|
|
|
|
|
|
} else if (typeof obj[key] === 'object') {
|
|
|
|
|
|
obj[key] = getRes(obj[key]);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 判断obj[key]是否在arr中
|
|
|
|
|
|
if (arr.has(key)) {
|
|
|
|
|
|
let aseFile = '';
|
|
|
|
|
|
aseFile = isDecode
|
2025-04-03 16:17:07 +08:00
|
|
|
|
? customBase64Decode(obj[key], key)
|
2025-03-07 08:18:45 +08:00
|
|
|
|
: customBase64Encode(obj[key]);
|
|
|
|
|
|
const isGarbled =
|
|
|
|
|
|
// eslint-disable-next-line no-control-regex
|
|
|
|
|
|
/[\u0000-\u0008\v\f\u000E-\u001F\u007F-\u00FF\u{E000}-\u{F8FF}]/u.test(
|
|
|
|
|
|
aseFile,
|
|
|
|
|
|
);
|
|
|
|
|
|
if (isGarbled) {
|
|
|
|
|
|
aseFile = obj[key];
|
|
|
|
|
|
}
|
|
|
|
|
|
if (isDecode && sensitiveData.has(key)) {
|
|
|
|
|
|
// 数据脱敏,自动匹配姓名、手机号、身份证
|
|
|
|
|
|
switch (key) {
|
|
|
|
|
|
// case 'accept_name':
|
|
|
|
|
|
// case 'express_name':
|
|
|
|
|
|
// case 'express_region':
|
|
|
|
|
|
// case 'patient': {
|
|
|
|
|
|
// // 保留前两个字符,其余用星号代替
|
|
|
|
|
|
// aseFile = aseFile.slice(0, 1).padEnd(aseFile.length, '*');
|
|
|
|
|
|
// break;
|
|
|
|
|
|
// }
|
|
|
|
|
|
// case 'express_mobile':
|
|
|
|
|
|
// case 'mobile': {
|
|
|
|
|
|
// // 保留前三位和后四位,中间用星号代替
|
|
|
|
|
|
// aseFile =
|
|
|
|
|
|
// aseFile.slice(0, 3).padEnd(aseFile.length - 4, '*') +
|
|
|
|
|
|
// aseFile.slice(-4);
|
|
|
|
|
|
// break;
|
|
|
|
|
|
// }
|
|
|
|
|
|
case 'id_card':
|
|
|
|
|
|
case 'idcard': {
|
|
|
|
|
|
// 保留前六位和后四位,中间用星号代替
|
|
|
|
|
|
aseFile =
|
|
|
|
|
|
aseFile.slice(0, 6).padEnd(aseFile.length - 4, '*') +
|
|
|
|
|
|
aseFile.slice(-4);
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
default: {
|
|
|
|
|
|
// 默认情况下,全部用星号代替
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
obj[key] = aseFile;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return obj;
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('错误', error);
|
|
|
|
|
|
return obj;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 解密
|
|
|
|
|
|
* @param data
|
2025-04-03 16:17:07 +08:00
|
|
|
|
* @param key
|
2025-03-07 08:18:45 +08:00
|
|
|
|
*/
|
2025-04-03 16:17:07 +08:00
|
|
|
|
function customBase64Decode(data: string, key = '') {
|
2025-03-07 08:18:45 +08:00
|
|
|
|
// return data
|
|
|
|
|
|
try {
|
|
|
|
|
|
// 第一次Base64解码
|
|
|
|
|
|
const decodedFirst = Base64.decode(data);
|
|
|
|
|
|
// 截取从第30个字符开始往后的内容
|
|
|
|
|
|
const subStr = decodedFirst.slice(30);
|
|
|
|
|
|
// 第二次Base64解码
|
|
|
|
|
|
const base64 = Base64.decode(subStr);
|
|
|
|
|
|
return base64.length > 0 ? base64 : data;
|
|
|
|
|
|
} catch (error) {
|
2025-04-03 16:17:07 +08:00
|
|
|
|
console.error(
|
|
|
|
|
|
`解码Base64字符串【${data}】时发生错误,KEY:【${key}】`,
|
|
|
|
|
|
error,
|
|
|
|
|
|
);
|
2025-03-07 08:18:45 +08:00
|
|
|
|
return data;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 加密
|
|
|
|
|
|
* @param data
|
|
|
|
|
|
*/
|
|
|
|
|
|
function customBase64Encode(data: string) {
|
|
|
|
|
|
// eslint-disable-next-line eqeqeq
|
|
|
|
|
|
if (data == null || data === '') return data;
|
|
|
|
|
|
// return data
|
|
|
|
|
|
try {
|
|
|
|
|
|
// 和加密方法差不多但是是要生成30个随机字符
|
|
|
|
|
|
let randomString = '';
|
|
|
|
|
|
for (let i = 0; i < 30; i++) {
|
|
|
|
|
|
const randomChar = String.fromCodePoint(
|
|
|
|
|
|
Math.floor(Math.random() * (126 - 32 + 1)) + 32,
|
|
|
|
|
|
);
|
|
|
|
|
|
randomString += randomChar;
|
|
|
|
|
|
}
|
|
|
|
|
|
const encoded = Base64.encode(data);
|
|
|
|
|
|
return Base64.encode(randomString + encoded);
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error(`加密字符串【${data}】时发生错误`, error);
|
|
|
|
|
|
return data;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|