2026-07-30 14:16:58 +08:00
|
|
|
|
import { Message, Modal } from '@arco-design/web-vue';
|
|
|
|
|
|
import type { AxiosResponse, InternalAxiosRequestConfig } from 'axios';
|
|
|
|
|
|
import axios from 'axios';
|
|
|
|
|
|
import { useUserStore } from '@/store';
|
|
|
|
|
|
import { getToken } from '@/utils/auth';
|
|
|
|
|
|
|
|
|
|
|
|
export interface HttpResponse<T = unknown> {
|
|
|
|
|
|
status: number;
|
|
|
|
|
|
msg: string;
|
|
|
|
|
|
code: number;
|
|
|
|
|
|
data: T;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let initialized = false;
|
2026-08-04 21:50:20 +08:00
|
|
|
|
let sessionExpiredDialogVisible = false;
|
|
|
|
|
|
|
|
|
|
|
|
function networkErrorMessage(error: unknown) {
|
|
|
|
|
|
if (!axios.isAxiosError<HttpResponse>(error)) return '网络请求失败,请稍后重试';
|
|
|
|
|
|
if (!error.response) return '无法连接服务器,请检查网络后重试';
|
|
|
|
|
|
if (error.response.status === 403) return '当前账号无权执行此操作';
|
|
|
|
|
|
return error.response.data?.msg || `请求失败(${error.response.status})`;
|
|
|
|
|
|
}
|
2026-07-30 14:16:58 +08:00
|
|
|
|
|
|
|
|
|
|
export function setupHttp() {
|
|
|
|
|
|
if (initialized) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
initialized = true;
|
|
|
|
|
|
|
|
|
|
|
|
const baseURL = import.meta.env.VITE_API_BASE_URL?.trim();
|
|
|
|
|
|
if (baseURL) {
|
|
|
|
|
|
axios.defaults.baseURL = baseURL;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
axios.interceptors.request.use(
|
|
|
|
|
|
(config: InternalAxiosRequestConfig) => {
|
|
|
|
|
|
const token = getToken();
|
|
|
|
|
|
if (token) {
|
|
|
|
|
|
config.headers.Authorization = `Bearer ${token}`;
|
|
|
|
|
|
}
|
|
|
|
|
|
return config;
|
|
|
|
|
|
},
|
|
|
|
|
|
(error) => Promise.reject(error),
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
axios.interceptors.response.use(
|
|
|
|
|
|
(response: AxiosResponse<HttpResponse>) => {
|
|
|
|
|
|
const res = response.data;
|
|
|
|
|
|
if (res.code !== 20000) {
|
|
|
|
|
|
Message.error({
|
|
|
|
|
|
content: res.msg || '请求失败',
|
|
|
|
|
|
duration: 5 * 1000,
|
|
|
|
|
|
});
|
|
|
|
|
|
if (
|
|
|
|
|
|
[50008, 50012, 50014].includes(res.code) &&
|
|
|
|
|
|
response.config.url !== '/api/user/info'
|
|
|
|
|
|
) {
|
2026-08-04 21:50:20 +08:00
|
|
|
|
if (!sessionExpiredDialogVisible) {
|
|
|
|
|
|
sessionExpiredDialogVisible = true;
|
|
|
|
|
|
Modal.error({
|
2026-07-30 14:16:58 +08:00
|
|
|
|
title: '登录已失效',
|
2026-08-04 21:50:20 +08:00
|
|
|
|
content: '当前登录状态已失效。重新登录后将返回当前页面。',
|
2026-07-30 14:16:58 +08:00
|
|
|
|
okText: '重新登录',
|
|
|
|
|
|
async onOk() {
|
|
|
|
|
|
const userStore = useUserStore();
|
2026-08-04 21:50:20 +08:00
|
|
|
|
sessionStorage.setItem('auth-redirect', `${window.location.pathname}${window.location.search}`);
|
2026-07-30 14:16:58 +08:00
|
|
|
|
await userStore.logout();
|
2026-08-04 21:50:20 +08:00
|
|
|
|
window.location.assign('/login');
|
|
|
|
|
|
},
|
|
|
|
|
|
onCancel() {
|
|
|
|
|
|
sessionExpiredDialogVisible = false;
|
2026-07-30 14:16:58 +08:00
|
|
|
|
},
|
2026-08-04 21:50:20 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
2026-07-30 14:16:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
return Promise.reject(new Error(res.msg || '请求失败'));
|
|
|
|
|
|
}
|
|
|
|
|
|
return res as unknown as AxiosResponse<HttpResponse>;
|
|
|
|
|
|
},
|
|
|
|
|
|
(error) => {
|
2026-08-04 21:50:20 +08:00
|
|
|
|
const content = networkErrorMessage(error);
|
2026-07-30 14:16:58 +08:00
|
|
|
|
Message.error({
|
2026-08-04 21:50:20 +08:00
|
|
|
|
content,
|
2026-07-30 14:16:58 +08:00
|
|
|
|
duration: 5 * 1000,
|
|
|
|
|
|
});
|
2026-08-04 21:50:20 +08:00
|
|
|
|
return Promise.reject(new Error(content));
|
2026-07-30 14:16:58 +08:00
|
|
|
|
},
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|