feat : 新增sm4加密

This commit is contained in:
bin
2025-11-28 16:28:54 +08:00
parent 7c8a91eb2d
commit d154196321
7 changed files with 308 additions and 186 deletions

View File

@@ -17,6 +17,7 @@ import {
import { PageEnum } from './enums/pagesEnums';
import { stringify } from 'querystring';
import { message } from 'antd';
import { encrypt, decrypt, needEncrypt } from '@/utils/encrypt';
const isDev = process.env.NODE_ENV === 'development';
const loginOut = async () => {
@@ -217,18 +218,20 @@ export async function render(oldRender: () => void) {
const checkRegion = 5 * 60 * 1000;
export const request = {
...errorConfig,
// baseURL: process.env.NODE_ENV === 'development' ? '' : 'https://qd.zhaopinzao8dian.com/api',
baseURL: process.env.NODE_ENV === 'development' ? '' : 'https://qd.zhaopinzao8dian.com/api',
// baseURL: 'http://39.98.44.136:8080',
baseURL:
process.env.NODE_ENV === 'development'
? 'http://10.213.6.207:19010'
: 'http://10.213.6.207:19010/api',
// baseURL:
// process.env.NODE_ENV === 'development'
// ? 'http://10.213.6.207:19010'
// : 'http://10.213.6.207:19010/api',
requestInterceptors: [
(url: any, options: { headers: any }) => {
const headers = options.headers ? options.headers : [];
(url: any, options: { headers: any; data?: any; params?: any; method?: string }) => {
const headers = options.headers ? options.headers : {};
console.log('request ====>:', url);
const authHeader = headers['Authorization'];
const isToken = headers['isToken'];
// 处理认证token
if (!authHeader && isToken !== false) {
const expireTime = getTokenExpireTime();
if (expireTime) {
@@ -248,18 +251,82 @@ export const request = {
clearSessionToken();
}
}
// 处理SM4加密 - 根据config的isEncrypt来判断
if (needEncrypt(options)) {
console.log('进行SM4加密处理');
let requestData = options.data;
let requestParams = options.params;
// 加密请求数据
if (requestData && Object.keys(requestData).length > 0) {
const jsonData = JSON.stringify(requestData);
const encryptedBody = encrypt(jsonData);
requestData = {
encrypted: true,
encryptedData: encryptedBody,
timestamp: Date.now(),
};
}
// 加密查询参数
if (requestParams && Object.keys(requestParams).length > 0) {
const jsonParams = JSON.stringify(requestParams);
const encryptedParams = encrypt(jsonParams);
requestParams = {
encrypted: true,
encryptedData: encryptedParams,
timestamp: Date.now(),
};
}
// 添加加密标识头
headers['X-Encrypted'] = 'true';
return {
url,
options: {
...options,
headers,
data: requestData,
params: requestParams,
},
};
}
// 处理开发环境API路径
if (process.env.NODE_ENV !== 'development') {
if (url.startsWith('/api')) {
url = url.replace(/^\/api/, '');
}
}
return { url, options };
return { url, options: { ...options, headers } };
},
],
responseInterceptors: [
(response) => {
// 不再需要异步处理读取返回体内容可直接在data中读出部分字段可在 config 中找到
const { data = {} as any, config } = response;
// 检查是否需要解密
const isEncrypted = data.encrypted;
if (isEncrypted && data.encryptedData) {
console.log('进行SM4解密处理');
try {
// 解密响应数据
const decryptedData = decrypt(data.encryptedData);
response.data =
typeof decryptedData === 'string' ? JSON.parse(decryptedData) : decryptedData;
} catch (error) {
console.error('响应解密失败:', error);
// 如果解密失败,保持原始数据
}
}
// 处理业务状态码
switch (data.code) {
case 401:
loginOut();
@@ -268,8 +335,7 @@ export const request = {
if (data.code !== 200 && data.msg) {
message.info(data.msg);
}
// console.log('data: ', data)
// console.log('config: ', config)
return response;
},
],