2021-12-06 17:11:15 +08:00
|
|
|
|
import Axios, { AxiosInstance, AxiosRequestConfig } from "axios";
|
|
|
|
|
|
import {
|
|
|
|
|
|
PureHttpError,
|
|
|
|
|
|
RequestMethods,
|
2022-03-03 23:30:08 +08:00
|
|
|
|
PureHttpResponse,
|
2021-12-06 17:11:15 +08:00
|
|
|
|
PureHttpRequestConfig
|
|
|
|
|
|
} from "./types.d";
|
|
|
|
|
|
import qs from "qs";
|
|
|
|
|
|
import NProgress from "../progress";
|
|
|
|
|
|
// import { loadEnv } from "@build/index";
|
|
|
|
|
|
import { getToken } from "/@/utils/auth";
|
|
|
|
|
|
import { useUserStoreHook } from "/@/store/modules/user";
|
|
|
|
|
|
|
|
|
|
|
|
// 加载环境变量 VITE_PROXY_DOMAIN(开发环境) VITE_PROXY_DOMAIN_REAL(打包后的线上环境)
|
|
|
|
|
|
// const { VITE_PROXY_DOMAIN, VITE_PROXY_DOMAIN_REAL } = loadEnv();
|
|
|
|
|
|
|
|
|
|
|
|
// 相关配置请参考:www.axios-js.com/zh-cn/docs/#axios-request-config-1
|
|
|
|
|
|
const defaultConfig: AxiosRequestConfig = {
|
|
|
|
|
|
// baseURL:
|
|
|
|
|
|
// process.env.NODE_ENV === "production"
|
|
|
|
|
|
// ? VITE_PROXY_DOMAIN_REAL
|
|
|
|
|
|
// : VITE_PROXY_DOMAIN,
|
2022-10-25 17:51:21 +08:00
|
|
|
|
// 当前使用mock模拟请求,将baseURL制空,如果你的环境用到了http请求,请删除下面的baseURL启用上面的baseURL,并将第10行、15行代码注释取消
|
2021-12-06 17:11:15 +08:00
|
|
|
|
baseURL: "",
|
|
|
|
|
|
timeout: 10000,
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
Accept: "application/json, text/plain, */*",
|
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
|
"X-Requested-With": "XMLHttpRequest"
|
|
|
|
|
|
},
|
|
|
|
|
|
// 数组格式参数序列化
|
|
|
|
|
|
paramsSerializer: params => qs.stringify(params, { indices: false })
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class PureHttp {
|
|
|
|
|
|
constructor() {
|
|
|
|
|
|
this.httpInterceptorsRequest();
|
|
|
|
|
|
this.httpInterceptorsResponse();
|
|
|
|
|
|
}
|
2022-09-11 16:46:21 +08:00
|
|
|
|
/** 初始化配置对象 */
|
2021-12-06 17:11:15 +08:00
|
|
|
|
private static initConfig: PureHttpRequestConfig = {};
|
|
|
|
|
|
|
2022-09-11 16:46:21 +08:00
|
|
|
|
/** 保存当前Axios实例对象 */
|
2021-12-06 17:11:15 +08:00
|
|
|
|
private static axiosInstance: AxiosInstance = Axios.create(defaultConfig);
|
|
|
|
|
|
|
2022-09-11 16:46:21 +08:00
|
|
|
|
/** 请求拦截 */
|
2021-12-06 17:11:15 +08:00
|
|
|
|
private httpInterceptorsRequest(): void {
|
|
|
|
|
|
PureHttp.axiosInstance.interceptors.request.use(
|
2022-10-25 17:51:21 +08:00
|
|
|
|
async (config: PureHttpRequestConfig) => {
|
2021-12-06 17:11:15 +08:00
|
|
|
|
const $config = config;
|
|
|
|
|
|
// 开启进度条动画
|
|
|
|
|
|
NProgress.start();
|
|
|
|
|
|
// 优先判断post/get等方法是否传入回掉,否则执行初始化设置等回掉
|
|
|
|
|
|
if (typeof config.beforeRequestCallback === "function") {
|
|
|
|
|
|
config.beforeRequestCallback($config);
|
|
|
|
|
|
return $config;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (PureHttp.initConfig.beforeRequestCallback) {
|
|
|
|
|
|
PureHttp.initConfig.beforeRequestCallback($config);
|
|
|
|
|
|
return $config;
|
|
|
|
|
|
}
|
2022-10-25 17:51:21 +08:00
|
|
|
|
/** 请求白名单,放置一些不需要token的接口(通过设置请求白名单,防止token过期后再请求造成的死循环问题) */
|
|
|
|
|
|
const whiteList = ["/refreshToken", "/login"];
|
|
|
|
|
|
return whiteList.some(v => config.url.indexOf(v) > -1)
|
|
|
|
|
|
? config
|
|
|
|
|
|
: new Promise(resolve => {
|
|
|
|
|
|
const data = getToken();
|
|
|
|
|
|
if (data) {
|
|
|
|
|
|
const now = new Date().getTime();
|
|
|
|
|
|
const expired = parseInt(data.expires) - now <= 0;
|
|
|
|
|
|
if (expired) {
|
|
|
|
|
|
// token过期刷新
|
|
|
|
|
|
useUserStoreHook()
|
|
|
|
|
|
.handRefreshToken({ refreshToken: data.refreshToken })
|
|
|
|
|
|
.then(res => {
|
|
|
|
|
|
config.headers["Authorization"] =
|
|
|
|
|
|
"Bearer " + res.data.accessToken;
|
|
|
|
|
|
resolve($config);
|
|
|
|
|
|
});
|
|
|
|
|
|
} else {
|
|
|
|
|
|
config.headers["Authorization"] =
|
|
|
|
|
|
"Bearer " + data.accessToken;
|
|
|
|
|
|
resolve($config);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
resolve($config);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2021-12-06 17:11:15 +08:00
|
|
|
|
},
|
|
|
|
|
|
error => {
|
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-11 16:46:21 +08:00
|
|
|
|
/** 响应拦截 */
|
2021-12-06 17:11:15 +08:00
|
|
|
|
private httpInterceptorsResponse(): void {
|
|
|
|
|
|
const instance = PureHttp.axiosInstance;
|
|
|
|
|
|
instance.interceptors.response.use(
|
2022-03-03 23:30:08 +08:00
|
|
|
|
(response: PureHttpResponse) => {
|
2021-12-06 17:11:15 +08:00
|
|
|
|
const $config = response.config;
|
|
|
|
|
|
// 关闭进度条动画
|
|
|
|
|
|
NProgress.done();
|
|
|
|
|
|
// 优先判断post/get等方法是否传入回掉,否则执行初始化设置等回掉
|
|
|
|
|
|
if (typeof $config.beforeResponseCallback === "function") {
|
|
|
|
|
|
$config.beforeResponseCallback(response);
|
|
|
|
|
|
return response.data;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (PureHttp.initConfig.beforeResponseCallback) {
|
|
|
|
|
|
PureHttp.initConfig.beforeResponseCallback(response);
|
|
|
|
|
|
return response.data;
|
|
|
|
|
|
}
|
|
|
|
|
|
return response.data;
|
|
|
|
|
|
},
|
|
|
|
|
|
(error: PureHttpError) => {
|
|
|
|
|
|
const $error = error;
|
|
|
|
|
|
$error.isCancelRequest = Axios.isCancel($error);
|
|
|
|
|
|
// 关闭进度条动画
|
|
|
|
|
|
NProgress.done();
|
|
|
|
|
|
// 所有的响应异常 区分来源为取消请求/非取消请求
|
|
|
|
|
|
return Promise.reject($error);
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-11 16:46:21 +08:00
|
|
|
|
/** 通用请求工具函数 */
|
2021-12-06 17:11:15 +08:00
|
|
|
|
public request<T>(
|
|
|
|
|
|
method: RequestMethods,
|
|
|
|
|
|
url: string,
|
|
|
|
|
|
param?: AxiosRequestConfig,
|
|
|
|
|
|
axiosConfig?: PureHttpRequestConfig
|
|
|
|
|
|
): Promise<T> {
|
|
|
|
|
|
const config = {
|
|
|
|
|
|
method,
|
|
|
|
|
|
url,
|
|
|
|
|
|
...param,
|
|
|
|
|
|
...axiosConfig
|
|
|
|
|
|
} as PureHttpRequestConfig;
|
|
|
|
|
|
|
|
|
|
|
|
// 单独处理自定义请求/响应回掉
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
PureHttp.axiosInstance
|
|
|
|
|
|
.request(config)
|
|
|
|
|
|
.then((response: undefined) => {
|
|
|
|
|
|
resolve(response);
|
|
|
|
|
|
})
|
|
|
|
|
|
.catch(error => {
|
|
|
|
|
|
reject(error);
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-11 16:46:21 +08:00
|
|
|
|
/** 单独抽离的post工具函数 */
|
2022-02-18 14:55:04 +08:00
|
|
|
|
public post<T, P>(
|
2021-12-06 17:11:15 +08:00
|
|
|
|
url: string,
|
|
|
|
|
|
params?: T,
|
|
|
|
|
|
config?: PureHttpRequestConfig
|
2022-02-18 14:55:04 +08:00
|
|
|
|
): Promise<P> {
|
|
|
|
|
|
return this.request<P>("post", url, params, config);
|
2021-12-06 17:11:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-11 16:46:21 +08:00
|
|
|
|
/** 单独抽离的get工具函数 */
|
2022-02-18 14:55:04 +08:00
|
|
|
|
public get<T, P>(
|
2021-12-06 17:11:15 +08:00
|
|
|
|
url: string,
|
|
|
|
|
|
params?: T,
|
|
|
|
|
|
config?: PureHttpRequestConfig
|
2022-02-18 14:55:04 +08:00
|
|
|
|
): Promise<P> {
|
|
|
|
|
|
return this.request<P>("get", url, params, config);
|
2021-12-06 17:11:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const http = new PureHttp();
|