From 728dfe45d3b365ad461cda75cadf9999509d0000 Mon Sep 17 00:00:00 2001 From: BTMuli Date: Sat, 17 Jan 2026 16:12:53 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E5=B0=9D=E8=AF=95=E6=9B=B4?= =?UTF-8?q?=E6=96=B0cookie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #197 --- src/App.vue | 8 ++- src/plugins/Sqlite/modules/userAccount.ts | 73 ++++++++++++++++++++++- 2 files changed, 77 insertions(+), 4 deletions(-) diff --git a/src/App.vue b/src/App.vue index 0c707aa0..07cfdea0 100644 --- a/src/App.vue +++ b/src/App.vue @@ -34,7 +34,7 @@ import { openUrl } from "@tauri-apps/plugin-opener"; import TGLogger from "@utils/TGLogger.js"; import { getWindowSize, resizeWindow } from "@utils/TGWindow.js"; import { storeToRefs } from "pinia"; -import { computed, nextTick, onMounted, onUnmounted, ref } from "vue"; +import { computed, nextTick, onBeforeMount, onUnmounted, ref } from "vue"; import { useRouter } from "vue-router"; const router = useRouter(); @@ -53,18 +53,18 @@ let closeListener: UnlistenFn | null = null; let textScaleListener: UnlistenFn | null = null; let yaeFlag: Array = []; -onMounted(async () => { +onBeforeMount(async () => { const win = getCurrentWindow(); isMain.value = win.label === "TeyvatGuide"; if (isMain.value) { const title = "Teyvat Guide v" + (await app.getVersion()) + " Beta"; await win.setTitle(title); await listenOnInit(); - await core.invoke("init_app"); dpListener = await event.listen("active_deep_link", handleDpListen); yaeListener = await event.listen("yae_read", handleYaeListen); closeListener = await event.listen("main-window-close-requested", handleWindowClose); await nextTick(); + await core.invoke("init_app"); } if (needResize.value !== "false") await resizeWindow(); document.documentElement.className = theme.value; @@ -317,6 +317,8 @@ async function checkUserLoad(): Promise { isLogin.value = false; return; } + // 检测ck刷新 + await TSUserAccount.account.updateCk(); if (!isLogin.value) isLogin.value = true; // 然后获取最近的UID if (uid.value === undefined || !uidDB.includes(uid.value)) { diff --git a/src/plugins/Sqlite/modules/userAccount.ts b/src/plugins/Sqlite/modules/userAccount.ts index e8321e8c..f329d004 100644 --- a/src/plugins/Sqlite/modules/userAccount.ts +++ b/src/plugins/Sqlite/modules/userAccount.ts @@ -1,8 +1,12 @@ /** * 用户账户模块 - * @since Beta v0.9.0 + * @since Beta v0.9.2 */ +import showLoading from "@comp/func/loading.js"; +import showSnackbar from "@comp/func/snackbar.js"; +import bbsReq from "@req/bbsReq.js"; +import passportReq from "@req/passportReq.js"; import { path } from "@tauri-apps/api"; import { exists, mkdir, readTextFile, writeTextFile } from "@tauri-apps/plugin-fs"; import TGLogger from "@utils/TGLogger.js"; @@ -136,6 +140,72 @@ async function saveAccount(data: TGApp.App.Account.User): Promise { await db.execute(sql); } +/** + * 检测并更新 + * @since Beta v0.9.2 + * @returns 无返回值 + */ +async function updateAllAccountCk(): Promise { + const accounts = await getAllAccount(); + const checkTime = 5 * 24 * 3600 * 1000; + for (const account of accounts) { + const diffTime = Date.now() - new Date(account.updated).getTime(); + if (diffTime > checkTime) { + const update = await updateAccountCk(account); + if (update) { + showSnackbar.success(`成功更新${account.uid}的Cookie`); + } + } + } +} + +/** + * 更新用户Cookie + * @since Beta v0.9.2 + * @param data - 用户信息 + * @returns 是否更新成功 + */ +async function updateAccountCk(data: TGApp.App.Account.User): Promise { + await showLoading.start("正在更新用户Cookie", `UID:${data.uid},上次更新:${data.updated}`); + const ck = data.cookie; + await showLoading.update("正在获取 LToken"); + const ltokenRes = await passportReq.lToken.get(ck); + if (typeof ltokenRes !== "string") { + await showLoading.end(); + showSnackbar.error(`[${ltokenRes.retcode}]${ltokenRes.message}`); + await TGLogger.Error(`获取LToken失败:${ltokenRes.retcode}-${ltokenRes.message}`); + return false; + } + ck.ltoken = ltokenRes; + await showLoading.update("正在获取 CookieToken"); + const cookieTokenRes = await passportReq.cookieToken(ck); + if (typeof cookieTokenRes !== "string") { + await showLoading.end(); + showSnackbar.error(`[${cookieTokenRes.retcode}]${cookieTokenRes.message}`); + await TGLogger.Error( + `获取CookieToken失败:${cookieTokenRes.retcode}-${cookieTokenRes.message}`, + ); + return false; + } + ck.cookie_token = cookieTokenRes; + await showLoading.update("正在获取用户信息"); + const briefRes = await bbsReq.userInfo(ck); + if ("retcode" in briefRes) { + await showLoading.end(); + showSnackbar.error(`[${briefRes.retcode}]${briefRes.message}`); + await TGLogger.Error(`获取用户数据失败:${briefRes.retcode}-${briefRes.message}`); + return false; + } + const updated = timestampToDate(new Date().getTime()); + await showLoading.update("正在写入数据库"); + const db = await TGSqlite.getDB(); + await db.execute( + "UPDATE UserAccount SET cookie = '?' AND brief = '?' AND updated = '?' WHERE uid = '?';", + [JSON.stringify(ck), JSON.stringify(briefRes), updated, data.uid], + ); + return true; +} + /** * 备份用户数据 * @since Beta v0.6.0 @@ -311,6 +381,7 @@ const TSUserAccount = { getAllAccount, getAccount: getUserAccount, saveAccount, + updateCk: updateAllAccountCk, copy: copyCookie, deleteAccount, backup: backUpAccount,