2025-03-31 18:24:37 +08:00
|
|
|
import axios, { AxiosInstance, AxiosError, AxiosResponse, InternalAxiosRequestConfig } from 'axios';
|
2025-04-03 18:21:17 +08:00
|
|
|
import { ElMessage } from "element-plus";
|
|
|
|
import { useCommonStore } from "@/store/common";
|
|
|
|
import router from "@/router/index";
|
2025-03-31 18:24:37 +08:00
|
|
|
|
|
|
|
const service: AxiosInstance = axios.create({
|
2025-04-03 18:21:17 +08:00
|
|
|
baseURL: import.meta.env.VITE_APP_URL,
|
|
|
|
timeout: 5000
|
2025-03-31 18:24:37 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
service.interceptors.request.use(
|
2025-04-03 18:21:17 +08:00
|
|
|
(config: InternalAxiosRequestConfig) => {
|
|
|
|
const comm = useCommonStore();
|
|
|
|
config.headers['Access-Token'] = comm.user.token;
|
|
|
|
return config;
|
|
|
|
},
|
|
|
|
(error: AxiosError) => {
|
|
|
|
return Promise.reject();
|
|
|
|
}
|
2025-03-31 18:24:37 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
service.interceptors.response.use(
|
2025-04-03 18:21:17 +08:00
|
|
|
(response: AxiosResponse) => {
|
2025-04-07 18:35:54 +08:00
|
|
|
const { data, headers } = response;
|
2025-04-12 18:35:54 +08:00
|
|
|
if (headers['content-type'] === "application/vnd.ms-excel;charset=utf-8") return data
|
|
|
|
|
2025-04-08 18:31:37 +08:00
|
|
|
if (data.code !== 200) {
|
|
|
|
ElMessage.error(data.msg)
|
2025-04-03 18:21:17 +08:00
|
|
|
|
2025-04-08 18:31:37 +08:00
|
|
|
if (data.code === 1003) {
|
|
|
|
const comm = useCommonStore();
|
|
|
|
comm.clearStore()
|
|
|
|
router.replace('/login');
|
2025-04-03 18:21:17 +08:00
|
|
|
}
|
2025-04-08 18:31:37 +08:00
|
|
|
return Promise.reject(data);
|
|
|
|
} else {
|
|
|
|
return data.data
|
2025-03-31 18:24:37 +08:00
|
|
|
}
|
2025-04-08 18:31:37 +08:00
|
|
|
|
2025-04-03 18:21:17 +08:00
|
|
|
},
|
|
|
|
(error: AxiosError) => {
|
|
|
|
return Promise.reject();
|
|
|
|
}
|
2025-03-31 18:24:37 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
export default service;
|