gitee clientId默认第一个出现401则随机一个

This commit is contained in:
xiaoqi.cxq 2022-08-04 17:12:38 +08:00
parent bfb7a6447e
commit 7cd0cee836
2 changed files with 16 additions and 3 deletions

View File

@ -36,6 +36,18 @@ module.exports = (app) => {
}), user.paypalIpn); }), user.paypalIpn);
app.get('/giteeClientId', (req, res) => { app.get('/giteeClientId', (req, res) => {
const giteeClientIds = conf.values.giteeClientId.split(','); const giteeClientIds = conf.values.giteeClientId.split(',');
// 仅一个 则直接返回
if (giteeClientIds.length === 1) {
res.send(giteeClientIds[0]);
return;
}
// 是否随机一个clientId 默认第一个 如果random 为1 则使用随机 避免单个应用接口次数用满无法自动切换其他应用
const random = req.query.random;
if (!random) {
res.send(giteeClientIds[0]);
return;
}
// 随机一个
const clientId = giteeClientIds[Math.floor((giteeClientIds.length * Math.random()))]; const clientId = giteeClientIds[Math.floor((giteeClientIds.length * Math.random()))];
res.send(clientId); res.send(clientId);
}); });

View File

@ -68,12 +68,13 @@ export default {
/** /**
* https://developer.gitee.com/apps/building-oauth-apps/authorization-options-for-oauth-apps/ * https://developer.gitee.com/apps/building-oauth-apps/authorization-options-for-oauth-apps/
*/ */
async startOauth2(lastToken, silent = false, isMain) { async startOauth2(lastToken, silent = false, isMain, randomClientId) {
let tokenBody; let tokenBody;
if (!silent) { if (!silent) {
const clientId = (await networkSvc.request({ const clientId = (await networkSvc.request({
method: 'GET', method: 'GET',
url: 'giteeClientId', url: 'giteeClientId',
params: { random: randomClientId },
})).body; })).body;
// Get an OAuth2 code // Get an OAuth2 code
const { code } = await networkSvc.startOauth2( const { code } = await networkSvc.startOauth2(
@ -119,7 +120,7 @@ export default {
})).body; })).body;
} catch (err) { } catch (err) {
if (err.status === 401) { if (err.status === 401) {
this.startOauth2(); this.startOauth2(null, false, isMain, 1);
} }
throw err; throw err;
} }
@ -219,7 +220,7 @@ export default {
return tree; return tree;
} catch (err) { } catch (err) {
if (err.status === 401) { if (err.status === 401) {
this.startOauth2(); this.startOauth2(null, false, null, 1);
} }
throw err; throw err;
} }