|
|
@@ -2,6 +2,8 @@ import axios, { AxiosRequestConfig, AxiosResponse, AxiosError } from 'axios';
|
|
|
import { ElMessage as Message, ElMessageBox as MessageBox, ElLoading as Loading } from 'element-plus';
|
|
|
import tokenInfo from '@/stores/modules/token';
|
|
|
import useUserInfo from "@/stores/modules/user";
|
|
|
+import pinia from "@/stores/index";
|
|
|
+import ENV_CONFIG from "@/config.json";
|
|
|
|
|
|
// 加载动画的并发管理
|
|
|
const activeRequests = new Set<string>();
|
|
|
@@ -24,13 +26,28 @@ function loadingClose(requestId: string) {
|
|
|
*/
|
|
|
const service = axios.create({
|
|
|
timeout: 5000, // 设置请求超时时间
|
|
|
- baseURL: '__API__',
|
|
|
+ // baseURL: '__API__',
|
|
|
});
|
|
|
console.log('__API__');
|
|
|
|
|
|
// 请求拦截器
|
|
|
service.interceptors.request.use(
|
|
|
(config: AxiosRequestConfig) => {
|
|
|
+
|
|
|
+ // 动态设置baseURL
|
|
|
+ const appConfig = pinia.state.value.config?.appConfig;
|
|
|
+ let env = 'local'; // 默认环境
|
|
|
+
|
|
|
+ if (appConfig?.env) {
|
|
|
+ env = appConfig.env;
|
|
|
+ // 从ENV_CONFIG获取对应环境的API地址
|
|
|
+ const apiConfig = ENV_CONFIG[env];
|
|
|
+ if (apiConfig?.api) {
|
|
|
+ config.baseURL = apiConfig.api;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ console.log(`使用环境: ${env}, API地址: ${config.baseURL || '__API__'}`);
|
|
|
+
|
|
|
// 在发送请求之前做些什么,例如添加 token
|
|
|
const tokenInfoStore = tokenInfo();
|
|
|
const token = tokenInfoStore.getToken; // 使用 getToken() 获取 token
|