|
|
@@ -19,11 +19,13 @@ import { getCurrentTenantId } from "../utils/tenant";
|
|
|
*/
|
|
|
const transform: AxiosTransform = {
|
|
|
/**
|
|
|
- * 处理请求数据
|
|
|
+ * 响应数据转换处理
|
|
|
*/
|
|
|
- transformRequestHook: (res: AxiosResponse<Result>, options: RequestOptions) => {
|
|
|
+ transformResponseHook: (
|
|
|
+ res: AxiosResponse<Result>,
|
|
|
+ options: RequestOptions
|
|
|
+ ) => {
|
|
|
const { isTransformResponse, isReturnNativeResponse } = options;
|
|
|
-
|
|
|
// 返回原生响应头
|
|
|
if (isReturnNativeResponse) {
|
|
|
return res;
|
|
|
@@ -43,7 +45,8 @@ const transform: AxiosTransform = {
|
|
|
const { code, result, message: msg, success } = data;
|
|
|
|
|
|
// 成功
|
|
|
- const hasSuccess = data && Reflect.has(data, "code") && (code === 200 || code === 0);
|
|
|
+ const hasSuccess =
|
|
|
+ data && Reflect.has(data, "code") && (code === 200 || code === 0);
|
|
|
if (hasSuccess) {
|
|
|
if (success && msg && options.successMessageMode === "success") {
|
|
|
message.success(msg);
|
|
|
@@ -53,14 +56,14 @@ const transform: AxiosTransform = {
|
|
|
|
|
|
// 错误处理
|
|
|
let errorMsg = msg || "请求失败";
|
|
|
-
|
|
|
// 根据不同的 code 处理
|
|
|
switch (code) {
|
|
|
case 401:
|
|
|
errorMsg = msg || "登录已过期";
|
|
|
// 跳转登录页
|
|
|
+ localStorage.removeItem("token");
|
|
|
setTimeout(() => {
|
|
|
- window.location.href = "/login";
|
|
|
+ window.location.href = "/spaces/login";
|
|
|
}, 1500);
|
|
|
break;
|
|
|
default:
|
|
|
@@ -83,7 +86,14 @@ const transform: AxiosTransform = {
|
|
|
* 请求之前处理 config
|
|
|
*/
|
|
|
beforeRequestHook: (config, options) => {
|
|
|
- const { apiUrl, joinPrefix, joinParamsToUrl, formatDate, joinTime = true, urlPrefix } = options;
|
|
|
+ const {
|
|
|
+ apiUrl,
|
|
|
+ joinPrefix,
|
|
|
+ joinParamsToUrl,
|
|
|
+ formatDate,
|
|
|
+ joinTime = true,
|
|
|
+ urlPrefix,
|
|
|
+ } = options;
|
|
|
|
|
|
// 添加前缀
|
|
|
if (joinPrefix && urlPrefix && !config.url?.startsWith("http")) {
|
|
|
@@ -102,7 +112,10 @@ const transform: AxiosTransform = {
|
|
|
if (config.method?.toUpperCase() === "GET") {
|
|
|
if (typeof params === "object") {
|
|
|
// GET 请求加上时间戳参数,避免从缓存中拿数据
|
|
|
- config.params = Object.assign(params || {}, joinTimestamp(joinTime, false));
|
|
|
+ config.params = Object.assign(
|
|
|
+ params || {},
|
|
|
+ joinTimestamp(joinTime, false)
|
|
|
+ );
|
|
|
} else {
|
|
|
// 兼容 restful 风格
|
|
|
config.url = config.url + params + `${joinTimestamp(joinTime, true)}`;
|
|
|
@@ -111,7 +124,11 @@ const transform: AxiosTransform = {
|
|
|
} else {
|
|
|
if (typeof params === "object") {
|
|
|
formatDate && formatRequestDate(params);
|
|
|
- if (Reflect.has(config, "data") && config.data && Object.keys(config.data).length > 0) {
|
|
|
+ if (
|
|
|
+ Reflect.has(config, "data") &&
|
|
|
+ config.data &&
|
|
|
+ Object.keys(config.data).length > 0
|
|
|
+ ) {
|
|
|
config.data = data;
|
|
|
config.params = params;
|
|
|
} else {
|
|
|
@@ -120,7 +137,10 @@ const transform: AxiosTransform = {
|
|
|
config.params = undefined;
|
|
|
}
|
|
|
if (joinParamsToUrl) {
|
|
|
- config.url = setObjToUrlParams(config.url as string, Object.assign({}, config.params, config.data));
|
|
|
+ config.url = setObjToUrlParams(
|
|
|
+ config.url as string,
|
|
|
+ Object.assign({}, config.params, config.data)
|
|
|
+ );
|
|
|
}
|
|
|
} else {
|
|
|
// 兼容 restful 风格
|
|
|
@@ -150,10 +170,11 @@ const transform: AxiosTransform = {
|
|
|
config.headers["X-Tenant-ID"] = String(tenantId);
|
|
|
|
|
|
// 如果需要 token,这里可以添加
|
|
|
- const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IuWGheWuueS4reW_g-euoeeQhuWRmCIsImV4cCI6MTc2NDk2NzgwNn0.mF7iqqQ5sGsHbb5aPSAKyMcVmidPkbWGQoXLYEbdnSI';
|
|
|
+ const token =
|
|
|
+ "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IuWGheWuueS4reW_g-euoeeQhuWRmCIsImV4cCI6MTc2NTA0MjczOX0.xymZCEWejOAfdncfJRdgL27ejI7_7nE2TzT9XX7soXk";
|
|
|
if (token && options.requestOptions?.withToken !== false) {
|
|
|
config.headers.Authorization = token;
|
|
|
- config.headers['X-Access-Token'] = token;
|
|
|
+ config.headers["X-Access-Token"] = token;
|
|
|
}
|
|
|
|
|
|
return config;
|
|
|
@@ -162,16 +183,26 @@ const transform: AxiosTransform = {
|
|
|
/**
|
|
|
* 响应拦截器处理
|
|
|
*/
|
|
|
- responseInterceptors: (res: AxiosResponse<any>) => {
|
|
|
- return res;
|
|
|
- },
|
|
|
+ // responseInterceptors: (res: AxiosResponse<any>) => {
|
|
|
+ // // 可以在这里统一处理响应数据
|
|
|
+ // // 例如:记录日志、刷新 token 等
|
|
|
+
|
|
|
+ // // 检查响应头中的新 token
|
|
|
+ // const newToken = res.headers["x-access-token"] || res.headers["authorization"];
|
|
|
+ // if (newToken) {
|
|
|
+ // // 更新 token(可以存储到 localStorage 或状态管理中)
|
|
|
+ // console.log("[HTTP] New token received:", newToken);
|
|
|
+ // }
|
|
|
+
|
|
|
+ // return res;
|
|
|
+ // },
|
|
|
|
|
|
/**
|
|
|
* 响应错误处理
|
|
|
*/
|
|
|
responseInterceptorsCatch: (error: any) => {
|
|
|
const { response, code, message: msg, config } = error || {};
|
|
|
- const errorMessageMode = config?.requestOptions?.errorMessageMode || "none";
|
|
|
+ const errorMessageMode = config?.requestOptions?.errorMessageMode || "message";
|
|
|
const responseMsg: string = response?.data?.message ?? "";
|
|
|
const err: string = error?.toString?.() ?? "";
|
|
|
let errMessage = "";
|
|
|
@@ -196,7 +227,19 @@ const transform: AxiosTransform = {
|
|
|
throw new Error(error as any);
|
|
|
}
|
|
|
|
|
|
- checkStatus(error?.response?.status, responseMsg, errorMessageMode);
|
|
|
+ // 处理 HTTP 状态码错误
|
|
|
+ if (response?.status) {
|
|
|
+ checkStatus(response.status, responseMsg, errorMessageMode);
|
|
|
+
|
|
|
+ // 401 未授权,跳转登录页
|
|
|
+ if (response.status === 401) {
|
|
|
+ // 清除 token
|
|
|
+ localStorage.removeItem("token");
|
|
|
+ // 跳转登录页
|
|
|
+ window.location.href = "/spaces/login";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
return Promise.reject(error);
|
|
|
},
|
|
|
};
|