2025-10-21 16:50:33 +08:00
|
|
|
|
import axios from 'axios'
|
|
|
|
|
|
import { ElMessage } from 'element-plus'
|
|
|
|
|
|
import router from '@/router'
|
2025-11-03 18:09:23 +08:00
|
|
|
|
import { getApiBaseURL } from '@/utils/apiHelper'
|
2025-10-21 16:50:33 +08:00
|
|
|
|
|
|
|
|
|
|
// 创建axios实例
|
2025-11-03 18:09:23 +08:00
|
|
|
|
// 自动检测:如果通过 Nginx 访问(包含 ngrok),使用相对路径;否则使用完整 URL
|
2025-10-21 16:50:33 +08:00
|
|
|
|
const api = axios.create({
|
2025-11-03 18:09:23 +08:00
|
|
|
|
baseURL: getApiBaseURL(),
|
2025-10-27 10:46:49 +08:00
|
|
|
|
timeout: 900000, // 增加到15分钟,适应视频生成时间
|
2025-10-21 16:50:33 +08:00
|
|
|
|
withCredentials: true,
|
2025-11-05 18:18:53 +08:00
|
|
|
|
maxRedirects: 0, // 不自动跟随重定向,手动处理302
|
2025-10-21 16:50:33 +08:00
|
|
|
|
headers: {
|
|
|
|
|
|
'Content-Type': 'application/json'
|
2025-11-05 18:18:53 +08:00
|
|
|
|
},
|
|
|
|
|
|
validateStatus: function (status) {
|
|
|
|
|
|
// 允许所有状态码,包括302,让拦截器处理
|
|
|
|
|
|
return status >= 200 && status < 600
|
2025-10-21 16:50:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// 请求拦截器
|
|
|
|
|
|
api.interceptors.request.use(
|
|
|
|
|
|
(config) => {
|
2025-11-13 17:01:39 +08:00
|
|
|
|
// 登录相关的接口不需要添加token
|
|
|
|
|
|
const loginUrls = [
|
|
|
|
|
|
'/auth/login',
|
|
|
|
|
|
'/auth/login/email',
|
|
|
|
|
|
'/auth/register',
|
|
|
|
|
|
'/verification/email/send',
|
|
|
|
|
|
'/verification/email/verify',
|
|
|
|
|
|
'/verification/email/dev-set',
|
|
|
|
|
|
'/public/'
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前请求是否是登录相关接口
|
|
|
|
|
|
const isLoginRequest = loginUrls.some(url => config.url.includes(url))
|
|
|
|
|
|
|
|
|
|
|
|
if (!isLoginRequest) {
|
|
|
|
|
|
// 非登录请求才添加Authorization头
|
2025-12-11 13:32:24 +08:00
|
|
|
|
const token = localStorage.getItem('token')
|
2025-11-13 17:01:39 +08:00
|
|
|
|
if (token && token !== 'null' && token.trim() !== '') {
|
|
|
|
|
|
config.headers.Authorization = `Bearer ${token}`
|
|
|
|
|
|
console.log('请求拦截器:添加Authorization头,token长度:', token.length)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
console.warn('请求拦截器:未找到有效的token')
|
|
|
|
|
|
}
|
2025-11-05 18:18:53 +08:00
|
|
|
|
} else {
|
2025-11-13 17:01:39 +08:00
|
|
|
|
console.log('请求拦截器:登录相关请求,不添加token:', config.url)
|
2025-10-21 16:50:33 +08:00
|
|
|
|
}
|
2025-11-13 17:01:39 +08:00
|
|
|
|
|
2025-10-21 16:50:33 +08:00
|
|
|
|
return config
|
|
|
|
|
|
},
|
|
|
|
|
|
(error) => {
|
|
|
|
|
|
console.error('请求拦截器错误:', error)
|
|
|
|
|
|
return Promise.reject(error)
|
|
|
|
|
|
}
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// 响应拦截器
|
|
|
|
|
|
api.interceptors.response.use(
|
|
|
|
|
|
(response) => {
|
2025-11-05 18:18:53 +08:00
|
|
|
|
// 检查是否是HTML响应(可能是302重定向的结果)
|
|
|
|
|
|
if (response.data && typeof response.data === 'string' && response.data.trim().startsWith('<!DOCTYPE')) {
|
|
|
|
|
|
console.error('收到HTML响应,可能是认证失败:', response.config.url)
|
2025-11-13 17:01:39 +08:00
|
|
|
|
|
|
|
|
|
|
// 只有非登录请求才清除token并跳转
|
|
|
|
|
|
const loginUrls = ['/auth/login', '/auth/login/email', '/auth/register', '/verification/', '/public/']
|
|
|
|
|
|
const isLoginRequest = loginUrls.some(url => response.config.url.includes(url))
|
|
|
|
|
|
|
|
|
|
|
|
if (!isLoginRequest) {
|
|
|
|
|
|
// 清除无效的token并跳转到登录页
|
2025-12-11 13:32:24 +08:00
|
|
|
|
localStorage.removeItem('token')
|
|
|
|
|
|
localStorage.removeItem('user')
|
2025-11-13 17:01:39 +08:00
|
|
|
|
// 避免重复跳转
|
|
|
|
|
|
if (router.currentRoute.value.path !== '/login') {
|
|
|
|
|
|
ElMessage.error('认证失败,请重新登录')
|
|
|
|
|
|
router.push('/login')
|
|
|
|
|
|
}
|
2025-11-05 18:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
// 返回错误,让调用方知道这是认证失败
|
|
|
|
|
|
return Promise.reject(new Error('认证失败:收到HTML响应'))
|
|
|
|
|
|
}
|
2025-11-13 17:01:39 +08:00
|
|
|
|
|
2025-11-05 18:18:53 +08:00
|
|
|
|
// 检查302重定向
|
|
|
|
|
|
if (response.status === 302) {
|
|
|
|
|
|
console.error('收到302重定向,可能是认证失败:', response.config.url)
|
2025-11-13 17:01:39 +08:00
|
|
|
|
|
|
|
|
|
|
// 只有非登录请求才清除token并跳转
|
|
|
|
|
|
const loginUrls = ['/auth/login', '/auth/login/email', '/auth/register', '/verification/', '/public/']
|
|
|
|
|
|
const isLoginRequest = loginUrls.some(url => response.config.url.includes(url))
|
|
|
|
|
|
|
|
|
|
|
|
if (!isLoginRequest) {
|
2025-12-11 13:32:24 +08:00
|
|
|
|
localStorage.removeItem('token')
|
|
|
|
|
|
localStorage.removeItem('user')
|
2025-11-13 17:01:39 +08:00
|
|
|
|
if (router.currentRoute.value.path !== '/login') {
|
|
|
|
|
|
ElMessage.error('认证失败,请重新登录')
|
|
|
|
|
|
router.push('/login')
|
|
|
|
|
|
}
|
2025-11-05 18:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
return Promise.reject(new Error('认证失败:302重定向'))
|
|
|
|
|
|
}
|
2025-11-13 17:01:39 +08:00
|
|
|
|
|
2025-10-27 10:46:49 +08:00
|
|
|
|
// 直接返回response,让调用方处理data
|
|
|
|
|
|
return response
|
2025-10-21 16:50:33 +08:00
|
|
|
|
},
|
|
|
|
|
|
(error) => {
|
|
|
|
|
|
if (error.response) {
|
|
|
|
|
|
const { status, data } = error.response
|
2025-11-13 17:01:39 +08:00
|
|
|
|
|
2025-11-05 18:18:53 +08:00
|
|
|
|
// 检查响应数据是否是HTML(302重定向的结果)
|
|
|
|
|
|
if (data && typeof data === 'string' && data.trim().startsWith('<!DOCTYPE')) {
|
|
|
|
|
|
console.error('收到HTML响应(可能是302重定向):', error.config.url)
|
2025-11-13 17:01:39 +08:00
|
|
|
|
|
|
|
|
|
|
// 只有非登录请求才清除token并跳转
|
|
|
|
|
|
const loginUrls = ['/auth/login', '/auth/login/email', '/auth/register', '/verification/', '/public/']
|
|
|
|
|
|
const isLoginRequest = loginUrls.some(url => error.config.url.includes(url))
|
|
|
|
|
|
|
|
|
|
|
|
if (!isLoginRequest) {
|
2025-12-11 13:32:24 +08:00
|
|
|
|
localStorage.removeItem('token')
|
|
|
|
|
|
localStorage.removeItem('user')
|
2025-11-13 17:01:39 +08:00
|
|
|
|
if (router.currentRoute.value.path !== '/login') {
|
|
|
|
|
|
ElMessage.error('认证失败,请重新登录')
|
|
|
|
|
|
router.push('/login')
|
|
|
|
|
|
}
|
2025-11-05 18:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
return Promise.reject(error)
|
|
|
|
|
|
}
|
2025-11-13 17:01:39 +08:00
|
|
|
|
|
2025-10-21 16:50:33 +08:00
|
|
|
|
switch (status) {
|
|
|
|
|
|
case 401:
|
2025-11-05 18:18:53 +08:00
|
|
|
|
case 302:
|
2025-11-13 17:01:39 +08:00
|
|
|
|
// 只有非登录请求才清除token并跳转
|
|
|
|
|
|
const loginUrls = ['/auth/login', '/auth/login/email', '/auth/register', '/verification/', '/public/']
|
|
|
|
|
|
const isLoginRequest = loginUrls.some(url => error.config.url.includes(url))
|
|
|
|
|
|
|
|
|
|
|
|
if (!isLoginRequest) {
|
|
|
|
|
|
// 302也可能是认证失败导致的
|
2025-12-11 13:32:24 +08:00
|
|
|
|
localStorage.removeItem('token')
|
|
|
|
|
|
localStorage.removeItem('user')
|
2025-11-13 17:01:39 +08:00
|
|
|
|
if (router.currentRoute.value.path !== '/login') {
|
|
|
|
|
|
ElMessage.error('认证失败,请重新登录')
|
|
|
|
|
|
router.push('/login')
|
|
|
|
|
|
}
|
2025-11-05 18:18:53 +08:00
|
|
|
|
}
|
2025-10-21 16:50:33 +08:00
|
|
|
|
break
|
|
|
|
|
|
case 403:
|
2025-11-13 17:01:39 +08:00
|
|
|
|
// 403可能是权限不足或CORS问题
|
|
|
|
|
|
// 如果是登录请求的403,不要显示"权限不足",而是显示具体错误信息
|
|
|
|
|
|
const loginUrls403 = ['/auth/login', '/auth/login/email', '/auth/register', '/verification/', '/public/']
|
|
|
|
|
|
const isLoginRequest403 = loginUrls403.some(url => error.config.url.includes(url))
|
|
|
|
|
|
|
|
|
|
|
|
if (!isLoginRequest403) {
|
|
|
|
|
|
ElMessage.error('权限不足')
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 登录请求的403,显示具体错误或网络问题
|
|
|
|
|
|
ElMessage.error(data?.message || '请求失败,请检查网络连接')
|
|
|
|
|
|
}
|
2025-10-21 16:50:33 +08:00
|
|
|
|
break
|
|
|
|
|
|
case 404:
|
|
|
|
|
|
ElMessage.error('请求的资源不存在')
|
|
|
|
|
|
break
|
|
|
|
|
|
case 500:
|
|
|
|
|
|
ElMessage.error('服务器内部错误')
|
|
|
|
|
|
break
|
|
|
|
|
|
default:
|
2025-10-27 10:46:49 +08:00
|
|
|
|
ElMessage.error(data?.message || '请求失败')
|
2025-10-21 16:50:33 +08:00
|
|
|
|
}
|
2025-10-27 10:46:49 +08:00
|
|
|
|
} else if (error.request) {
|
2025-10-21 16:50:33 +08:00
|
|
|
|
ElMessage.error('网络错误,请检查网络连接')
|
2025-10-27 10:46:49 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
ElMessage.error('请求配置错误')
|
2025-10-21 16:50:33 +08:00
|
|
|
|
}
|
2025-11-13 17:01:39 +08:00
|
|
|
|
|
2025-10-21 16:50:33 +08:00
|
|
|
|
return Promise.reject(error)
|
|
|
|
|
|
}
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
export default api
|