2025-03-14 16:27:50 +08:00
|
|
|
|
import Request from "@/lib/request/index.js";
|
2025-04-25 18:10:54 +08:00
|
|
|
|
import {
|
|
|
|
|
refreshTokenFn
|
|
|
|
|
} from "@/api/login.js";
|
2025-03-14 16:27:50 +08:00
|
|
|
|
import storage from "@/utils/storage.js";
|
2025-04-25 18:10:54 +08:00
|
|
|
|
import {
|
|
|
|
|
md5
|
|
|
|
|
} from "@/utils/md5.js";
|
2025-03-14 16:27:50 +08:00
|
|
|
|
import Foundation from "@/utils/Foundation.js";
|
|
|
|
|
import api from "@/config/api.js";
|
|
|
|
|
|
|
|
|
|
import uuid from "@/utils/uuid.modified.js";
|
|
|
|
|
|
2025-04-30 14:28:07 +08:00
|
|
|
|
import {
|
|
|
|
|
clearIm
|
|
|
|
|
} from "@/utils/handleim.js";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-03-14 16:27:50 +08:00
|
|
|
|
function cleanStorage() {
|
2025-04-25 18:10:54 +08:00
|
|
|
|
uni.showToast({
|
|
|
|
|
title: "你的登录状态已过期,请重新登录",
|
|
|
|
|
icon: "none",
|
|
|
|
|
duration: 1500,
|
|
|
|
|
});
|
|
|
|
|
if (uni.showLoading()) {
|
|
|
|
|
uni.hideLoading();
|
|
|
|
|
}
|
2025-04-21 17:39:41 +08:00
|
|
|
|
|
2025-04-30 14:28:07 +08:00
|
|
|
|
storage.setHasLogin(false);
|
|
|
|
|
storage.setAccessToken("");
|
|
|
|
|
storage.setRefreshToken("");
|
2025-05-02 17:32:03 +08:00
|
|
|
|
storage.setUuid("");
|
2025-04-30 14:28:07 +08:00
|
|
|
|
storage.setUserInfo({});
|
|
|
|
|
// 清理vlog信息
|
|
|
|
|
storage.setVlogToken("");
|
|
|
|
|
storage.setVlogUserInfo(null);
|
|
|
|
|
// 清除初始化数据内容
|
|
|
|
|
storage.setRefreshVlogIndex("1"); //需要刷新
|
|
|
|
|
// 退出im登录
|
|
|
|
|
clearIm()
|
|
|
|
|
|
|
|
|
|
// 获取当前页面栈
|
|
|
|
|
const pages = getCurrentPages();
|
|
|
|
|
const currentPage = pages[pages.length - 1];
|
|
|
|
|
const currentRoute = currentPage.route;
|
|
|
|
|
// 防抖处理跳转
|
|
|
|
|
// #ifdef MP-WEIXIN
|
|
|
|
|
if (currentRoute !== 'pages/passport/wechatMPLogin') {
|
|
|
|
|
uni.navigateTo({
|
|
|
|
|
url: "/pages/passport/wechatMPLogin"
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// #endif
|
|
|
|
|
// #ifndef MP-WEIXIN
|
|
|
|
|
if (currentRoute !== 'pages/passport/login') {
|
|
|
|
|
uni.navigateTo({
|
|
|
|
|
url: "/pages/passport/login"
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// #endif
|
2025-03-14 16:27:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let http = new Request();
|
|
|
|
|
|
|
|
|
|
http.setConfig((config) => {
|
2025-04-25 18:10:54 +08:00
|
|
|
|
// 没有uuid创建
|
|
|
|
|
if (!storage.getUuid()) {
|
2025-05-02 17:22:45 +08:00
|
|
|
|
storage.setUuid("wzj666");
|
2025-04-25 18:10:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 设置全局配置 */
|
|
|
|
|
config.baseURL = api.buyer;
|
|
|
|
|
config.header = {
|
|
|
|
|
...config.header,
|
|
|
|
|
};
|
|
|
|
|
config.validateStatus = (statusCode) => {
|
|
|
|
|
// 不论什么状态,统一在正确中处理
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
return config;
|
2025-03-14 16:27:50 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
http.interceptors.request.use(
|
2025-04-25 18:10:54 +08:00
|
|
|
|
(config) => {
|
|
|
|
|
/* 请求之前拦截器。可以使用async await 做异步操作 */
|
|
|
|
|
let accessToken = storage.getAccessToken();
|
|
|
|
|
|
|
|
|
|
if (accessToken) {
|
|
|
|
|
const nonce = Foundation.randomString(6);
|
|
|
|
|
const timestamp = parseInt(new Date().getTime() / 1000);
|
|
|
|
|
const sign = md5(nonce + timestamp + accessToken);
|
|
|
|
|
const _params = {
|
|
|
|
|
nonce,
|
|
|
|
|
timestamp,
|
|
|
|
|
sign,
|
|
|
|
|
};
|
|
|
|
|
let params = config.params || {};
|
|
|
|
|
params = {
|
|
|
|
|
...params,
|
|
|
|
|
..._params,
|
|
|
|
|
};
|
2025-04-21 17:39:41 +08:00
|
|
|
|
|
2025-04-30 14:28:07 +08:00
|
|
|
|
config.params = params;
|
|
|
|
|
config.header.accessToken = accessToken;
|
|
|
|
|
}
|
|
|
|
|
// 配置vlog所需参数
|
|
|
|
|
let vlogToken = storage.getVlogToken();
|
|
|
|
|
let vlogId = storage.getVlogUserInfo();
|
|
|
|
|
if (vlogToken) {
|
|
|
|
|
config.header.headerUserToken = vlogToken;
|
|
|
|
|
config.header.headerUserId = vlogId.id;
|
|
|
|
|
}
|
|
|
|
|
config.header = {
|
|
|
|
|
...config.header,
|
2025-05-02 17:32:03 +08:00
|
|
|
|
uuid: "wzj666",
|
2025-04-30 14:28:07 +08:00
|
|
|
|
};
|
|
|
|
|
return config;
|
|
|
|
|
},
|
|
|
|
|
(config) => {
|
|
|
|
|
return Promise.reject(config);
|
|
|
|
|
}
|
2025-03-14 16:27:50 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 是否正在刷新的标记
|
|
|
|
|
let isRefreshing = false;
|
|
|
|
|
//重试队列
|
|
|
|
|
let requests = [];
|
|
|
|
|
// 必须使用异步函数,注意
|
|
|
|
|
http.interceptors.response.use(
|
2025-04-30 14:28:07 +08:00
|
|
|
|
async (response) => {
|
|
|
|
|
console.log(response)
|
|
|
|
|
/* 请求之后拦截器。可以使用async await 做异步操作 */
|
|
|
|
|
// token存在并且token过期
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 后续进来的请求,此时异步获取token时isRefreshing一定为true
|
|
|
|
|
* 那么也就代表此时token一定是过期状态,响应码也一定为403
|
|
|
|
|
* 也就会去清理缓存
|
|
|
|
|
* 所以会导致页面多次刷新到登录页
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// if (isRefreshing && response.statusCode === 403) {
|
|
|
|
|
// cleanStorage();
|
|
|
|
|
// isRefreshing = false;
|
|
|
|
|
// }
|
2025-04-21 17:39:41 +08:00
|
|
|
|
|
2025-04-25 18:10:54 +08:00
|
|
|
|
let token = storage.getAccessToken();
|
2025-05-05 17:45:22 +08:00
|
|
|
|
|
2025-04-25 18:10:54 +08:00
|
|
|
|
if (
|
|
|
|
|
(token && response.statusCode === 403) ||
|
2025-04-30 14:28:07 +08:00
|
|
|
|
(token && response.data.status === 403)
|
2025-04-25 18:10:54 +08:00
|
|
|
|
) {
|
2025-05-02 17:32:03 +08:00
|
|
|
|
console.log('去刷tk1')
|
2025-04-25 18:10:54 +08:00
|
|
|
|
if (!isRefreshing) {
|
|
|
|
|
isRefreshing = true;
|
2025-05-02 17:32:03 +08:00
|
|
|
|
console.log('去刷tk2')
|
2025-04-25 18:10:54 +08:00
|
|
|
|
//调用刷新token的接口
|
|
|
|
|
return refreshTokenFn(storage.getRefreshToken())
|
|
|
|
|
.then((res) => {
|
|
|
|
|
let {
|
|
|
|
|
accessToken,
|
|
|
|
|
refreshToken
|
|
|
|
|
} = res.data.result;
|
|
|
|
|
storage.setAccessToken(accessToken);
|
|
|
|
|
storage.setRefreshToken(refreshToken);
|
|
|
|
|
|
|
|
|
|
response.header.accessToken = `${accessToken}`;
|
|
|
|
|
// token 刷新后将数组的方法重新执行
|
|
|
|
|
requests.forEach((cb) => cb(accessToken));
|
|
|
|
|
requests = []; // 重新请求完清空
|
|
|
|
|
return http.request(response.config);
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
2025-04-30 14:28:07 +08:00
|
|
|
|
console.log('刷新token失败刷新token失败刷新token失败刷新token失败刷新token失败刷新token失败')
|
|
|
|
|
console.log(err);
|
2025-04-25 18:10:54 +08:00
|
|
|
|
cleanStorage();
|
|
|
|
|
return Promise.reject(err);
|
|
|
|
|
})
|
|
|
|
|
.finally(() => {
|
|
|
|
|
isRefreshing = false;
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
// 返回未执行 resolve 的 Promise
|
|
|
|
|
return new Promise((resolve) => {
|
2025-04-30 14:28:07 +08:00
|
|
|
|
// 用函数形式将 resolve 存入,等待刷新后再执行
|
2025-04-25 18:10:54 +08:00
|
|
|
|
requests.push((token) => {
|
2025-04-30 14:28:07 +08:00
|
|
|
|
// 刷新token后传入新的token
|
2025-04-25 18:10:54 +08:00
|
|
|
|
response.header.accessToken = `${token}`;
|
|
|
|
|
resolve(http.request(response.config));
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-30 14:28:07 +08:00
|
|
|
|
// 这是没有token的情况
|
2025-04-25 18:10:54 +08:00
|
|
|
|
} else if (
|
|
|
|
|
(!token && response.statusCode === 403) ||
|
|
|
|
|
response.data.code === 403
|
|
|
|
|
) {
|
2025-04-30 14:28:07 +08:00
|
|
|
|
console.log('没有token的情况');
|
2025-04-25 18:10:54 +08:00
|
|
|
|
cleanStorage();
|
2025-04-21 17:39:41 +08:00
|
|
|
|
|
2025-04-30 14:28:07 +08:00
|
|
|
|
// 如果当前状态码为正常但是success为不正常时
|
|
|
|
|
} else if (
|
|
|
|
|
(response.statusCode == 200 && !response.data.success) ||
|
|
|
|
|
response.statusCode == 400
|
|
|
|
|
) {
|
2025-05-07 17:45:16 +08:00
|
|
|
|
<<<<<<< Updated upstream
|
2025-05-05 17:45:22 +08:00
|
|
|
|
if (response.data.code == 408) {
|
|
|
|
|
uni.showModal({
|
|
|
|
|
title: '提示',
|
|
|
|
|
content: response.data.message,
|
|
|
|
|
showCancel: false,
|
|
|
|
|
success() {
|
|
|
|
|
logout().then(() => {
|
|
|
|
|
cleanStorage();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
uni.showToast({
|
|
|
|
|
title: response.data.message,
|
|
|
|
|
icon: "none",
|
|
|
|
|
duration: 1500,
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
uni.showToast({
|
|
|
|
|
title: response.data.message,
|
|
|
|
|
icon: "none",
|
|
|
|
|
duration: 1500,
|
|
|
|
|
});
|
2025-05-07 17:45:16 +08:00
|
|
|
|
=======
|
|
|
|
|
if (response.data.message) {
|
|
|
|
|
if (response.data.code == 408) {
|
|
|
|
|
uni.showModal({
|
|
|
|
|
title: '提示',
|
|
|
|
|
content: response.data.message,
|
|
|
|
|
showCancel: false,
|
|
|
|
|
success() {
|
|
|
|
|
logout().then(() => {
|
|
|
|
|
cleanStorage();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
} else if (response.data.code == 20003 || response.data.code == 20004) {
|
|
|
|
|
// rereshtoken也失效了=20004提示=用户已退出,请重新登录,身份凭证token过期=20003提示=用户未登录
|
|
|
|
|
cleanStorage();
|
|
|
|
|
} else {
|
|
|
|
|
uni.showToast({
|
|
|
|
|
title: response.data.message,
|
|
|
|
|
icon: "none",
|
|
|
|
|
duration: 1500,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
>>>>>>> Stashed changes
|
2025-04-30 14:28:07 +08:00
|
|
|
|
}
|
|
|
|
|
} else if (response.data.code == 502) {
|
|
|
|
|
cleanStorage();
|
|
|
|
|
}
|
|
|
|
|
return response;
|
|
|
|
|
},
|
|
|
|
|
(error) => {
|
|
|
|
|
return error;
|
|
|
|
|
}
|
2025-03-14 16:27:50 +08:00
|
|
|
|
);
|
|
|
|
|
|
2025-04-25 18:10:54 +08:00
|
|
|
|
export {
|
|
|
|
|
http
|
|
|
|
|
};
|
2025-03-14 16:27:50 +08:00
|
|
|
|
|
|
|
|
|
export const Method = {
|
2025-04-25 18:10:54 +08:00
|
|
|
|
GET: "GET",
|
|
|
|
|
POST: "POST",
|
|
|
|
|
PUT: "PUT",
|
|
|
|
|
DELETE: "DELETE",
|
|
|
|
|
};
|