小程序修正
This commit is contained in:
1 parent
5a92bab835
commit
6ae4c109ce
12 files changed
+232
-62
No files matched your search
+31
-18
@@ -11,8 +11,8 @@
|
||||
const config = require('../config/index');
|
||||
const store = require('./store');
|
||||
|
||||
/** 进行中的刷新,避免并发 401 触发多次刷新 */
|
||||
let refreshing = null;
|
||||
/** 进行中的会话恢复,避免并发 401 触发多次恢复流程 */
|
||||
let recovering = null;
|
||||
|
||||
function staticUrl(path) {
|
||||
if (!path) return '';
|
||||
@@ -52,14 +52,14 @@ function rawRequest(options) {
|
||||
});
|
||||
}
|
||||
|
||||
/** 带鉴权的请求。token 失效时刷新一次并重试。 */
|
||||
/** 带鉴权的请求。token 失效时自动恢复会话并重试一次。 */
|
||||
function request(options) {
|
||||
return rawRequest(options).then(({ statusCode, body }) => {
|
||||
if (body.code === 0) return body.data;
|
||||
|
||||
const needLogin = statusCode === 401 || body.code === 2000 || body.code === 2001;
|
||||
if (needLogin && options.auth !== false && options.retried !== true) {
|
||||
return refreshAccessToken()
|
||||
return recoverSession()
|
||||
.then(() => request(Object.assign({}, options, { retried: true })))
|
||||
.catch(() => {
|
||||
store.reset();
|
||||
@@ -71,27 +71,41 @@ function request(options) {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 恢复会话。两级兜底:
|
||||
* 1. 用 refreshToken 换新 accessToken(快,不打扰用户)
|
||||
* 2. refreshToken 也没有或已失效时,重新走一次微信静默登录
|
||||
*
|
||||
* 第 2 级是必需的:「退出登录」会清空全部 token,
|
||||
* 此后任何需要鉴权的操作(例如再次点「登录注册」改昵称)都必须先重新登录,
|
||||
* 否则必然 401。
|
||||
*/
|
||||
function recoverSession() {
|
||||
if (recovering) return recovering;
|
||||
|
||||
recovering = refreshAccessToken()
|
||||
.catch(() => login())
|
||||
.finally(() => {
|
||||
recovering = null;
|
||||
});
|
||||
|
||||
return recovering;
|
||||
}
|
||||
|
||||
function refreshAccessToken() {
|
||||
if (refreshing) return refreshing;
|
||||
const refreshToken = store.getRefreshToken();
|
||||
if (!refreshToken) return Promise.reject({ code: 2000, message: '未登录' });
|
||||
|
||||
refreshing = rawRequest({
|
||||
return rawRequest({
|
||||
url: '/v1/auth/refresh',
|
||||
method: 'POST',
|
||||
data: { refreshToken },
|
||||
auth: false,
|
||||
})
|
||||
.then(({ body }) => {
|
||||
if (body.code !== 0) throw { code: 2000, message: '登录已过期' };
|
||||
store.setTokens(body.data.accessToken);
|
||||
return body.data.accessToken;
|
||||
})
|
||||
.finally(() => {
|
||||
refreshing = null;
|
||||
});
|
||||
|
||||
return refreshing;
|
||||
}).then(({ body }) => {
|
||||
if (body.code !== 0) throw { code: 2000, message: '登录已过期' };
|
||||
store.setTokens(body.data.accessToken);
|
||||
return body.data.accessToken;
|
||||
});
|
||||
}
|
||||
|
||||
// ── 登录 ──────────────────────────────────────────────────────────────────
|
||||
@@ -195,4 +209,3 @@ function requestVirtualPayment(payData) {
|
||||
}
|
||||
|
||||
module.exports = Object.assign(api, { requestVirtualPayment });
|
||||
|
||||
@@ -108,9 +108,9 @@ const store = {
|
||||
|
||||
reset() {
|
||||
memory.user = null;
|
||||
memory.siteConfig = null;
|
||||
memory.entitlements = [];
|
||||
store.clearTokens();
|
||||
// 站点配置与登录态无关,保留;否则退出登录后页面文案会退回默认值
|
||||
wx.removeStorageSync(config.STORAGE.USER);
|
||||
wx.removeStorageSync(config.STORAGE.ENTITLEMENTS);
|
||||
notify();
|
||||
@@ -118,4 +118,3 @@ const store = {
|
||||
};
|
||||
|
||||
module.exports = store;
|
||||
|
||||
Reference in new issue
Block a user