2026-04-21 22:07:29 +08:00
|
|
|
import axios from 'axios'
|
|
|
|
|
import { ElMessage } from 'element-plus'
|
|
|
|
|
import router from '@/router'
|
|
|
|
|
|
|
|
|
|
const client = axios.create({
|
|
|
|
|
baseURL: import.meta.env.VITE_API_BASE_URL ?? '/api',
|
|
|
|
|
timeout: 15000,
|
|
|
|
|
})
|
|
|
|
|
|
2026-04-28 16:08:07 +08:00
|
|
|
if (import.meta.env.DEV) {
|
|
|
|
|
client.interceptors.request.use((config) => {
|
|
|
|
|
console.debug('[tenant-platform][HTTP] request', {
|
|
|
|
|
method: config.method?.toUpperCase(),
|
|
|
|
|
url: config.baseURL ? `${config.baseURL}${config.url ?? ''}` : config.url,
|
|
|
|
|
params: config.params,
|
|
|
|
|
})
|
|
|
|
|
return config
|
|
|
|
|
})
|
|
|
|
|
client.interceptors.response.use((res) => {
|
|
|
|
|
console.debug('[tenant-platform][HTTP] response', {
|
|
|
|
|
status: res.status,
|
|
|
|
|
url: res.config.url,
|
|
|
|
|
})
|
|
|
|
|
return res
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-21 22:07:29 +08:00
|
|
|
client.interceptors.request.use((config) => {
|
|
|
|
|
const token = localStorage.getItem('token')
|
|
|
|
|
if (token) {
|
|
|
|
|
config.headers.Authorization = `Bearer ${token}`
|
|
|
|
|
}
|
|
|
|
|
return config
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
client.interceptors.response.use(
|
|
|
|
|
(res) => res,
|
|
|
|
|
(error) => {
|
|
|
|
|
if (error.response?.status === 401) {
|
|
|
|
|
localStorage.removeItem('token')
|
|
|
|
|
router.push('/login')
|
|
|
|
|
} else {
|
|
|
|
|
const msg = error.response?.data?.message ?? '请求失败'
|
|
|
|
|
ElMessage.error(msg)
|
|
|
|
|
}
|
|
|
|
|
return Promise.reject(error)
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
export default client
|