TODO:可信代码库-接口响应参数解密
This commit is contained in:
@@ -11,6 +11,67 @@ import { useAccountStore } from '@/stores/user';
|
||||
|
||||
const handleError = dealWarning();
|
||||
|
||||
import CryptoJS from 'crypto-js'; // 引入加密库
|
||||
|
||||
// 加密配置
|
||||
const sKey = CryptoJS.enc.Utf8.parse('mXzfCSPBKmEA8aLq');
|
||||
const iv = CryptoJS.enc.Utf8.parse('tbeJJLC6dZQXXtWr');
|
||||
|
||||
/**
|
||||
* 解密函数
|
||||
* @param text 待解密的Base64字符串
|
||||
*/
|
||||
const $Decrypt = (text) => {
|
||||
debugger
|
||||
if (!text) return text;
|
||||
|
||||
try {
|
||||
// 处理Base64编码
|
||||
let src = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Base64.parse(text));
|
||||
|
||||
// AES-CBC-PKCS7解密
|
||||
let bytes = CryptoJS.AES.decrypt(src, sKey, {
|
||||
iv,
|
||||
mode: CryptoJS.mode.CBC,
|
||||
padding: CryptoJS.pad.Pkcs7
|
||||
});
|
||||
|
||||
let originalText = bytes.toString(CryptoJS.enc.Utf8);
|
||||
|
||||
// 尝试解析为JSON对象
|
||||
return JSON.parse(originalText);
|
||||
} catch (e) {
|
||||
console.error('解密失败:', e);
|
||||
return text; // 解密失败返回原始数据
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 加密函数
|
||||
* @param data 待加密的对象或字符串
|
||||
*/
|
||||
const $Encrypt = (data) => {
|
||||
if (!data) return data;
|
||||
|
||||
try {
|
||||
// 将数据转换为JSON字符串
|
||||
const jsonData = typeof data === 'string' ? data : JSON.stringify(data);
|
||||
|
||||
// AES-CBC-PKCS7加密
|
||||
const encrypted = CryptoJS.AES.encrypt(jsonData, sKey, {
|
||||
iv,
|
||||
mode: CryptoJS.mode.CBC,
|
||||
padding: CryptoJS.pad.Pkcs7
|
||||
});
|
||||
|
||||
// 转换为Base64字符串
|
||||
return encrypted.toString();
|
||||
} catch (e) {
|
||||
console.error('加密失败:', e);
|
||||
return data; // 加密失败返回原始数据
|
||||
}
|
||||
};
|
||||
|
||||
/** 设置用户中心相关api迁移时的api前缀 */
|
||||
const setPassportPrefix = (url: string, method: string) => {
|
||||
const prefix = (import.meta as any).env.VITE_PASSPORT_PREFIX;
|
||||
@@ -45,6 +106,10 @@ export const baseDevURL = (import.meta as any).env.VITE_DEV_API_HOST;
|
||||
type CustomConfigs = {
|
||||
// 是否使用自定义的错误处理
|
||||
customError?: boolean;
|
||||
// 是否需要加密请求数据
|
||||
needEncrypt?: boolean;
|
||||
// 是否需要解密响应数据
|
||||
needDecrypt?: boolean;
|
||||
};
|
||||
|
||||
interface PendingTask {
|
||||
@@ -135,6 +200,7 @@ const proxyService = (params: any, customConfigs?: CustomConfigs) => {
|
||||
// response 拦截器
|
||||
service.interceptors.response.use(
|
||||
(response) => {
|
||||
// response.data.data = $Decrypt('OTNNwNE3Q6oeb4diUBcOiA==')
|
||||
return response;
|
||||
},
|
||||
async (error) => {
|
||||
|
||||
Reference in New Issue
Block a user