From f75a39b783955a0fa24e5fdd8139c76e474a8021 Mon Sep 17 00:00:00 2001 From: BTMuli Date: Fri, 19 May 2023 14:55:02 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20=E5=BC=80=E5=A7=8B=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/Config.vue | 64 +++++++++++------------------------ src/web/request/initCookie.ts | 2 ++ 2 files changed, 21 insertions(+), 45 deletions(-) diff --git a/src/pages/Config.vue b/src/pages/Config.vue index ad675c05..f7dff660 100644 --- a/src/pages/Config.vue +++ b/src/pages/Config.vue @@ -409,7 +409,6 @@ function submitHome () { // 输入 Cookie async function inputCookie () { - // 将 Cookie 从 string 转为 object const cookie = confirmInput.value; if (cookie === "") { snackbarText.value = "Cookie 为空!"; @@ -417,54 +416,29 @@ async function inputCookie () { snackbar.value = true; return; } - loadingTitle.value = "正在保存 Cookie..."; - loading.value = true; - const cookieObj: any = {}; - cookie.replace(/\s+/g, "").split(";").forEach((item) => { - const itemArr = item.split("="); - cookieObj[itemArr[0]] = itemArr[1]; - }); - const saveCookie:BTMuli.User.Base.Cookie = { - account_id: cookieObj.account_id || "", - cookie_token: cookieObj.cookie_token || "", - ltoken: cookieObj.ltoken || "", - ltuid: cookieObj.ltuid || "", - mid: cookieObj.mid || "", - stoken: cookieObj.stoken || "", - stuid: cookieObj.stuid || "", - login_ticket: cookieObj.login_ticket || "", - login_uid: cookieObj.login_uid || "", - }; - // 保存到数据库 - await TGSqlite.inputCookie(JSON.stringify(saveCookie)); - // 保存到 store - localStorage.setItem("cookie", JSON.stringify(saveCookie)); - if (saveCookie.stoken === "") { - loadingTitle.value = "正在获取 tokens..."; - const tokenRes = await TGRequest.User.byLoginTicket.getLTokens(cookie, cookieObj.login_ticket, cookieObj.login_uid); - if (Array.isArray(tokenRes)) { - loadingTitle.value = "正在保存 tokens..."; - const lToken = tokenRes.find((item) => item.name === "ltoken"); - const sToken = tokenRes.find((item) => item.name === "stoken"); - if (lToken) await TGSqlite.saveAppData("ltoken", lToken.token); - if (sToken) await TGSqlite.saveAppData("stoken", sToken.token); - loading.value = false; - snackbarText.value = "Cookie 已保存!"; - snackbarColor.value = "success"; - snackbar.value = true; - } else { - loading.value = false; - snackbarText.value = "Cookie 无效!"; - snackbarColor.value = "error"; - snackbar.value = true; - } - } else { - await TGSqlite.saveAppData("ltoken", saveCookie.ltoken); - await TGSqlite.saveAppData("stoken", saveCookie.stoken); + loadingTitle.value = "正在获取 tokens..."; + // 提取 cookie.login_ticket 和 cookie.login_uid + const ticket = cookie.match(/login_ticket=(.*?);/)?.toString(); + const uid = cookie.match(/login_uid=(.*?);/)?.toString(); + // 如果两者不存在 + if (!ticket || !uid) { + snackbarText.value = "Cookie 无效!"; + snackbarColor.value = "error"; + snackbar.value = true; + return; + } + console.log(ticket, uid); + try { + await TGRequest.User.init(ticket, uid); loading.value = false; snackbarText.value = "Cookie 已保存!"; snackbarColor.value = "success"; snackbar.value = true; + } catch (err) { + loading.value = false; + snackbarText.value = "Cookie 无效!"; + snackbarColor.value = "error"; + snackbar.value = true; } } diff --git a/src/web/request/initCookie.ts b/src/web/request/initCookie.ts index eac8c166..e733707c 100644 --- a/src/web/request/initCookie.ts +++ b/src/web/request/initCookie.ts @@ -48,6 +48,8 @@ async function initCookie (ticket: string, uid: string): Promise { const mid = await verifyLToken(cookie.ltoken, cookie.ltuid, cookie.stoken); if (typeof mid === "string") cookie.mid = mid; await TGSqlite.saveAppData("cookie", JSON.stringify(cookie)); + } else { + throw new Error("获取 token 失败"); } }