接口加密
This commit is contained in:
@@ -1,5 +1,66 @@
|
||||
import config from "@/config.js"
|
||||
import { sm4Encrypt, sm4Decrypt } from '@/utils/crypto';
|
||||
import useUserStore from '@/stores/useUserStore';
|
||||
|
||||
const needToEncryptSet = new Set([
|
||||
'POST:/app/login',
|
||||
'GET:/app/user/resume',
|
||||
'POST:/app/user/resume',
|
||||
'POST:/app/user/experience/edit',
|
||||
'POST:/app/user/experience/delete',
|
||||
'GET:/app/user/experience/getSingle',
|
||||
'GET:/app/user/experience/list',
|
||||
'POST:/app/user/cert',
|
||||
'POST:/app/user/getUserArchives',
|
||||
]);
|
||||
|
||||
const encryptPathPrefixes = [
|
||||
'/app/common/',
|
||||
'/app/chat/',
|
||||
'/app/speech/',
|
||||
'/app/job/',
|
||||
'/app/company/',
|
||||
];
|
||||
|
||||
const isEncryptNeeded = (method, url) => {
|
||||
const key = `${method.toUpperCase()}:${url}`;
|
||||
if (needToEncryptSet.has(key)) return true;
|
||||
for (const encryptKey of needToEncryptSet) {
|
||||
const [encryptMethod, encryptUrl] = encryptKey.split(':');
|
||||
if (encryptMethod === method.toUpperCase() && url.startsWith(encryptUrl.split('/{')[0])) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
for (const prefix of encryptPathPrefixes) {
|
||||
if (url.startsWith(prefix)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
const encryptRequestData = (data) => {
|
||||
const jsonData = JSON.stringify(data);
|
||||
// const jsonData = JSON.stringify({a: '1'});
|
||||
console.log('[请求] 加密前:', jsonData)
|
||||
return {
|
||||
encrypted: true,
|
||||
encryptedData: sm4Encrypt(config.sm4Config.key, jsonData),
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
};
|
||||
|
||||
const handleResponseData = (resData) => {
|
||||
try {
|
||||
if (resData?.encrypted) {
|
||||
const decrypted = sm4Decrypt(config.sm4Config.key, resData.encryptedData);
|
||||
resData = JSON.parse(decrypted);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('[请求] 解密失败:', e.message);
|
||||
}
|
||||
return resData;
|
||||
};
|
||||
export function request({
|
||||
url,
|
||||
method = 'GET',
|
||||
@@ -89,25 +150,27 @@ export function createRequest(url, data = {}, method = 'GET', loading = false, h
|
||||
if(needHeader){
|
||||
header["Authorization"] = encodeURIComponent(Authorization);
|
||||
}
|
||||
const requestData = isEncryptNeeded(method, url) ? encryptRequestData(data) : data;
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.request({
|
||||
url: config.baseUrl + url,
|
||||
method: method,
|
||||
data: data,
|
||||
data: requestData,
|
||||
header,
|
||||
success: resData => {
|
||||
// 响应拦截
|
||||
if (resData.statusCode === 200) {
|
||||
const responseData = handleResponseData(resData.data)
|
||||
const {
|
||||
code,
|
||||
msg
|
||||
} = resData.data
|
||||
} = responseData
|
||||
if (code === 200) {
|
||||
resolve(resData.data)
|
||||
resolve(responseData)
|
||||
return
|
||||
}
|
||||
// 处理业务错误
|
||||
if (resData.data?.code === 401 || resData.data?.code === 402) {
|
||||
if (responseData?.code === 401 || responseData?.code === 402) {
|
||||
useUserStore().logOut()
|
||||
}
|
||||
// 显示具体的错误信息
|
||||
|
||||
Reference in New Issue
Block a user