fix
This commit is contained in:
parent
aeb5bd5c1b
commit
356ca908fa
1
src/api/login.js
Normal file
1
src/api/login.js
Normal file
@ -0,0 +1 @@
|
|||||||
|
import request from "@/utils/service/index.js";
|
@ -3,7 +3,11 @@
|
|||||||
{
|
{
|
||||||
"path": "pages/index/index",
|
"path": "pages/index/index",
|
||||||
"style": {
|
"style": {
|
||||||
"navigationBarTitleText": "uni-app"
|
"navigationBarTitleText": "",
|
||||||
|
"enablePullDownRefresh": false,
|
||||||
|
"app-plus": {
|
||||||
|
"titleNView": false // 禁用原生导航
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
56
utils/service/index.js
Normal file
56
utils/service/index.js
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
import Request from "./request/index.js";
|
||||||
|
import useToast from "@/hooks/toast/useToast.js";
|
||||||
|
const { showMessage } = useToast();
|
||||||
|
|
||||||
|
const request = new Request({
|
||||||
|
baseURL: " http://114.218.158.24:9020/",
|
||||||
|
timeout: 1000 * 60 * 5,
|
||||||
|
interceptors: {
|
||||||
|
//实例的请求拦截器
|
||||||
|
requestInterceptors: (config) => {
|
||||||
|
config.headers["Content-Type"] =
|
||||||
|
config.method === "get"
|
||||||
|
? "application/x-www-form-urlencoded"
|
||||||
|
: "application/json";
|
||||||
|
|
||||||
|
const token = uni.getStorageSync("token") || "";
|
||||||
|
if (config.isFormData) {
|
||||||
|
config.headers["Content-Type"] = "multipart/form-data";
|
||||||
|
config.headers["Authorization"] = token;
|
||||||
|
} else {
|
||||||
|
config.headers["Authorization"] = token;
|
||||||
|
}
|
||||||
|
return config;
|
||||||
|
},
|
||||||
|
//实例的响应拦截器
|
||||||
|
responseInterceptors: async (response) => {
|
||||||
|
if (response.data.status === 1) {
|
||||||
|
showMessage({ type: "error", message: response.data.msg });
|
||||||
|
}
|
||||||
|
if (response.data.code === 401) {
|
||||||
|
uni.navigateTo({
|
||||||
|
url: "/pages/login/index",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if ([200, 201, 204].includes(response.status)) {
|
||||||
|
return response.config.responseType === "blob" ? response : response;
|
||||||
|
} else {
|
||||||
|
showMessage({ type: "error", message: response.data.msg });
|
||||||
|
return Promise.reject(
|
||||||
|
new Error(response.data.msg || "An error occurred.")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const fontRequest = (config) => {
|
||||||
|
console.log(import.meta);
|
||||||
|
|
||||||
|
if (["get", "GET"].includes(config.method)) {
|
||||||
|
config.params = config.data;
|
||||||
|
}
|
||||||
|
return request.request(config);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default fontRequest;
|
87
utils/service/request/index.js
Normal file
87
utils/service/request/index.js
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
import axios from "axios";
|
||||||
|
class Request {
|
||||||
|
// axios 实例
|
||||||
|
instance;
|
||||||
|
// 拦截器对象
|
||||||
|
interceptorsObj;
|
||||||
|
// * 存放取消请求控制器Map
|
||||||
|
abortControllerMap;
|
||||||
|
constructor(config) {
|
||||||
|
this.instance = axios.create(config);
|
||||||
|
// * 初始化存放取消请求控制器Map
|
||||||
|
this.abortControllerMap = new Map();
|
||||||
|
this.interceptorsObj = config.interceptors;
|
||||||
|
// 拦截器执行顺序 接口请求 -> 实例请求 -> 全局请求 -> 实例响应 -> 全局响应 -> 接口响应
|
||||||
|
this.instance.interceptors.request.use(
|
||||||
|
(res) => {
|
||||||
|
const controller = new AbortController();
|
||||||
|
const url = res.url || "";
|
||||||
|
res.signal = controller.signal;
|
||||||
|
this.abortControllerMap.set(url, controller);
|
||||||
|
return res;
|
||||||
|
},
|
||||||
|
(err) => err
|
||||||
|
);
|
||||||
|
// 使用实例拦截器
|
||||||
|
this.instance.interceptors.request.use(
|
||||||
|
this.interceptorsObj?.requestInterceptors,
|
||||||
|
this.interceptorsObj?.requestInterceptorsCatch
|
||||||
|
);
|
||||||
|
this.instance.interceptors.response.use(
|
||||||
|
this.interceptorsObj?.responseInterceptors,
|
||||||
|
this.interceptorsObj?.responseInterceptorsCatch
|
||||||
|
);
|
||||||
|
// 全局响应拦截器保证最后执行
|
||||||
|
this.instance.interceptors.response.use(
|
||||||
|
// 因为我们接口的数据都在res.data下,所以我们直接返回res.data
|
||||||
|
(res) => {
|
||||||
|
const url = res.config.url || "";
|
||||||
|
this.abortControllerMap.delete(url);
|
||||||
|
return res.data;
|
||||||
|
},
|
||||||
|
(err) => err
|
||||||
|
);
|
||||||
|
}
|
||||||
|
request(config) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
// 如果我们为单个请求设置拦截器,这里使用单个请求的拦截器
|
||||||
|
if (config.interceptors?.requestInterceptors) {
|
||||||
|
config = config.interceptors.requestInterceptors(config);
|
||||||
|
}
|
||||||
|
this.instance
|
||||||
|
.request(config)
|
||||||
|
.then((res) => {
|
||||||
|
// 如果我们为单个响应设置拦截器,这里使用单个响应的拦截器
|
||||||
|
if (config.interceptors?.responseInterceptors) {
|
||||||
|
res = config.interceptors.responseInterceptors(res);
|
||||||
|
}
|
||||||
|
resolve(res);
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
// .finally(() => {})
|
||||||
|
});
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 取消全部请求
|
||||||
|
*/
|
||||||
|
cancelAllRequest() {
|
||||||
|
for (const [, controller] of this.abortControllerMap) {
|
||||||
|
controller.abort();
|
||||||
|
}
|
||||||
|
this.abortControllerMap.clear();
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 取消指定的请求
|
||||||
|
* @param url 待取消的请求URL
|
||||||
|
*/
|
||||||
|
cancelRequest(url) {
|
||||||
|
const urlList = Array.isArray(url) ? url : [url];
|
||||||
|
for (const _url of urlList) {
|
||||||
|
this.abortControllerMap.get(_url)?.abort();
|
||||||
|
this.abortControllerMap.delete(_url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export default Request;
|
Loading…
Reference in New Issue
Block a user