Skip to content

Commit

Permalink
♻️ 重构用户登录逻辑及切换
Browse files Browse the repository at this point in the history
  • Loading branch information
BTMuli committed Sep 21, 2024
1 parent 39a1a1d commit cc7dd7c
Show file tree
Hide file tree
Showing 27 changed files with 734 additions and 892 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@
"jsencrypt": "^3.3.2",
"pinia": "^2.2.2",
"pinia-plugin-persistedstate": "^4.0.1",
"qrcode.vue": "^3.4.1",
"uuid": "^10.0.0",
"vue": "^3.5.6",
"vue-echarts": "^7.0.3",
Expand Down
12 changes: 0 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 29 additions & 39 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import TSidebar from "./components/app/t-sidebar.vue";
import showConfirm from "./components/func/confirm.js";
import showSnackbar from "./components/func/snackbar.js";
import TGSqlite from "./plugins/Sqlite/index.js";
import TSUserAccount from "./plugins/Sqlite/modules/userAccount.js";
import { useAppStore } from "./store/modules/app.js";
import { useUserStore } from "./store/modules/user.js";
import { getBuildTime } from "./utils/TGBuild.js";
Expand Down Expand Up @@ -132,51 +133,40 @@ async function checkDeviceFp(): Promise<void> {
}
}
// 检测 ck,info 数据
async function checkUserLoad(): Promise<void> {
const ckDB = await TGSqlite.getCookie();
if (JSON.stringify(ckDB) === "{}" && appStore.isLogin) {
showSnackbar({
text: "获取 Cookie 失败!请重新登录!",
color: "error",
timeout: 3000,
});
appStore.isLogin = false;
await TGLogger.Error("[App][checkUserLoad] 获取 Cookie 失败!");
// 检测用户数据目录
const appData = await TGSqlite.getAppData();
const userDir = appData.find((item) => item.key === "userDir")?.value;
if (typeof userDir === "undefined") await TGSqlite.saveAppData("userDir", appStore.userDir);
else if (userDir !== appStore.userDir) appStore.userDir = userDir;
await mkdir(appStore.userDir, { recursive: true });
// 检测用户数据
const uidDB = await TSUserAccount.account.getAllUid();
if (uidDB.length === 0) {
showSnackbar({ text: "未检测到可用UID,请重新登录!", color: "warn" });
return;
}
if (JSON.stringify(ckDB) !== "{}" && !appStore.isLogin) appStore.isLogin = true;
const ckLocal = userStore.cookie.value;
if (JSON.stringify(ckLocal) !== JSON.stringify(ckDB)) userStore.cookie.value = ckDB;
const infoLocal = userStore.briefInfo.value;
const appData = await TGSqlite.getAppData();
const infoDB = appData.find((item) => item.key === "userInfo")?.value;
if (typeof infoDB === "undefined" && JSON.stringify(infoLocal) !== "{}") {
await TGSqlite.saveAppData("userInfo", JSON.stringify(infoLocal));
} else if (typeof infoDB !== "undefined" && infoLocal !== JSON.parse(infoDB)) {
userStore.briefInfo.value = JSON.parse(infoDB);
console.info("briefInfo 数据已更新!");
// 然后获取最近的UID
if (userStore.uid.value === undefined || !uidDB.includes(userStore.uid.value)) {
userStore.uid.value = uidDB[0];
}
const accountLocal = userStore.account.value;
const accountDB = await TGSqlite.getCurAccount();
if (accountDB === false) {
if (!appStore.isLogin) return;
showSnackbar({
text: "获取 GameAccount 失败!请尝试更新数据库!",
color: "error",
timeout: 3000,
});
await TGLogger.Error("[App][checkUserLoad] 获取 GameAccount 失败!");
return;
const curAccount = await TSUserAccount.account.getAccount(userStore.uid.value);
if (curAccount === false) {
showSnackbar({ text: `未获取到${userStore.uid.value}账号数据`, color: "error" });
await TGLogger.Error(`[App][listenOnInit] 获取${userStore.uid.value}账号数据失败`);
await new Promise((resolve) => setTimeout(resolve, 1000));
} else {
userStore.briefInfo.value = curAccount.brief;
userStore.cookie.value = curAccount.cookie;
}
if (accountDB !== accountLocal) userStore.account.value = accountDB;
const userDir = appData.find((item) => item.key === "userDir")?.value;
if (typeof userDir === "undefined") {
await TGSqlite.saveAppData("userDir", appStore.userDir);
return;
const curGameAccount = await TSUserAccount.game.getCurAccount(userStore.uid.value);
if (curGameAccount === false) {
showSnackbar({ text: `未获取到${userStore.uid.value}游戏数据`, color: "error" });
await TGLogger.Error(`[App][listenOnInit] 获取${userStore.uid.value}游戏数据失败`);
await new Promise((resolve) => setTimeout(resolve, 1000));
} else {
userStore.account.value = curGameAccount;
}
if (userDir !== appStore.userDir) appStore.userDir = userDir;
await mkdir(appStore.userDir, { recursive: true });
}
async function getDeepLink(): Promise<UnlistenFn> {
Expand Down
Loading

0 comments on commit cc7dd7c

Please sign in to comment.