Skip to content

Commit

Permalink
🐛 修复数据恢复异常
Browse files Browse the repository at this point in the history
  • Loading branch information
BTMuli committed Jun 20, 2024
1 parent b87de84 commit 7ea47b4
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/components/config/tc-info.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@
import { app, os } from "@tauri-apps/api";
import { onMounted, ref } from "vue";
import TGSqlite from "../../plugins/Sqlite";
import { useAchievementsStore } from "../../store/modules/achievements";
import showSnackbar from "../func/snackbar";
import TGSqlite from "../../plugins/Sqlite/index.js";
import { useAchievementsStore } from "../../store/modules/achievements.js";
import showSnackbar from "../func/snackbar.js";
const achievementsStore = useAchievementsStore();
Expand Down
4 changes: 2 additions & 2 deletions src/pages/common/Config.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
<v-list class="config-list">
<v-list-subheader :inset="true" class="config-header" title="设置" />
<v-divider :inset="true" class="border-opacity-75" />
<v-list-item title="数据备份" @click="confirmBackup">
<v-list-item title="数据备份" @click="confirmBackup()">
<template #prepend>
<div class="config-icon">
<v-icon>mdi-database-export</v-icon>
</div>
</template>
</v-list-item>
<v-list-item title="数据恢复" @click="confirmRestore">
<v-list-item title="数据恢复" @click="confirmRestore()">
<template #prepend>
<div class="config-icon">
<v-icon>mdi-database-import</v-icon>
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/Sqlite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
import { app } from "@tauri-apps/api";
import Database from "tauri-plugin-sql-api";

import initDataSql from "./sql/initData";
import initDataSql from "./sql/initData.js";
import {
importAbyssData,
insertAbyssData,
insertAppData,
insertGameAccountData,
insertRecordData,
insertRoleData,
} from "./sql/insertData";
} from "./sql/insertData.js";

class Sqlite {
/**
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/Sqlite/modules/userGacha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* @since Beta v0.4.7
*/

import { AppCharacterData, AppWeaponData } from "../../../data/index";
import TGSqlite from "../index";
import { importUIGFData } from "../sql/updateData";
import { AppCharacterData, AppWeaponData } from "../../../data/index.js";
import TGSqlite from "../index.js";
import { importUIGFData } from "../sql/updateData.js";

type gachaItemTypeRes =
| ["角色", TGApp.App.Character.WikiBriefInfo]
Expand Down Expand Up @@ -34,7 +34,7 @@ function getGachaItemType(item_id: string): gachaItemTypeRes {
* @return {TGApp.Plugins.UIGF.GachaItem} 转换后的数据
*/
function transGacha(gacha: TGApp.Plugins.UIGF.GachaItem): TGApp.Plugins.UIGF.GachaItem {
const type = getGachaItemType(gacha.item_id);
const type = getGachaItemType(gacha.item_id!);
let res = gacha;
res.item_type = type[0];
if (type[0] === "角色") {
Expand Down
34 changes: 24 additions & 10 deletions src/utils/dataBS.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
/**
* @file utils/dataBS.ts
* @description 用户数据的备份、恢复、迁移
* @since Beta v0.4.7
* @since Beta v0.4.10
*/

import { fs, path } from "@tauri-apps/api";
import { FileEntry } from "@tauri-apps/api/fs";

import showSnackbar from "../components/func/snackbar";
import TGSqlite from "../plugins/Sqlite";
import showSnackbar from "../components/func/snackbar.js";
import TGSqlite from "../plugins/Sqlite/index.js";
import TSUserAchi from "../plugins/Sqlite/modules/userAchi.js";
import TSUserGacha from "../plugins/Sqlite/modules/userGacha.js";

import TGLogger from "./TGLogger.js";
import { exportUigfData, readUigfData, verifyUigfData } from "./UIGF.js";

/**
Expand Down Expand Up @@ -56,7 +58,9 @@ export async function restoreUserData(dir: string): Promise<void> {
});
return;
}
const files = (await fs.readDir(dir)).filter((item) => item.type === "File");
const filesRead = await fs.readDir(dir, { recursive: false });
const files: FileEntry[] = filesRead.filter((item: FileEntry) => item.path.endsWith(".json"));
await TGLogger.Info(`[DataBS][restoreUserData] files: ${JSON.stringify(files)}`);
// 恢复成就数据
const achiFind = files.find((item) => item.name === "UIAF.json");
if (achiFind) {
Expand All @@ -65,37 +69,45 @@ export async function restoreUserData(dir: string): Promise<void> {
await fs.readTextFile(achiFind.path),
);
await TSUserAchi.mergeUIAF(dataAchi);
await TGLogger.Info(`[DataBS][restoreUserData] 成就数据恢复成功`);
} catch (e) {
showSnackbar({
text: `成就数据恢复失败 ${e}`,
color: "error",
});
await TGLogger.Error(`[DataBS][restoreUserData] 成就数据恢复失败 ${e}`);
errNum++;
}
} else {
showSnackbar({
text: "成就数据恢复失败,备份文件不存在",
color: "warn",
});
await TGLogger.Warn(`[DataBS][restoreUserData] 未检测到成就数据备份文件`);
await new Promise((resolve) => setTimeout(resolve, 1500));
}
// 恢复 ck
const ckFind = files.find((item) => item.name === "cookie.json");
if (ckFind) {
try {
const dataCK = await fs.readTextFile(ckFind.path);
await TGSqlite.saveAppData("cookie", JSON.stringify(JSON.parse(dataCK)));
await TGLogger.Info(`[DataBS][restoreUserData] Cookie 数据恢复成功`);
} catch (e) {
showSnackbar({
text: `Cookie 数据恢复失败 ${e}`,
color: "error",
});
await TGLogger.Error(`[DataBS][restoreUserData] Cookie 数据恢复失败 ${e}`);
errNum++;
}
} else {
showSnackbar({
text: "Cookie 数据恢复失败,备份文件不存在",
color: "warn",
});
await TGLogger.Warn(`[DataBS][restoreUserData] 未检测到 Cookie 数据备份文件`);
await new Promise((resolve) => setTimeout(resolve, 1500));
}
// 恢复深渊数据
const abyssFind = files.find((item) => item.name === "abyss.json");
Expand All @@ -117,27 +129,29 @@ export async function restoreUserData(dir: string): Promise<void> {
text: "深渊数据恢复失败,备份文件不存在",
color: "warn",
});
await TGLogger.Warn(`[DataBS][restoreUserData] 未检测到深渊数据备份文件`);
await new Promise((resolve) => setTimeout(resolve, 1500));
}
// 恢复祈愿数据
const reg = /UIGF_(\d+).json/;
const dataGachaList = files.filter((item) => reg.test(item.name));
const dataGachaList = files.filter((item) => reg.test(item.path));
for (const item of dataGachaList) {
const check = await verifyUigfData(item.path);
if (!check) {
errNum++;
continue;
}
const data = await readUigfData(item.path);
const uid = data.info.uid;
try {
const data = await readUigfData(item.path);
const uid = data.info.uid;
for (const item of data.list) {
await TSUserGacha.mergeUIGF(uid, item);
}
await TSUserGacha.mergeUIGF(uid, data.list);
await TGLogger.Info(`[DataBS][restoreUserData] UID: ${uid} 祈愿数据恢复成功`);
} catch (e) {
showSnackbar({
text: `UID: ${uid} 祈愿数据恢复失败`,
color: "error",
});
await TGLogger.Error(`[DataBS][restoreUserData] UID: ${uid} 祈愿数据恢复失败 ${e}`);
errNum++;
}
}
Expand Down
2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { defineConfig } from "vite";
// import VueDevtools from "vite-plugin-vue-devtools";
import vuetify from "vite-plugin-vuetify";

import buildTimePlugin from "./src/utils/TGBuild";
import buildTimePlugin from "./src/utils/TGBuild.js";

// https://vitejs.dev/config/
export default defineConfig({
Expand Down

0 comments on commit 7ea47b4

Please sign in to comment.