diff --git a/server/index.js b/server/index.js index 16ac2f00..283be061 100644 --- a/server/index.js +++ b/server/index.js @@ -36,6 +36,18 @@ module.exports = (app) => { }), user.paypalIpn); app.get('/giteeClientId', (req, res) => { 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()))]; res.send(clientId); }); diff --git a/src/services/providers/helpers/giteeHelper.js b/src/services/providers/helpers/giteeHelper.js index 70771f13..e682f279 100644 --- a/src/services/providers/helpers/giteeHelper.js +++ b/src/services/providers/helpers/giteeHelper.js @@ -68,12 +68,13 @@ export default { /** * 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; if (!silent) { const clientId = (await networkSvc.request({ method: 'GET', url: 'giteeClientId', + params: { random: randomClientId }, })).body; // Get an OAuth2 code const { code } = await networkSvc.startOauth2( @@ -119,7 +120,7 @@ export default { })).body; } catch (err) { if (err.status === 401) { - this.startOauth2(); + this.startOauth2(null, false, isMain, 1); } throw err; } @@ -219,7 +220,7 @@ export default { return tree; } catch (err) { if (err.status === 401) { - this.startOauth2(); + this.startOauth2(null, false, null, 1); } throw err; }