From 4f7a7bc4294ebb16734797297da419d97ebdf0d5 Mon Sep 17 00:00:00 2001 From: BTMuli Date: Tue, 19 May 2026 18:47:41 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E8=83=A1=E6=A1=83=E4=BA=91?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=87=AA=E5=8A=A8=E5=88=B7=E6=96=B0=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=EF=BC=8C=E4=BC=98=E5=8C=96=E7=94=A8=E6=88=B7=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E6=9B=B4=E6=96=B0=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/pageConfig/tc-hutaoBadge.vue | 4 +- src/pages/common/PageHome.vue | 5 ++- src/store/modules/hutao.ts | 46 +++++++++++++++++++-- 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/components/pageConfig/tc-hutaoBadge.vue b/src/components/pageConfig/tc-hutaoBadge.vue index 8c888769..65cacb19 100644 --- a/src/components/pageConfig/tc-hutaoBadge.vue +++ b/src/components/pageConfig/tc-hutaoBadge.vue @@ -46,7 +46,7 @@ import useHutaoStore from "@store/hutao.js"; import { openUrl } from "@tauri-apps/plugin-opener"; import { timeStr2str } from "@utils/toolFunc.js"; import { storeToRefs } from "pinia"; -import { ref } from "vue"; +import { onMounted, ref } from "vue"; const hutaoStore = useHutaoStore(); const { userName, userInfo, isLogin } = storeToRefs(useHutaoStore()); @@ -54,6 +54,8 @@ const { userName, userInfo, isLogin } = storeToRefs(useHutaoStore()); const showVerify = ref(false); const loadInfo = ref(false); +onMounted(async () => await hutaoStore.tryAutoRefresh()); + function getAccountDesc(): string { if (!isLogin.value) return "未登录"; if (userInfo.value) { diff --git a/src/pages/common/PageHome.vue b/src/pages/common/PageHome.vue index 82b16360..ba02ebc4 100644 --- a/src/pages/common/PageHome.vue +++ b/src/pages/common/PageHome.vue @@ -87,6 +87,7 @@ import TGLogger from "@utils/TGLogger.js"; import { compareVersions } from "@utils/toolFunc.js"; import { storeToRefs } from "pinia"; import { defineComponent, onMounted, ref, shallowRef, watch } from "vue"; +import useHutaoStore from "@store/hutao.js"; /** * 单文件组件类型 @@ -106,6 +107,7 @@ type SelectItem = { const homeStore = useHomeStore(); const bbsStore = useBBSStore(); +const hutaoStore = useHutaoStore(); const { devMode, isLogin, lastUcts } = storeToRefs(useAppStore()); const { gameList } = storeToRefs(bbsStore); @@ -125,9 +127,9 @@ onMounted(async () => { // @ts-expect-error-next-line The import.meta meta-property is not allowed in files which will build into CommonJS output. const isProdEnv = import.meta.env.MODE === "production"; if (isProdEnv && devMode.value) devMode.value = false; + await showLoading.start("正在加载首页小部件"); if (isLogin.value) { await TSUserAccount.account.updateCk(); - await showLoading.start("正在加载首页小部件"); games.value = gameList.value.map((i) => ({ icon: i.app_icon, title: i.name, gid: i.id })); showItems.value = homeStore.getShowItems(); showItemsAll.value = ["便笺签到", "素材日历", "限时祈愿", "近期活动"]; @@ -138,6 +140,7 @@ onMounted(async () => { oldItems.value = showItems.value; await loadComp(); await checkAppUpdate(); + await hutaoStore.tryAutoRefresh(); }); watch( diff --git a/src/store/modules/hutao.ts b/src/store/modules/hutao.ts index bce6a77f..260cfb1f 100644 --- a/src/store/modules/hutao.ts +++ b/src/store/modules/hutao.ts @@ -1,6 +1,6 @@ /** * 胡桃账号 - * @since Beta v0.10.1 + * @since Beta v0.10.2 */ import showDialog from "@comp/func/dialog.js"; @@ -28,6 +28,8 @@ const useHutaoStore = defineStore( const accessExpire = ref(0); /** 用户信息 */ const userInfo = ref(); + /** 上次刷新时间 */ + const lastUts = ref(0); /** * 检测是否超时 @@ -40,7 +42,11 @@ const useHutaoStore = defineStore( } async function tryLogin(): Promise { - const inputN = await showDialog.input("请输入胡桃云账号", "邮箱:"); + const inputN = await showDialog.inputF({ + title: "请输入胡桃云账号", + text: "邮箱:", + type: "email", + }); if (!inputN) { showSnackbar.cancel("已取消胡桃云账号输入"); return; @@ -49,7 +55,11 @@ const useHutaoStore = defineStore( showSnackbar.warn("请输入合法邮箱地址"); return; } - const inputP = await showDialog.input("请输入密码", "密码"); + const inputP = await showDialog.inputF({ + title: "请输入密码", + text: "密码", + type: "password", + }); if (!inputP) { showSnackbar.warn("已取消胡桃云登录"); return; @@ -123,6 +133,11 @@ const useHutaoStore = defineStore( async function tryRefreshInfo(): Promise { await tryRefreshToken(); + if (!isLogin.value) return; + if (!accessToken.value || accessToken.value === "") { + showSnackbar.warn("未找到合法的AccessToken"); + return; + } try { const resp = await hutao.Account.info(accessToken.value!); if (resp.retcode !== 0) { @@ -139,6 +154,7 @@ const useHutaoStore = defineStore( } userInfo.value = resp.data; showSnackbar.success("成功刷新用户信息"); + lastUts.value = Math.floor(Date.now() / 1000); } catch (e) { const errMsg = TGHttps.getErrMsg(e); showSnackbar.error(`刷新用户信息失败:${errMsg}`); @@ -155,10 +171,18 @@ const useHutaoStore = defineStore( try { const resp = await hutao.Token.refresh(refreshToken.value); if (resp.retcode !== 0) { - showSnackbar.warn(`[${resp.retcode}] ${resp.message}`); await TGLogger.Warn( `[HutaoStore][tryRefreshToken] 刷新Token失败:${resp.retcode} ${resp.message}`, ); + // 令牌无效或者过期,自动退出账号 + if (resp.retcode === 514002) { + refreshToken.value = ""; + accessToken.value = ""; + isLogin.value = false; + showSnackbar.warn(`[${resp.retcode}] ${resp.message},已自动退出`); + } else { + showSnackbar.warn(`[${resp.retcode}] ${resp.message}`); + } return; } if (!resp.data) { @@ -184,6 +208,19 @@ const useHutaoStore = defineStore( return Date.now() > expire; } + async function tryAutoRefresh(): Promise { + if (!isLogin.value) return; + const nowTs = Math.floor(Date.now() / 1000); + const diffTime = nowTs - lastUts.value; + if (diffTime < 60 * 60 * 24 * 3) return; + try { + await tryRefreshInfo(); + } catch (e) { + await TGLogger.Error(`[HutaoStore][tryAutoRefresh]自动刷新异常: ${e}`); + lastUts.value = Math.floor(Date.now() / 1000); + } + } + return { isLogin, userName, @@ -197,6 +234,7 @@ const useHutaoStore = defineStore( tryRefreshToken, tryRefreshInfo, checkGachaExpire, + tryAutoRefresh, }; }, {