61 lines
1.5 KiB
TypeScript
61 lines
1.5 KiB
TypeScript
import axios, { InternalAxiosRequestConfig, AxiosResponse } from "axios";
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
// 创建 axios 实例
|
|
const service = axios.create({
|
|
baseURL: import.meta.env.VITE_APP_BASE_API,
|
|
timeout: 50000,
|
|
headers: {
|
|
"Request-ID":uuidv4(),
|
|
"Workspace":import.meta.env.VITE_Workspace,
|
|
"Content-Type": "application/json;charset=utf-8"
|
|
},
|
|
});
|
|
|
|
// 请求拦截器
|
|
service.interceptors.request.use(
|
|
(config: InternalAxiosRequestConfig) => {
|
|
const accessToken = localStorage.getItem("token");
|
|
if (accessToken) {
|
|
config.headers.Authorization = accessToken;
|
|
}
|
|
return config;
|
|
},
|
|
(error: any) => {
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
|
|
// 响应拦截器
|
|
service.interceptors.response.use(
|
|
(response: AxiosResponse) => {
|
|
const { code, msg } = response.data;
|
|
if (code === "00000") {
|
|
return response.data;
|
|
}
|
|
// 响应数据为二进制流处理(Excel导出)
|
|
if (response.data instanceof ArrayBuffer) {
|
|
return response;
|
|
}
|
|
|
|
// ElMessage.error(msg || "系统出错");
|
|
return Promise.reject(new Error(msg || "Error"));
|
|
},
|
|
(error: any) => {
|
|
if (error.response.data) {
|
|
const { code, msg } = error.response.data;
|
|
// token 过期,重新登录
|
|
if (code === "A0230") {
|
|
return Promise.reject(new Error(msg || "Error"));
|
|
} else {
|
|
// ElMessage.error(msg || "系统出错")
|
|
return Promise.reject(new Error(msg || "Error"));
|
|
}
|
|
}
|
|
return Promise.reject(error.message);
|
|
}
|
|
);
|
|
|
|
// 导出 axios 实例
|
|
export default service;
|