chore: initial baseline with P0-safety .gitignore
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
// =============================================================================
|
||||
// IT智能服务台 — Portal API 层
|
||||
// =============================================================================
|
||||
// 说明:封装所有与后端 API 的交互
|
||||
// =============================================================================
|
||||
|
||||
import axios from 'axios'
|
||||
|
||||
// 创建 axios 实例
|
||||
const apiClient = axios.create({
|
||||
baseURL: '/api',
|
||||
timeout: 10000,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
})
|
||||
|
||||
// 请求拦截器:添加 Token
|
||||
apiClient.interceptors.request.use(
|
||||
(config) => {
|
||||
// 从 localStorage 获取 Token
|
||||
const token = localStorage.getItem('portal_token')
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`
|
||||
}
|
||||
return config
|
||||
},
|
||||
(error) => {
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
// 响应拦截器:处理错误
|
||||
apiClient.interceptors.response.use(
|
||||
(response) => {
|
||||
return response
|
||||
},
|
||||
(error) => {
|
||||
// 401 错误:Token 过期或无效
|
||||
if (error.response?.status === 401) {
|
||||
// 清除本地存储的 Token
|
||||
localStorage.removeItem('portal_token')
|
||||
localStorage.removeItem('portal_user')
|
||||
// 跳转到选择页
|
||||
window.location.href = '/itportal/select'
|
||||
}
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
export default apiClient
|
||||
Reference in New Issue
Block a user