mirror of
https://github.com/BTMuli/TeyvatGuide.git
synced 2026-05-26 06:15:45 +08:00
✨ 胡桃云添加自动刷新功能,优化用户信息更新逻辑
This commit is contained in:
@@ -46,7 +46,7 @@ import useHutaoStore from "@store/hutao.js";
|
|||||||
import { openUrl } from "@tauri-apps/plugin-opener";
|
import { openUrl } from "@tauri-apps/plugin-opener";
|
||||||
import { timeStr2str } from "@utils/toolFunc.js";
|
import { timeStr2str } from "@utils/toolFunc.js";
|
||||||
import { storeToRefs } from "pinia";
|
import { storeToRefs } from "pinia";
|
||||||
import { ref } from "vue";
|
import { onMounted, ref } from "vue";
|
||||||
|
|
||||||
const hutaoStore = useHutaoStore();
|
const hutaoStore = useHutaoStore();
|
||||||
const { userName, userInfo, isLogin } = storeToRefs(useHutaoStore());
|
const { userName, userInfo, isLogin } = storeToRefs(useHutaoStore());
|
||||||
@@ -54,6 +54,8 @@ const { userName, userInfo, isLogin } = storeToRefs(useHutaoStore());
|
|||||||
const showVerify = ref<boolean>(false);
|
const showVerify = ref<boolean>(false);
|
||||||
const loadInfo = ref<boolean>(false);
|
const loadInfo = ref<boolean>(false);
|
||||||
|
|
||||||
|
onMounted(async () => await hutaoStore.tryAutoRefresh());
|
||||||
|
|
||||||
function getAccountDesc(): string {
|
function getAccountDesc(): string {
|
||||||
if (!isLogin.value) return "未登录";
|
if (!isLogin.value) return "未登录";
|
||||||
if (userInfo.value) {
|
if (userInfo.value) {
|
||||||
|
|||||||
@@ -87,6 +87,7 @@ import TGLogger from "@utils/TGLogger.js";
|
|||||||
import { compareVersions } from "@utils/toolFunc.js";
|
import { compareVersions } from "@utils/toolFunc.js";
|
||||||
import { storeToRefs } from "pinia";
|
import { storeToRefs } from "pinia";
|
||||||
import { defineComponent, onMounted, ref, shallowRef, watch } from "vue";
|
import { defineComponent, onMounted, ref, shallowRef, watch } from "vue";
|
||||||
|
import useHutaoStore from "@store/hutao.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 单文件组件类型
|
* 单文件组件类型
|
||||||
@@ -106,6 +107,7 @@ type SelectItem = {
|
|||||||
|
|
||||||
const homeStore = useHomeStore();
|
const homeStore = useHomeStore();
|
||||||
const bbsStore = useBBSStore();
|
const bbsStore = useBBSStore();
|
||||||
|
const hutaoStore = useHutaoStore();
|
||||||
|
|
||||||
const { devMode, isLogin, lastUcts } = storeToRefs(useAppStore());
|
const { devMode, isLogin, lastUcts } = storeToRefs(useAppStore());
|
||||||
const { gameList } = storeToRefs(bbsStore);
|
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.
|
// @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";
|
const isProdEnv = import.meta.env.MODE === "production";
|
||||||
if (isProdEnv && devMode.value) devMode.value = false;
|
if (isProdEnv && devMode.value) devMode.value = false;
|
||||||
|
await showLoading.start("正在加载首页小部件");
|
||||||
if (isLogin.value) {
|
if (isLogin.value) {
|
||||||
await TSUserAccount.account.updateCk();
|
await TSUserAccount.account.updateCk();
|
||||||
await showLoading.start("正在加载首页小部件");
|
|
||||||
games.value = gameList.value.map((i) => ({ icon: i.app_icon, title: i.name, gid: i.id }));
|
games.value = gameList.value.map((i) => ({ icon: i.app_icon, title: i.name, gid: i.id }));
|
||||||
showItems.value = homeStore.getShowItems();
|
showItems.value = homeStore.getShowItems();
|
||||||
showItemsAll.value = ["便笺签到", "素材日历", "限时祈愿", "近期活动"];
|
showItemsAll.value = ["便笺签到", "素材日历", "限时祈愿", "近期活动"];
|
||||||
@@ -138,6 +140,7 @@ onMounted(async () => {
|
|||||||
oldItems.value = showItems.value;
|
oldItems.value = showItems.value;
|
||||||
await loadComp();
|
await loadComp();
|
||||||
await checkAppUpdate();
|
await checkAppUpdate();
|
||||||
|
await hutaoStore.tryAutoRefresh();
|
||||||
});
|
});
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/**
|
/**
|
||||||
* 胡桃账号
|
* 胡桃账号
|
||||||
* @since Beta v0.10.1
|
* @since Beta v0.10.2
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import showDialog from "@comp/func/dialog.js";
|
import showDialog from "@comp/func/dialog.js";
|
||||||
@@ -28,6 +28,8 @@ const useHutaoStore = defineStore(
|
|||||||
const accessExpire = ref<number>(0);
|
const accessExpire = ref<number>(0);
|
||||||
/** 用户信息 */
|
/** 用户信息 */
|
||||||
const userInfo = ref<TGApp.Plugins.Hutao.Account.InfoRes>();
|
const userInfo = ref<TGApp.Plugins.Hutao.Account.InfoRes>();
|
||||||
|
/** 上次刷新时间 */
|
||||||
|
const lastUts = ref<number>(0);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检测是否超时
|
* 检测是否超时
|
||||||
@@ -40,7 +42,11 @@ const useHutaoStore = defineStore(
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function tryLogin(): Promise<void> {
|
async function tryLogin(): Promise<void> {
|
||||||
const inputN = await showDialog.input("请输入胡桃云账号", "邮箱:");
|
const inputN = await showDialog.inputF({
|
||||||
|
title: "请输入胡桃云账号",
|
||||||
|
text: "邮箱:",
|
||||||
|
type: "email",
|
||||||
|
});
|
||||||
if (!inputN) {
|
if (!inputN) {
|
||||||
showSnackbar.cancel("已取消胡桃云账号输入");
|
showSnackbar.cancel("已取消胡桃云账号输入");
|
||||||
return;
|
return;
|
||||||
@@ -49,7 +55,11 @@ const useHutaoStore = defineStore(
|
|||||||
showSnackbar.warn("请输入合法邮箱地址");
|
showSnackbar.warn("请输入合法邮箱地址");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const inputP = await showDialog.input("请输入密码", "密码");
|
const inputP = await showDialog.inputF({
|
||||||
|
title: "请输入密码",
|
||||||
|
text: "密码",
|
||||||
|
type: "password",
|
||||||
|
});
|
||||||
if (!inputP) {
|
if (!inputP) {
|
||||||
showSnackbar.warn("已取消胡桃云登录");
|
showSnackbar.warn("已取消胡桃云登录");
|
||||||
return;
|
return;
|
||||||
@@ -123,6 +133,11 @@ const useHutaoStore = defineStore(
|
|||||||
|
|
||||||
async function tryRefreshInfo(): Promise<void> {
|
async function tryRefreshInfo(): Promise<void> {
|
||||||
await tryRefreshToken();
|
await tryRefreshToken();
|
||||||
|
if (!isLogin.value) return;
|
||||||
|
if (!accessToken.value || accessToken.value === "") {
|
||||||
|
showSnackbar.warn("未找到合法的AccessToken");
|
||||||
|
return;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const resp = await hutao.Account.info(accessToken.value!);
|
const resp = await hutao.Account.info(accessToken.value!);
|
||||||
if (resp.retcode !== 0) {
|
if (resp.retcode !== 0) {
|
||||||
@@ -139,6 +154,7 @@ const useHutaoStore = defineStore(
|
|||||||
}
|
}
|
||||||
userInfo.value = resp.data;
|
userInfo.value = resp.data;
|
||||||
showSnackbar.success("成功刷新用户信息");
|
showSnackbar.success("成功刷新用户信息");
|
||||||
|
lastUts.value = Math.floor(Date.now() / 1000);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const errMsg = TGHttps.getErrMsg(e);
|
const errMsg = TGHttps.getErrMsg(e);
|
||||||
showSnackbar.error(`刷新用户信息失败:${errMsg}`);
|
showSnackbar.error(`刷新用户信息失败:${errMsg}`);
|
||||||
@@ -155,10 +171,18 @@ const useHutaoStore = defineStore(
|
|||||||
try {
|
try {
|
||||||
const resp = await hutao.Token.refresh(refreshToken.value);
|
const resp = await hutao.Token.refresh(refreshToken.value);
|
||||||
if (resp.retcode !== 0) {
|
if (resp.retcode !== 0) {
|
||||||
showSnackbar.warn(`[${resp.retcode}] ${resp.message}`);
|
|
||||||
await TGLogger.Warn(
|
await TGLogger.Warn(
|
||||||
`[HutaoStore][tryRefreshToken] 刷新Token失败:${resp.retcode} ${resp.message}`,
|
`[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;
|
return;
|
||||||
}
|
}
|
||||||
if (!resp.data) {
|
if (!resp.data) {
|
||||||
@@ -184,6 +208,19 @@ const useHutaoStore = defineStore(
|
|||||||
return Date.now() > expire;
|
return Date.now() > expire;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function tryAutoRefresh(): Promise<void> {
|
||||||
|
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 {
|
return {
|
||||||
isLogin,
|
isLogin,
|
||||||
userName,
|
userName,
|
||||||
@@ -197,6 +234,7 @@ const useHutaoStore = defineStore(
|
|||||||
tryRefreshToken,
|
tryRefreshToken,
|
||||||
tryRefreshInfo,
|
tryRefreshInfo,
|
||||||
checkGachaExpire,
|
checkGachaExpire,
|
||||||
|
tryAutoRefresh,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user