From 7cd0cee8363931ba6644bff5ed77cd5cf6deb316 Mon Sep 17 00:00:00 2001 From: "xiaoqi.cxq" Date: Thu, 4 Aug 2022 17:12:38 +0800 Subject: [PATCH] =?UTF-8?q?gitee=20clientId=E9=BB=98=E8=AE=A4=E7=AC=AC?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E5=87=BA=E7=8E=B0401=E5=88=99=E9=9A=8F?= =?UTF-8?q?=E6=9C=BA=E4=B8=80=E4=B8=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/index.js | 12 ++++++++++++ src/services/providers/helpers/giteeHelper.js | 7 ++++--- 2 files changed, 16 insertions(+), 3 deletions(-) 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; }