import axios from 'axios' import { ElMessage } from 'element-plus' import router from '@/router' import { getApiBaseURL } from '@/utils/apiHelper' // 创建axios实例 // 自动检测:如果通过 Nginx 访问(包含 ngrok),使用相对路径;否则使用完整 URL const api = axios.create({ baseURL: getApiBaseURL(), timeout: 900000, // 增加到15分钟,适应视频生成时间 withCredentials: true, maxRedirects: 0, // 不自动跟随重定向,手动处理302 headers: { 'Content-Type': 'application/json' }, validateStatus: function (status) { // 允许所有状态码,包括302,让拦截器处理 return status >= 200 && status < 600 } }) // 请求拦截器 api.interceptors.request.use( (config) => { // 使用JWT认证,添加Authorization头 const token = sessionStorage.getItem('token') if (token && token !== 'null' && token.trim() !== '') { config.headers.Authorization = `Bearer ${token}` console.log('请求拦截器:添加Authorization头,token长度:', token.length) } else { console.warn('请求拦截器:未找到有效的token') } return config }, (error) => { console.error('请求拦截器错误:', error) return Promise.reject(error) } ) // 响应拦截器 api.interceptors.response.use( (response) => { // 检查是否是HTML响应(可能是302重定向的结果) if (response.data && typeof response.data === 'string' && response.data.trim().startsWith(' { if (error.response) { const { status, data } = error.response // 检查响应数据是否是HTML(302重定向的结果) if (data && typeof data === 'string' && data.trim().startsWith('