Files
admin/src/api/client.ts
lafay 740bfc76fa
All checks were successful
Admin CI / build-and-push-web (push) Successful in 1m52s
build(config): update API endpoint to production URL and remove dev proxy
Changes the default API base URL from local development server (127.0.0.1:8080) to the production endpoint (bbs.littlelan.cn) and removes the Vite dev server proxy configuration, as the application now connects directly to the production API.
2026-04-04 00:31:53 +08:00

40 lines
946 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import axios from 'axios'
import { useAuthStore } from '@/stores/authStore'
const apiClient = axios.create({
baseURL: import.meta.env.VITE_API_BASE_URL || 'https://bbs.littlelan.cn/api/v1',
timeout: 10000,
headers: {
'Content-Type': 'application/json',
},
})
// 请求拦截器自动添加Token
apiClient.interceptors.request.use(
(config) => {
const { accessToken } = useAuthStore.getState()
if (accessToken) {
config.headers.Authorization = `Bearer ${accessToken}`
}
return config
},
(error) => {
return Promise.reject(error)
}
)
// 响应拦截器处理401错误
apiClient.interceptors.response.use(
(response) => response,
(error) => {
if (error.response?.status === 401) {
// Token过期清除认证状态并跳转登录
useAuthStore.getState().logout()
window.location.href = '/login'
}
return Promise.reject(error)
}
)
export default apiClient