import axios from 'axios' import { isForbidden, isUnauthorized, loginRedirectTarget, shouldRedirectUnauthorized } from './envelope' export { unwrap } from './envelope' export const http = axios.create({ baseURL: '/', withCredentials: true, timeout: 30_000, }) function redirectToLogin(requestUrl?: string): void { if (typeof window === 'undefined') return // 已在登录页或失败请求即登录端点时不再跳转,避免登录页循环与吞掉登录失败反馈。 if (!shouldRedirectUnauthorized(window.location.pathname, requestUrl)) return const target = loginRedirectTarget(window.location) window.location.assign('/admin-vue/login?redirect=' + target) } http.interceptors.response.use( (response) => { // Java 未登录以 HTTP 200 + body.code=401 返回,需统一按未登录处理; // 403(已登录但无后台权限,如用工具前端账号 token 访问后台)与 401 同样跳登录页。 if (isUnauthorized(response.data) || isForbidden(response.data)) redirectToLogin(response.config?.url) return response }, (error) => { if ( error?.response?.status === 401 || error?.response?.status === 403 || isUnauthorized(error?.response?.data) || isForbidden(error?.response?.data) ) { redirectToLogin(error?.config?.url) } return Promise.reject(error) }, )