From 816ce31279674bfe19785a53e316d35731c10cec Mon Sep 17 00:00:00 2001 From: whyour Date: Mon, 28 Feb 2022 22:30:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9openapi=E8=8E=B7=E5=8F=96toke?= =?UTF-8?q?n=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/services/open.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/back/services/open.ts b/back/services/open.ts index f2705dec..5a81cc00 100644 --- a/back/services/open.ts +++ b/back/services/open.ts @@ -108,12 +108,26 @@ export default class OpenService { client_id: string; client_secret: string; }): Promise { - const token = uuidV4(); + let token = uuidV4(); const expiration = Math.round(Date.now() / 1000) + 2592000; // 2592000 30天 const doc = await AppModel.findOne({ where: { client_id, client_secret } }); if (doc) { + const timestamp = Math.round(Date.now() / 1000); + const invalidTokens = (doc.tokens || []).filter( + (x) => x.expiration >= timestamp, + ); + let tokens = invalidTokens; + if (invalidTokens.length > 5) { + tokens = [ + ...invalidTokens.slice(0, 4), + { ...invalidTokens[4], expiration }, + ]; + token = invalidTokens[4].value; + } else { + tokens = [...invalidTokens, { value: token, expiration }]; + } await AppModel.update( - { tokens: [...(doc.tokens || []), { value: token, expiration }] }, + { tokens }, { where: { client_id, client_secret } }, ); return {