|
|
@@ -13,6 +13,9 @@ interface IResponse<T> {
|
|
|
message: string;
|
|
|
}
|
|
|
|
|
|
+// token 过期时的登出防抖:并发多个请求同时过期时只触发一次清理 + 跳转
|
|
|
+let _isLoggingOut = false;
|
|
|
+
|
|
|
class Http {
|
|
|
private baseUrl: string;
|
|
|
private header?: any;
|
|
|
@@ -58,50 +61,35 @@ class Http {
|
|
|
method,
|
|
|
data,
|
|
|
header,
|
|
|
+ timeout: 15000,
|
|
|
})
|
|
|
.then((res) => {
|
|
|
- // console.log(res)
|
|
|
- // 刷新token 已废弃
|
|
|
- // if (res.code === 888) {
|
|
|
- // // eslint-disable-next-line promise/no-nesting
|
|
|
- // return WxRequest<IResponse<R>>({
|
|
|
- // url: `${url}${query ? (urlHasParams ? "&" : "?") : ""}${query}`,
|
|
|
- // method,
|
|
|
- // data,
|
|
|
- // header: {
|
|
|
- // ...header,
|
|
|
- // Authorization: getApp<any>().globalData.token || "",
|
|
|
- // },
|
|
|
- // });
|
|
|
- // }
|
|
|
+ // 401 类响应码:登录态过期。并发场景下只执行一次清理和跳转
|
|
|
if ([21005, 21000, 10001].includes(Number(res.code))) {
|
|
|
- const hasToken = getApp<any>().globalData.token;
|
|
|
- clearToken();
|
|
|
- if (hasToken) {
|
|
|
+ const shouldRelaunch = !!getApp<any>().globalData.token && !_isLoggingOut;
|
|
|
+ if (shouldRelaunch) {
|
|
|
+ _isLoggingOut = true;
|
|
|
+ clearToken();
|
|
|
setTimeout(() => {
|
|
|
uni.reLaunch({
|
|
|
url: "/pages/map/map",
|
|
|
});
|
|
|
+ _isLoggingOut = false;
|
|
|
}, 1500);
|
|
|
}
|
|
|
throw {
|
|
|
- errMsg: hasToken ? "请重新登录" : "",
|
|
|
- _suppressToast: !hasToken,
|
|
|
+ errMsg: shouldRelaunch ? "登录已过期,请重新登录" : "",
|
|
|
+ _suppressToast: !shouldRelaunch,
|
|
|
};
|
|
|
}
|
|
|
return res;
|
|
|
})
|
|
|
.then((res) => {
|
|
|
- // eslint-disable-next-line no-console
|
|
|
- // console.log("接口返回", res);
|
|
|
- const { message = '' } = res || {
|
|
|
- msg: "出现错误",
|
|
|
- };
|
|
|
- if (res && message === "ok") {
|
|
|
+ if (res && (res.message === "ok" || Number(res.code) === 200)) {
|
|
|
resolve(res.data);
|
|
|
} else {
|
|
|
throw {
|
|
|
- errMsg: message,
|
|
|
+ errMsg: (res && (res.message || res.msg)) || "出现错误",
|
|
|
};
|
|
|
}
|
|
|
})
|
|
|
@@ -141,7 +129,6 @@ export function WxRequest<T = any>(
|
|
|
uni.request({
|
|
|
...option,
|
|
|
success(res: any) {
|
|
|
- // console.log('微信返回', res)
|
|
|
const { statusCode, header } = res;
|
|
|
// if (header["Authorization"]) {
|
|
|
// setToken(header["Authorization"]);
|
|
|
@@ -152,9 +139,11 @@ export function WxRequest<T = any>(
|
|
|
// return;
|
|
|
// }
|
|
|
if (statusCode > 200) {
|
|
|
+ console.error("请求失败", option.url, statusCode);
|
|
|
reject({
|
|
|
- errMsg: `${option.url}:${statusCode}`,
|
|
|
+ errMsg: "网络请求失败,请稍后再试",
|
|
|
});
|
|
|
+ return;
|
|
|
}
|
|
|
resolve(res.data);
|
|
|
},
|
|
|
@@ -164,5 +153,3 @@ export function WxRequest<T = any>(
|
|
|
}
|
|
|
|
|
|
export default Http;
|
|
|
-
|
|
|
-export const http = new Http();
|