mirror of
https://github.com/BTMuli/TeyvatGuide.git
synced 2025-12-13 09:28:14 +08:00
feat(TGIndex): 完成数据库的数据写入
This commit is contained in:
76
src/App.vue
76
src/App.vue
@@ -15,9 +15,11 @@
|
||||
import TSidebar from "./components/t-sidebar.vue";
|
||||
import useAppStore from "./store/modules/app";
|
||||
import { TGAppDataList } from "./data";
|
||||
import { getDataList } from "./data/init";
|
||||
import { fs } from "@tauri-apps/api";
|
||||
import { BaseDirectory } from "@tauri-apps/api/fs";
|
||||
import { onMounted } from "vue";
|
||||
import { InitTGData, DeleteTGData, WriteTGData } from "./utils/TGIndex";
|
||||
|
||||
const appStore = useAppStore();
|
||||
|
||||
@@ -25,42 +27,44 @@ onMounted(async () => {
|
||||
await checkLoad();
|
||||
});
|
||||
async function checkLoad() {
|
||||
if (!appStore.loading) {
|
||||
try {
|
||||
await fs.readDir(`${appStore.dataPath.app}`);
|
||||
} catch (e) {
|
||||
await fs.createDir("appData", { dir: BaseDirectory.AppLocalData });
|
||||
}
|
||||
try {
|
||||
await fs.readDir(`${appStore.dataPath.user}`);
|
||||
} catch (e) {
|
||||
await fs.createDir("userData", { dir: BaseDirectory.AppLocalData });
|
||||
}
|
||||
try {
|
||||
await fs.readDir(`${appStore.dataPath.merge}`);
|
||||
} catch (e) {
|
||||
await fs.createDir("mergeData", { dir: BaseDirectory.AppLocalData });
|
||||
}
|
||||
try {
|
||||
await fs.readDir(`${appStore.dataPath.temp}`);
|
||||
} catch (e) {
|
||||
await fs.createDir("tempData", { dir: BaseDirectory.AppLocalData });
|
||||
}
|
||||
console.log("检测到数据未加载,开始加载数据...");
|
||||
TGAppDataList.AppData.map(async item => {
|
||||
await fs.writeFile(
|
||||
`${appStore.dataPath.app}\\${item.name}`,
|
||||
JSON.stringify(item.data, null, 4)
|
||||
);
|
||||
});
|
||||
TGAppDataList.MergeData.map(async item => {
|
||||
await fs.writeFile(
|
||||
`${appStore.dataPath.merge}\\${item.name}`,
|
||||
JSON.stringify(item.data, null, 4)
|
||||
);
|
||||
});
|
||||
appStore.loading = true;
|
||||
console.log("数据加载完成!");
|
||||
if (appStore.loading) {
|
||||
console.log("数据已加载!");
|
||||
return;
|
||||
}
|
||||
await DeleteTGData();
|
||||
await createDataDir();
|
||||
await writeData();
|
||||
await writeIndex();
|
||||
appStore.loading = true;
|
||||
console.log("数据加载完成!");
|
||||
}
|
||||
// 创建数据文件夹
|
||||
async function createDataDir() {
|
||||
console.log("开始创建数据文件夹...");
|
||||
await fs.createDir("appData", { dir: BaseDirectory.AppLocalData, recursive: true });
|
||||
await fs.createDir("userData", { dir: BaseDirectory.AppLocalData, recursive: true });
|
||||
await fs.createDir("mergeData", { dir: BaseDirectory.AppLocalData, recursive: true });
|
||||
await fs.createDir("tempData", { dir: BaseDirectory.AppLocalData, recursive: true });
|
||||
console.log("数据文件夹创建完成!");
|
||||
}
|
||||
// 将数据写入文件夹
|
||||
async function writeData() {
|
||||
console.log("开始写入数据...");
|
||||
TGAppDataList.AppData.map(async item => {
|
||||
await fs.writeFile(`${appStore.dataPath.app}\\${item.name}`, JSON.stringify(item.data));
|
||||
});
|
||||
TGAppDataList.MergeData.map(async item => {
|
||||
await fs.writeFile(`${appStore.dataPath.merge}\\${item.name}`, JSON.stringify(item.data));
|
||||
});
|
||||
console.log("数据写入完成!");
|
||||
}
|
||||
// 写入 IndexedDB
|
||||
async function writeIndex() {
|
||||
console.log("开始写入 IndexedDB...");
|
||||
await InitTGData();
|
||||
getDataList.map(async item => {
|
||||
await WriteTGData(item.name, item.data);
|
||||
});
|
||||
console.log("IndexedDB 写入完成!");
|
||||
}
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user