From b36fe7a67ef421db3855db0a26d9ee0266a34b76 Mon Sep 17 00:00:00 2001 From: CrescentLeaf Date: Tue, 7 Oct 2025 13:07:11 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=8E=A5=E5=8F=A3=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=88=B7=E6=96=B0=E8=AE=BF=E9=97=AE=E4=BB=A4?= =?UTF-8?q?=E7=89=8C=E5=88=B6=E9=80=A0=E7=9A=84=E7=9A=84=E7=81=B5=E8=BD=A6?= =?UTF-8?q?=20*=20=E6=98=AF=E4=BB=80=E4=B9=88=E6=88=91=E5=BF=98=E4=BA=86,?= =?UTF-8?q?=20=E4=BD=86=E6=98=AF=E8=BF=99=E5=B0=B1=E6=98=AF=E7=81=B5?= =?UTF-8?q?=E8=BD=A6=F0=9F=98=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/api/ApiDeclare.ts | 3 ++- client/api/Client.ts | 32 ++++++++++++++++++++++---------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/client/api/ApiDeclare.ts b/client/api/ApiDeclare.ts index 761aeb0..e8593fd 100644 --- a/client/api/ApiDeclare.ts +++ b/client/api/ApiDeclare.ts @@ -28,8 +28,9 @@ export type CallMethod = export type ClientEvent = "Client.onMessage" - export const CallableMethodBeforeAuth = [ +export const CallableMethodBeforeAuth = [ "User.auth", "User.register", "User.login", + "User.refreshAccessToken", ] \ No newline at end of file diff --git a/client/api/Client.ts b/client/api/Client.ts index 5267555..8a75c08 100644 --- a/client/api/Client.ts +++ b/client/api/Client.ts @@ -25,12 +25,10 @@ class Client { }, }) this.socket!.on("connect", async () => { - const re = await this.invoke("User.auth", { - access_token: data.access_token - }, 1000) - if (re.code != 200) - checkApiSuccessOrSncakbar(re, "重連失敗") this.connected = true + const re = await this.auth(data.access_token) + if (re.code != 200) + checkApiSuccessOrSncakbar(re, "重连失败") }) this.socket!.on("disconnect", () => { this.connected = false @@ -44,27 +42,41 @@ class Client { } }) } - static invoke(method: CallMethod, args: unknown = {}, timeout: number = 5000): Promise { + static invoke(method: CallMethod, args: unknown = {}, timeout: number = 5000, refreshAndRetryLimit: number = 3, forceRefreshAndRetry: boolean = false): Promise { + // 在 未初始化 / 未建立连接且调用非可调用接口 的时候进行延迟 + console.log(this.connected, method) if (this.socket == null || (!this.connected && !CallableMethodBeforeAuth.includes(method))) { return new Promise((reslove) => { setTimeout(async () => reslove(await this.invoke(method, args, timeout)), 500) }) } + // 反之, 返回 Promise return new Promise((resolve) => { this.socket!.timeout(timeout).emit("The_White_Silk", method, args, async (err: Error, res: ApiCallbackMessage) => { + // 错误处理 if (err) return resolve({ code: -1, msg: err.message.indexOf("timed out") != -1 ? "請求超時" : err.message, }) - if (!["User.refreshAccessToken", ...CallableMethodBeforeAuth].includes(method) && res.code == 401) { + // 在特殊的方法之中, 不予进行: 令牌刷新并重试 + // 附带 retry 次数限制 + if ( + ( + forceRefreshAndRetry || + ( + !CallableMethodBeforeAuth.includes(method) + && res.code == 401 + ) + ) && refreshAndRetryLimit > 0 + ) { const token = await this.refreshAccessToken() if (token) { data.access_token = token data.apply() resolve(await this.invoke(method, { ...args, - token - }, timeout)) + [method == "User.auth" ? "access_token" : "token"]: token, + }, timeout, refreshAndRetryLimit - 1)) } else resolve(res) } else @@ -81,7 +93,7 @@ class Client { static async auth(token: string, timeout: number = 5000) { const re = await this.invoke("User.auth", { access_token: token - }, timeout) + }, timeout, 1, true) if (re.code == 200) { await this.updateCachedProfile() document.cookie = 'token=' + token