This commit is contained in:
六如
2024-10-10 11:35:34 +08:00
parent d9fb25dc0a
commit e23911b075
193 changed files with 2435 additions and 1008 deletions

View File

@@ -13,19 +13,23 @@ import { stringify } from "qs";
import NProgress from "../progress";
import { getToken, formatToken } from "@/utils/auth";
import { useUserStoreHook } from "@/store/modules/user";
const PREFIX_MOCK: string = "mock";
import { ElMessage } from "element-plus";
export const baseUrl = (url: string) => {
if (url.startsWith("/")) {
url = url.substring(1);
}
if (url.startsWith(PREFIX_MOCK)) {
return url.substring(PREFIX_MOCK.length);
}
return `/api/${url}`;
};
export const createUrl = (data: object) => {
const ret = {};
for (const dataKey in data) {
ret[dataKey] = baseUrl(data[dataKey]);
}
return ret;
};
// 相关配置请参考www.axios-js.com/zh-cn/docs/#axios-request-config-1
const defaultConfig: AxiosRequestConfig = {
// 请求超时时间
@@ -45,6 +49,13 @@ class PureHttp {
constructor() {
this.httpInterceptorsRequest();
this.httpInterceptorsResponse();
PureHttp.initConfig.beforeResponseCallback = response => {
const resp = response.data || {};
if (resp.code && resp.code !== "0") {
console.log(resp);
ElMessage.error(resp.msg || "后台出错,请查看日志");
}
};
}
/** `token`过期后,暂存待执行的请求 */
@@ -166,7 +177,7 @@ class PureHttp {
): Promise<T> {
const config = {
method,
url: baseUrl(url),
url: url,
...param,
...axiosConfig
} as PureHttpRequestConfig;