第一次提交
This commit is contained in:
118
frontend/src/api/index.ts
Normal file
118
frontend/src/api/index.ts
Normal file
@@ -0,0 +1,118 @@
|
||||
// API 接口封装
|
||||
import axios from 'axios'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import router from '@/router'
|
||||
|
||||
// 获取API基础URL
|
||||
const getApiBaseURL = () => {
|
||||
// 如果在浏览器中,优先根据当前主机自动推断(避免从公网访问localhost的问题)
|
||||
if (typeof window !== 'undefined') {
|
||||
const hostname = window.location.hostname
|
||||
const protocol = window.location.protocol
|
||||
|
||||
// 如果是localhost或127.0.0.1,使用localhost:8037
|
||||
if (hostname === 'localhost' || hostname === '127.0.0.1') {
|
||||
const apiUrl = 'http://localhost:8037'
|
||||
console.log('[API] 使用本地API地址:', apiUrl)
|
||||
return apiUrl
|
||||
}
|
||||
|
||||
// 对于公网IP,必须使用相同的IP地址,不能使用localhost
|
||||
// 使用相同主机名,端口8037
|
||||
const apiUrl = `${protocol}//${hostname}:8037`
|
||||
console.log('[API] 自动检测API地址:', apiUrl, '(当前主机:', hostname, ')')
|
||||
return apiUrl
|
||||
}
|
||||
|
||||
// 如果不在浏览器中(SSR等),使用环境变量或默认值
|
||||
if (import.meta.env.VITE_API_URL) {
|
||||
return import.meta.env.VITE_API_URL
|
||||
}
|
||||
|
||||
// 默认值
|
||||
return 'http://localhost:8037'
|
||||
}
|
||||
|
||||
const api = axios.create({
|
||||
baseURL: getApiBaseURL(),
|
||||
timeout: 30000
|
||||
})
|
||||
|
||||
// 请求拦截器
|
||||
api.interceptors.request.use(
|
||||
(config) => {
|
||||
const token = localStorage.getItem('token')
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`
|
||||
}
|
||||
return config
|
||||
},
|
||||
(error) => {
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
// 响应拦截器
|
||||
api.interceptors.response.use(
|
||||
(response) => {
|
||||
return response
|
||||
},
|
||||
(error) => {
|
||||
const response = error.response
|
||||
const status = response?.status
|
||||
const data = response?.data
|
||||
|
||||
// 处理401未授权
|
||||
if (status === 401) {
|
||||
localStorage.removeItem('token')
|
||||
router.push('/login')
|
||||
ElMessage.error(data?.message || '登录已过期,请重新登录')
|
||||
return Promise.reject(error)
|
||||
}
|
||||
|
||||
// 处理403禁止访问
|
||||
if (status === 403) {
|
||||
ElMessage.error(data?.message || '无权访问此资源')
|
||||
return Promise.reject(error)
|
||||
}
|
||||
|
||||
// 处理404未找到
|
||||
if (status === 404) {
|
||||
ElMessage.error(data?.message || '请求的资源不存在')
|
||||
return Promise.reject(error)
|
||||
}
|
||||
|
||||
// 处理422验证错误
|
||||
if (status === 422) {
|
||||
const details = data?.details || []
|
||||
if (details.length > 0) {
|
||||
const firstError = details[0]
|
||||
ElMessage.error(`${firstError.field}: ${firstError.message}`)
|
||||
} else {
|
||||
ElMessage.error(data?.message || '请求参数验证失败')
|
||||
}
|
||||
return Promise.reject(error)
|
||||
}
|
||||
|
||||
// 处理500服务器错误
|
||||
if (status === 500) {
|
||||
const message = data?.message || '服务器内部错误,请稍后重试'
|
||||
ElMessage.error(message)
|
||||
console.error('服务器错误:', data)
|
||||
return Promise.reject(error)
|
||||
}
|
||||
|
||||
// 处理网络错误
|
||||
if (!response) {
|
||||
ElMessage.error('网络错误,请检查网络连接')
|
||||
return Promise.reject(error)
|
||||
}
|
||||
|
||||
// 其他错误
|
||||
const message = data?.message || error.message || '请求失败'
|
||||
ElMessage.error(message)
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
export default api
|
||||
Reference in New Issue
Block a user