2024-07-27 20:23:03 +08:00
|
|
|
|
import axios, { AxiosError, AxiosResponse, InternalAxiosRequestConfig } from "axios";
|
|
|
|
|
|
import { Modal, notification } from "antd";
|
2024-08-04 15:58:33 +08:00
|
|
|
|
import { useUserStore } from "@/stores";
|
2024-07-27 20:23:03 +08:00
|
|
|
|
|
|
|
|
|
|
// 配置项目API域名
|
2024-08-04 15:58:33 +08:00
|
|
|
|
export const baseURL = "http://localhost:9999/api";
|
|
|
|
|
|
// export const baseURL = "http://82.157.186.125:5000/api";
|
2024-07-27 20:23:03 +08:00
|
|
|
|
|
|
|
|
|
|
// 创建 axios 实例
|
|
|
|
|
|
export const instance = axios.create({
|
|
|
|
|
|
// 项目API根路径
|
|
|
|
|
|
baseURL,
|
|
|
|
|
|
// 请求超时的时间
|
|
|
|
|
|
timeout: 5000,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 请求拦截
|
|
|
|
|
|
instance.interceptors.request.use(
|
|
|
|
|
|
(config: InternalAxiosRequestConfig) => {
|
2024-08-08 12:36:31 +08:00
|
|
|
|
// 获取token
|
|
|
|
|
|
const token = JSON.parse(localStorage.getItem("user_storage") || "{}")?.state.token
|
2024-07-27 20:23:03 +08:00
|
|
|
|
|
|
|
|
|
|
// 如果有token就把赋值给请求头
|
|
|
|
|
|
if (token) config.headers["Authorization"] = `Bearer ${token}`;
|
|
|
|
|
|
|
|
|
|
|
|
return config;
|
|
|
|
|
|
},
|
|
|
|
|
|
(err: AxiosError) => {
|
|
|
|
|
|
notification.error({
|
2024-08-08 12:36:31 +08:00
|
|
|
|
message: '请求异常',
|
2024-07-27 20:23:03 +08:00
|
|
|
|
description: err.message,
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
return Promise.reject(err);
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// 响应拦截
|
|
|
|
|
|
instance.interceptors.response.use(
|
|
|
|
|
|
(res: AxiosResponse) => {
|
|
|
|
|
|
// 只要code不等于200, 就相当于响应失败
|
|
|
|
|
|
if (res.data?.code !== 200) {
|
|
|
|
|
|
notification.error({
|
2024-08-08 12:36:31 +08:00
|
|
|
|
message: '响应异常',
|
2024-07-27 20:23:03 +08:00
|
|
|
|
description: res.data?.message || "未知错误",
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
return Promise.reject(res.data);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return res.data;
|
|
|
|
|
|
},
|
|
|
|
|
|
(err: AxiosError) => {
|
2024-08-07 13:02:59 +08:00
|
|
|
|
// 如果code为401就证明认证失败
|
|
|
|
|
|
if (err.response?.status === 401) {
|
|
|
|
|
|
Modal.error({
|
|
|
|
|
|
title: '暂无权限',
|
2024-08-14 12:50:45 +08:00
|
|
|
|
content: '🔒️ 登录已过期,请重新登录?',
|
2024-08-07 13:02:59 +08:00
|
|
|
|
okText: "去登录",
|
|
|
|
|
|
onOk: () => {
|
2024-08-08 12:36:31 +08:00
|
|
|
|
const store = useUserStore.getState()
|
2024-08-07 13:02:59 +08:00
|
|
|
|
store.quitLogin()
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return Promise.reject(err.response?.data);
|
|
|
|
|
|
}
|
2024-08-08 12:36:31 +08:00
|
|
|
|
|
2024-07-27 20:23:03 +08:00
|
|
|
|
notification.error({
|
2024-08-08 12:36:31 +08:00
|
|
|
|
message: '程序异常',
|
2024-07-27 20:23:03 +08:00
|
|
|
|
description: err.message || "未知错误",
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
return Promise.reject(err);
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// 如果是GET传参就自动识别为query,POST为data
|
|
|
|
|
|
const Request = <T>(method: string, url: string, reqParams?: object) => {
|
|
|
|
|
|
if (!method) method = "GET";
|
|
|
|
|
|
|
|
|
|
|
|
return instance.request<any, Response<T>>({
|
|
|
|
|
|
method,
|
|
|
|
|
|
url,
|
2024-08-09 15:10:09 +08:00
|
|
|
|
[method.toLocaleUpperCase() === "GET" ? "params" : "data"]: reqParams
|
2024-07-27 20:23:03 +08:00
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export default Request;
|