|
@@ -9,10 +9,12 @@ import { AxiosCanceler } from "./helper/axiosCancel";
|
|
|
import { useUserStore } from "@/stores/modules/user";
|
|
import { useUserStore } from "@/stores/modules/user";
|
|
|
import { localGet } from "@/utils";
|
|
import { localGet } from "@/utils";
|
|
|
import router from "@/routers";
|
|
import router from "@/routers";
|
|
|
|
|
+import { crypto } from "@/utils/crypto";
|
|
|
|
|
|
|
|
export interface CustomAxiosRequestConfig extends InternalAxiosRequestConfig {
|
|
export interface CustomAxiosRequestConfig extends InternalAxiosRequestConfig {
|
|
|
loading?: boolean;
|
|
loading?: boolean;
|
|
|
cancel?: boolean;
|
|
cancel?: boolean;
|
|
|
|
|
+ encrypt?: boolean; // 是否加密请求参数
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const config = {
|
|
const config = {
|
|
@@ -26,6 +28,36 @@ const config = {
|
|
|
|
|
|
|
|
const axiosCanceler = new AxiosCanceler();
|
|
const axiosCanceler = new AxiosCanceler();
|
|
|
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * 判断是否应该加密请求
|
|
|
|
|
+ * @param config 请求配置
|
|
|
|
|
+ * @returns 是否加密
|
|
|
|
|
+ */
|
|
|
|
|
+function shouldEncrypt(config: CustomAxiosRequestConfig): boolean {
|
|
|
|
|
+ // 接口级别配置优先于全局配置
|
|
|
|
|
+ if (config.encrypt !== undefined) {
|
|
|
|
|
+ return config.encrypt;
|
|
|
|
|
+ }
|
|
|
|
|
+ // 检查全局加密开关
|
|
|
|
|
+ const globalEnabled = import.meta.env.VITE_API_ENCRYPTION_ENABLED === "true";
|
|
|
|
|
+ return globalEnabled && crypto.isConfigured();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 判断是否应该解密响应
|
|
|
|
|
+ * @param config 请求配置
|
|
|
|
|
+ * @param responseData 响应数据
|
|
|
|
|
+ * @returns 是否解密
|
|
|
|
|
+ */
|
|
|
|
|
+function shouldDecrypt(config: CustomAxiosRequestConfig, responseData: any): boolean {
|
|
|
|
|
+ // 只有在请求加密了,并且响应数据是字符串时才解密
|
|
|
|
|
+ if (!shouldEncrypt(config)) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ // 检查响应数据是否为字符串(Base64 编码的密文)
|
|
|
|
|
+ return typeof responseData === "string" && crypto.isConfigured();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
class RequestHttp {
|
|
class RequestHttp {
|
|
|
service: AxiosInstance;
|
|
service: AxiosInstance;
|
|
|
public constructor(config: AxiosRequestConfig) {
|
|
public constructor(config: AxiosRequestConfig) {
|
|
@@ -49,6 +81,21 @@ class RequestHttp {
|
|
|
if (config.headers && typeof config.headers.set === "function") {
|
|
if (config.headers && typeof config.headers.set === "function") {
|
|
|
config.headers.set("Authorization", userStore.token);
|
|
config.headers.set("Authorization", userStore.token);
|
|
|
}
|
|
}
|
|
|
|
|
+ // 加密处理
|
|
|
|
|
+ if (shouldEncrypt(config) && config.data) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ config.data = crypto.encrypt(config.data);
|
|
|
|
|
+ // 设置 Content-Type 为 application/json
|
|
|
|
|
+ if (config.headers && typeof config.headers.set === "function") {
|
|
|
|
|
+ config.headers.set("Content-Type", "application/json");
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ console.error("加密失败:", error);
|
|
|
|
|
+ // 加密失败时可以选择降级为明文传输或抛出错误
|
|
|
|
|
+ // 这里选择降级为明文传输,以避免影响业务
|
|
|
|
|
+ return Promise.reject(new Error("请求参数加密失败"));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
return config;
|
|
return config;
|
|
|
},
|
|
},
|
|
|
(error: AxiosError) => {
|
|
(error: AxiosError) => {
|
|
@@ -67,20 +114,34 @@ class RequestHttp {
|
|
|
const userStore = useUserStore();
|
|
const userStore = useUserStore();
|
|
|
axiosCanceler.removePending(config);
|
|
axiosCanceler.removePending(config);
|
|
|
config.loading && tryHideFullScreenLoading();
|
|
config.loading && tryHideFullScreenLoading();
|
|
|
|
|
+ // 解密处理
|
|
|
|
|
+ if (shouldDecrypt(config, data)) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ const decryptedData = crypto.decrypt(data);
|
|
|
|
|
+ // 将解密后的数据赋值给 data
|
|
|
|
|
+ // @ts-expect-error - 解密后的数据类型可能与原始类型不同
|
|
|
|
|
+ response.data = decryptedData;
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ console.error("解密失败:", error);
|
|
|
|
|
+ ElMessage.error("响应数据解密失败");
|
|
|
|
|
+ return Promise.reject(new Error("响应数据解密失败"));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ const processedData = response.data;
|
|
|
// 登录失效
|
|
// 登录失效
|
|
|
- if (data.code == ResultEnum.OVERDUE) {
|
|
|
|
|
|
|
+ if (processedData.code == ResultEnum.OVERDUE) {
|
|
|
userStore.setToken("");
|
|
userStore.setToken("");
|
|
|
router.replace(LOGIN_URL);
|
|
router.replace(LOGIN_URL);
|
|
|
- ElMessage.error(data.msg);
|
|
|
|
|
- return Promise.reject(data);
|
|
|
|
|
|
|
+ ElMessage.error(processedData.msg);
|
|
|
|
|
+ return Promise.reject(processedData);
|
|
|
}
|
|
}
|
|
|
// 全局错误信息拦截(防止下载文件的时候返回数据流,没有 code 直接报错)
|
|
// 全局错误信息拦截(防止下载文件的时候返回数据流,没有 code 直接报错)
|
|
|
- if (data.code && data.code !== ResultEnum.SUCCESS) {
|
|
|
|
|
- ElMessage.error(data.msg);
|
|
|
|
|
- return Promise.reject(data);
|
|
|
|
|
|
|
+ if (processedData.code && processedData.code !== ResultEnum.SUCCESS) {
|
|
|
|
|
+ ElMessage.error(processedData.msg);
|
|
|
|
|
+ return Promise.reject(processedData);
|
|
|
}
|
|
}
|
|
|
// 成功请求(在页面上除非特殊情况,否则不用处理失败逻辑)
|
|
// 成功请求(在页面上除非特殊情况,否则不用处理失败逻辑)
|
|
|
- return data;
|
|
|
|
|
|
|
+ return processedData;
|
|
|
},
|
|
},
|
|
|
async (error: AxiosError) => {
|
|
async (error: AxiosError) => {
|
|
|
const { response } = error;
|
|
const { response } = error;
|