feat(http): 优化开发环境token获取逻辑,优先从本地服务获取失败后回退到环境变量以提升开发体验

This commit is contained in:
xudan 2025-12-09 15:54:24 +08:00
parent 1b0f6866a5
commit c2b34dc1f0

View File

@ -1,3 +1,4 @@
import { message } from 'ant-design-vue'; import { message } from 'ant-design-vue';
import axios, { type AxiosInstance, type AxiosRequestConfig } from 'axios'; import axios, { type AxiosInstance, type AxiosRequestConfig } from 'axios';
@ -27,15 +28,25 @@ http.interceptors.request.use(
async (config) => { async (config) => {
try { try {
let token = ''; let token = '';
let tenantId = '';
let tenantId = import.meta.env.ENV_DEV_TENANT_ID;
// 开发环境处理逻辑 // 开发环境处理逻辑
if (import.meta.env.DEV) { if (import.meta.env.DEV) {
try { try {
token = import.meta.env.ENV_DEV_TOKEN; // 先尝试从本地服务获取token
tenantId = import.meta.env.ENV_DEV_TENANT_ID; const response = await axios.get('http://localhost:9000/api/token', { timeout: 3000 });
if (response.data.success) {
console.log('使用中间服务获取的token ing');
token = response.data.token;
}
} catch (error) { } catch (error) {
console.error('获取开发环境token失败:', error); console.warn(error+'本地token服务不可用使用环境变量token');
}
// 如果本地服务失败,使用环境变量
if (!token) {
token = import.meta.env.ENV_DEV_TOKEN;
} }
} else { } else {
// 生产环境直接从localStorage获取 // 生产环境直接从localStorage获取