新增 VITE_API_BASE_URL2 環境變數,更新請求模組以移除 token2 支持,並在登錄頁面中整合新的 token 登錄功能,調整狀態管理以支持多個 token 的存儲。

This commit is contained in:
超级老白兔
2025-08-16 16:31:02 +08:00
parent 85f4ca9744
commit 7dd20b0a9b
7 changed files with 157 additions and 62 deletions

View File

@@ -6,7 +6,7 @@ import axios, {
} from "axios";
import { Toast } from "antd-mobile";
import { useUserStore } from "@/store/module/user";
const { token, token2 } = useUserStore.getState();
const { token } = useUserStore.getState();
const DEFAULT_DEBOUNCE_GAP = 1000;
const debounceMap = new Map<string, number>();
@@ -19,13 +19,7 @@ const instance: AxiosInstance = axios.create({
});
instance.interceptors.request.use((config: any) => {
// 从配置中获取是否使用token2
const useToken2 = config.useToken2;
if (useToken2 && token2) {
config.headers = config.headers || {};
config.headers["Authorization"] = `Bearer ${token2}`;
} else if (token) {
if (token) {
config.headers = config.headers || {};
config.headers["Authorization"] = `Bearer ${token}`;
}
@@ -62,7 +56,6 @@ export function request(
method: Method = "GET",
config?: AxiosRequestConfig,
debounceGap?: number,
isToken2?: boolean,
): Promise<any> {
const gap =
typeof debounceGap === "number" ? debounceGap : DEFAULT_DEBOUNCE_GAP;
@@ -79,10 +72,7 @@ export function request(
url,
method,
...config,
} as any;
// 添加自定义属性
(axiosConfig as any).useToken2 = isToken2;
};
// 如果是FormData不设置Content-Type让浏览器自动设置
if (data instanceof FormData) {

View File

@@ -0,0 +1,79 @@
import axios, {
AxiosInstance,
AxiosRequestConfig,
Method,
AxiosResponse,
} from "axios";
import { Toast } from "antd-mobile";
import { useUserStore } from "@/store/module/user";
const { token2 } = useUserStore.getState();
const DEFAULT_DEBOUNCE_GAP = 1000;
const debounceMap = new Map<string, number>();
interface RequestConfig extends AxiosRequestConfig {
headers: {
Client?: string;
"Content-Type"?: string;
};
}
const instance: AxiosInstance = axios.create({
baseURL: (import.meta as any).env?.VITE_API_BASE_URL2 || "/api",
timeout: 20000,
headers: {
"Content-Type": "application/json",
Client: "kefu-client",
},
});
instance.interceptors.request.use((config: any) => {
if (token2) {
config.headers = config.headers || {};
config.headers["Authorization"] = `Bearer ${token2}`;
}
return config;
});
instance.interceptors.response.use(
(res: AxiosResponse) => {
return res.data;
},
err => {
Toast.show({ content: err.message || "网络异常", position: "top" });
return Promise.reject(err);
},
);
export function request(
url: string,
data?: any,
method: Method = "GET",
config?: RequestConfig,
debounceGap?: number,
): Promise<any> {
const gap =
typeof debounceGap === "number" ? debounceGap : DEFAULT_DEBOUNCE_GAP;
const key = `${method}_${url}_${JSON.stringify(data)}`;
const now = Date.now();
const last = debounceMap.get(key) || 0;
if (gap > 0 && now - last < gap) {
// Toast.show({ content: '请求过于频繁,请稍后再试', position: 'top' });
return Promise.reject("请求过于频繁,请稍后再试");
}
debounceMap.set(key, now);
const axiosConfig: RequestConfig = {
url,
method,
...config,
};
if (method.toUpperCase() === "GET") {
axiosConfig.params = data;
} else {
axiosConfig.data = data;
}
return instance(axiosConfig);
}
export default request;