canteen_system/src/axios/index.ts

120 lines
3.3 KiB
TypeScript
Raw Normal View History

2025-04-25 09:17:25 +08:00
import axios, {
AxiosError,
2025-04-28 14:13:49 +08:00
type AxiosInstance,
type AxiosRequestConfig,
type AxiosResponse,
2025-04-25 09:17:25 +08:00
} from "axios";
2025-04-28 14:13:49 +08:00
import router from "@/router";
import Loading from "@/shared/classes/Loading";
import { message } from "@/utils";
import { CLIENT_TYPE, LOGIN_ROUTER } from "@/config/constant";
import { useUserStore } from "@/stores/user";
2025-04-25 09:17:25 +08:00
const axiosConfig: AxiosRequestConfig = {
baseURL: import.meta.env.VITE_APP_BASE_API,
2025-04-28 14:13:49 +08:00
timeout:
import.meta.env.VITE_APP_ENV === "production" ? 10 * 1000 : 60 * 1000,
2025-04-25 09:17:25 +08:00
timeoutErrorMessage: "请求超时......",
};
class RequestHttp {
service: AxiosInstance;
public constructor(config: AxiosRequestConfig) {
this.service = axios.create(config);
//1.添加请求拦截
this.service.interceptors.request.use(
2025-04-28 14:13:49 +08:00
(config) => {
const userStore = useUserStore();
//请求必须带上当前客户端类型
2025-04-25 09:17:25 +08:00
config.headers.set("Client-Type", CLIENT_TYPE);
2025-04-28 14:13:49 +08:00
config.headers.set(
userStore.tokenInfo?.tokenName,
userStore.tokenInfo?.tokenValue
);
if (config.loading) {
Loading.show(config.loadingMessage);
}
//todo 默认带上用户token
2025-04-25 09:17:25 +08:00
return config;
},
2025-04-28 14:13:49 +08:00
async (error: AxiosError): Promise<string> => {
Loading.close();
message.error(error.message);
2025-04-25 09:17:25 +08:00
return Promise.reject(error);
}
);
//2.添加响应拦截
this.service.interceptors.response.use(
2025-04-28 14:13:49 +08:00
async (response: AxiosResponse): Promise<any> => {
Loading.close();
2025-04-25 09:17:25 +08:00
const jsonResult: JsonResult<unknown> = response.data;
2025-04-28 14:13:49 +08:00
if (jsonResult && jsonResult.code !== 200) {
//todo 一些特定的错误需要重新登录
if ([-1].includes(jsonResult.code)) {
//清除登录信息
useUserStore().resetUserInfo();
//跳转登录页
await router.push({
path: LOGIN_ROUTER.path,
});
}
message.error(jsonResult.message);
2025-04-25 09:17:25 +08:00
return Promise.reject(jsonResult);
}
return Promise.resolve(jsonResult);
},
2025-04-28 14:13:49 +08:00
async (error: AxiosError): Promise<string> => {
Loading.close();
message.error(error.message);
2025-04-25 09:17:25 +08:00
return Promise.reject(error);
}
);
}
/**
*
*/
get<T>(
url: string,
params?: object,
_object: AxiosRequestConfig = {}
): Promise<JsonResult<T>> {
return this.service.get(url, { params, ..._object });
}
post<T>(
url: string,
params?: object | object[],
_object: AxiosRequestConfig = {}
): Promise<JsonResult<T>> {
return this.service.post(url, params, _object);
}
put<T>(
url: string,
params?: object | object[],
_object: AxiosRequestConfig = {}
): Promise<JsonResult<T>> {
return this.service.put(url, params, _object);
}
delete<T>(
url: string,
params?: object,
_object: AxiosRequestConfig = {}
): Promise<JsonResult<T>> {
return this.service.delete(url, { params, ..._object });
}
download(
url: string,
params?: object,
_object: AxiosRequestConfig = {}
): Promise<BlobPart> {
return this.service.post(url, params, { ..._object, responseType: "blob" });
}
}
const api = new RequestHttp(axiosConfig);
export default api;