Skip to content

Commit

Permalink
feat: 登录后接口重放
Browse files Browse the repository at this point in the history
  • Loading branch information
Honye committed Aug 11, 2024
1 parent 9182d31 commit 3e8f042
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 27 deletions.
60 changes: 38 additions & 22 deletions miniprogram/apis/douban.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
import { request as baseRequest } from '../utils/request';
import { store } from '../store/index';

/**
* @type {{ params: RequestOption<any>, resolve: (data: any) => void, reject: (err: any) => void }[]}
*/
const queue = [];

export let isLoginIng = false;
/**
* @param {boolean} value
Expand All @@ -26,27 +31,38 @@ const request = (params) => {
...params,
};

return baseRequest({
header: {
...(!notAuthorization && accessToken && { Authorization: `Bearer ${accessToken}` }),
...header,
},
...rest,
})
.then((resp) => {
if (resp.ok) {
return resp.data;
} else if (resp.statusCode === 400 && [103, 106].includes(resp.data.code)) {
if (!isLoginIng) {
isLoginIng = true;
wx.navigateTo({
url: '/packages/douban/pages/login-phone/login-phone'
});
return new Promise((resolve, reject) => {
baseRequest({
header: {
...(!notAuthorization && accessToken && { Authorization: `Bearer ${accessToken}` }),
...header,
},
...rest,
})
.then((resp) => {
if (resp.ok) {
resolve(resp.data);
} else if (resp.statusCode === 400 && [103, 106].includes(resp.data.code)) {
queue.push({ params, resolve, reject });
if (!isLoginIng) {
isLoginIng = true;
wx.navigateTo({
url: '/packages/douban/pages/login-phone/login-phone'
});
}
} else {
reject(resp.data);
}
} else {
return Promise.reject(resp.data);
}
});
});
});
};

/** 登录成功后重放登录失效接口 */
export const replayRequest = () => {
while (queue.length) {
const { params, resolve, reject } = queue.shift();
request(params).then(resolve, reject);
}
};

/**
Expand Down Expand Up @@ -165,7 +181,7 @@ export const getHotMovies = (params) => {

/**
* 榜单合集
* @param {object} params
* @param {object} params
* @param {string} params.type
* @param {number} [params.start]
* @param {number} [params.count]
Expand All @@ -181,7 +197,7 @@ export const getCollectionList = (params) => {

/**
* 影院热映
* @param {object} params
* @param {object} params
* @param {number} [params.start]
* @param {number} [params.count]
* @returns {Promise<Douban.SubjectCollectionShowingItemsResult>}
Expand Down
7 changes: 2 additions & 5 deletions miniprogram/packages/douban/pages/login-phone/login-phone.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings';
import { store } from '../../../../store/index';
import { setLoginIng } from '../../../../apis/douban';
import { replayRequest, setLoginIng } from '../../../../apis/douban';
import { getCaptcha, verifyCaptcha } from '../../../../apis/douban/accounts';
import { apiSyncDouban } from '../../../../apis/vercel';
import { emitter, events } from '../../../../utils/events';
Expand All @@ -22,10 +22,6 @@ Page({
},
},

onLoad() {
console.log('=== 登录', store)
},

onUnload() {
setLoginIng(false);
},
Expand Down Expand Up @@ -97,6 +93,7 @@ Page({
refreshToken: refresh_token,
user: account_info,
});
replayRequest()
emitter.emit(events.LOGIN_SUCCESS);
wx.showToast({
icon: 'none',
Expand Down

0 comments on commit 3e8f042

Please sign in to comment.