Files
offerpai_web/src/utils/request.ts
T

37 lines
948 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 type { AxiosResponse } from 'axios'
import store from '@/stores'
/**
* 创建 axios 实例
* withCredentials: true — 浏览器自动携带 Cookie(包括 HttpOnly 的 Token
*/
const service = axios.create({
baseURL: import.meta.env.VITE_API_BASE_URL,
timeout: 15000,
withCredentials: true,
})
/**
* 响应拦截器 — 统一处理错误
*/
service.interceptors.response.use(
(response: AxiosResponse) => {
return response.data
},
(error) => {
const status = error.response?.status
if (status === 401) {
// 同步重置前端登录状态,弹出登录框
store.commit('SET_AUTHENTICATED', false)
store.dispatch('openLogin', window.location.pathname)
ElMessage.error('登录已过期,请重新登录')
} else {
ElMessage.error(error.response?.data?.msg || '请求失败')
}
return Promise.reject(error)
},
)
export default service