mirror of
https://github.com/BTMuli/TeyvatGuide.git
synced 2025-12-12 09:18:14 +08:00
fix(data): 优化数据加载逻辑
This commit is contained in:
25
src/App.vue
25
src/App.vue
@@ -14,11 +14,36 @@
|
||||
<script lang="ts">
|
||||
import { defineComponent } from "vue";
|
||||
import TSidebar from "./components/t-sidebar.vue";
|
||||
import useAppStore from "./store/modules/app";
|
||||
import TGAppData from "./data";
|
||||
import { fs } from "@tauri-apps/api";
|
||||
import { BaseDirectory } from "@tauri-apps/api/fs";
|
||||
|
||||
export default defineComponent({
|
||||
name: "App",
|
||||
components: {
|
||||
TSidebar,
|
||||
},
|
||||
async mounted() {
|
||||
const appStore = useAppStore();
|
||||
if (!appStore.loading) {
|
||||
try {
|
||||
await fs.readDir(`${appStore.dataPath.app}`);
|
||||
await fs.readDir(`${appStore.dataPath.user}`);
|
||||
} catch (e) {
|
||||
await fs.createDir("appData", { dir: BaseDirectory.AppLocalData });
|
||||
await fs.createDir("userData", { dir: BaseDirectory.AppLocalData });
|
||||
}
|
||||
await console.log("检测到数据未加载,开始加载数据...");
|
||||
TGAppData.map(async item => {
|
||||
await fs.writeFile(
|
||||
`${appStore.dataPath.app}\\${item.name}`,
|
||||
JSON.stringify(item.data, null, 2)
|
||||
);
|
||||
});
|
||||
appStore.loading = true;
|
||||
await console.log("数据加载完成!");
|
||||
}
|
||||
},
|
||||
});
|
||||
</script>
|
||||
|
||||
@@ -2,12 +2,6 @@
|
||||
<v-card>
|
||||
<v-card-title>配置</v-card-title>
|
||||
<v-list>
|
||||
<v-list-item @click="checkData" prepend-icon="mdi-folder">
|
||||
<v-list-item-title>检测数据</v-list-item-title>
|
||||
</v-list-item>
|
||||
<v-list-item @click="setAppData" prepend-icon="mdi-folder">
|
||||
<v-list-item-title>导入数据</v-list-item-title>
|
||||
</v-list-item>
|
||||
<v-list-item @click="openUserData" prepend-icon="mdi-folder">
|
||||
<v-list-item-title>打开用户数据目录</v-list-item-title>
|
||||
</v-list-item>
|
||||
@@ -19,101 +13,22 @@
|
||||
</v-list-item>
|
||||
</v-list>
|
||||
</v-card>
|
||||
<v-card>
|
||||
<v-card-title>相关路径</v-card-title>
|
||||
<v-list>
|
||||
<v-list-item>
|
||||
<v-list-item-title>用户数据目录</v-list-item-title>
|
||||
<v-list-item-subtitle>{{ appStore.dataPath.user }}</v-list-item-subtitle>
|
||||
</v-list-item>
|
||||
<v-list-item>
|
||||
<v-list-item-title>应用数据目录</v-list-item-title>
|
||||
<v-list-item-subtitle>{{ appStore.dataPath.app }}</v-list-item-subtitle>
|
||||
</v-list-item>
|
||||
<v-list-item>
|
||||
<v-list-item-title>appDir</v-list-item-title>
|
||||
<v-list-item-subtitle>{{ tauriPath.appDir }}</v-list-item-subtitle>
|
||||
</v-list-item>
|
||||
<v-list-item>
|
||||
<v-list-item-title>cacheDir</v-list-item-title>
|
||||
<v-list-item-subtitle>{{ tauriPath.cacheDir }}</v-list-item-subtitle>
|
||||
</v-list-item>
|
||||
<v-list-item>
|
||||
<v-list-item-title>configDir</v-list-item-title>
|
||||
<v-list-item-subtitle>{{ tauriPath.configDir }}</v-list-item-subtitle>
|
||||
</v-list-item>
|
||||
<v-list-item>
|
||||
<v-list-item-title>dataDir</v-list-item-title>
|
||||
<v-list-item-subtitle>{{ tauriPath.dataDir }}</v-list-item-subtitle>
|
||||
</v-list-item>
|
||||
<v-list-item>
|
||||
<v-list-item-title>executableDir</v-list-item-title>
|
||||
<v-list-item-subtitle>{{ tauriPath.executableDir }}</v-list-item-subtitle>
|
||||
</v-list-item>
|
||||
<v-list-item>
|
||||
<v-list-item-title>homeDir</v-list-item-title>
|
||||
<v-list-item-subtitle>{{ tauriPath.homeDir }}</v-list-item-subtitle>
|
||||
</v-list-item>
|
||||
</v-list>
|
||||
</v-card>
|
||||
</template>
|
||||
|
||||
<script lang="ts">
|
||||
import { defineComponent } from "vue";
|
||||
import useAppStore from "../store/modules/app";
|
||||
import { dialog, fs, path, notification } from "@tauri-apps/api";
|
||||
import { dialog, fs } from "@tauri-apps/api";
|
||||
import { BaseDirectory } from "@tauri-apps/api/fs";
|
||||
import TauriGenshinData from "../data";
|
||||
export default defineComponent({
|
||||
name: "Config",
|
||||
async mounted() {
|
||||
await this.getTauriPath();
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
source: "本地",
|
||||
appStore: useAppStore(),
|
||||
tauriPath: {
|
||||
appDir: "",
|
||||
cacheDir: "",
|
||||
configDir: "",
|
||||
dataDir: "",
|
||||
executableDir: "",
|
||||
homeDir: "",
|
||||
},
|
||||
};
|
||||
},
|
||||
methods: {
|
||||
// 检测本地数据
|
||||
async checkData() {
|
||||
if (await this.checkDir()) {
|
||||
await notification.sendNotification("数据目录不为空,检测成功");
|
||||
} else {
|
||||
await notification.sendNotification("数据目录为空,正在导入数据...");
|
||||
await this.setAppData();
|
||||
}
|
||||
},
|
||||
// 检查数据目录是否为空
|
||||
async checkDir() {
|
||||
const appStore = useAppStore();
|
||||
try {
|
||||
await fs.readDir(`${appStore.dataPath.app}`);
|
||||
await fs.readDir(`${appStore.dataPath.user}`);
|
||||
return true;
|
||||
} catch (e) {
|
||||
await fs.createDir("appData", { dir: BaseDirectory.AppLocalData });
|
||||
await fs.createDir("userData", { dir: BaseDirectory.AppLocalData });
|
||||
return false;
|
||||
}
|
||||
},
|
||||
// 导入数据
|
||||
async setAppData() {
|
||||
const appStore = useAppStore();
|
||||
TauriGenshinData.map(async item => {
|
||||
await fs.writeFile(`${appStore.dataPath.app}\\${item.name}`, item.data);
|
||||
});
|
||||
await dialog.message("数据导入成功");
|
||||
},
|
||||
// 打开数据文件夹
|
||||
async openUserData() {
|
||||
const appStore = useAppStore();
|
||||
@@ -140,23 +55,10 @@ export default defineComponent({
|
||||
const res = await dialog.confirm("确定要恢复默认配置吗?");
|
||||
if (res) {
|
||||
const appStore = useAppStore();
|
||||
const appLocalDataDir = await path.appLocalDataDir();
|
||||
appStore.dataPath = {
|
||||
app: `${appLocalDataDir}appData`,
|
||||
user: `${appLocalDataDir}userData`,
|
||||
};
|
||||
await appStore.init();
|
||||
await dialog.message("已恢复默认配置!");
|
||||
}
|
||||
},
|
||||
// 相关路径
|
||||
async getTauriPath() {
|
||||
this.tauriPath.appDir = await path.appDir();
|
||||
this.tauriPath.cacheDir = await path.cacheDir();
|
||||
this.tauriPath.configDir = await path.configDir();
|
||||
this.tauriPath.dataDir = await path.dataDir();
|
||||
this.tauriPath.executableDir = await path.executableDir();
|
||||
this.tauriPath.homeDir = await path.homeDir();
|
||||
},
|
||||
},
|
||||
});
|
||||
</script>
|
||||
|
||||
@@ -160,13 +160,16 @@ export default defineComponent({
|
||||
// 输出成就系列数据
|
||||
await console.log("正在输出成就系列数据...");
|
||||
await fs.writeFile(
|
||||
`${appDataDir}\\achievement.json`,
|
||||
JSON.stringify(achievementSeries)
|
||||
`${appDataDir}\\achievementSeries.json`,
|
||||
JSON.stringify(achievementSeries, null, 2)
|
||||
);
|
||||
await console.log("输出成就系列数据成功!");
|
||||
// 输出成就数据
|
||||
await console.log("正在输出成就数据...");
|
||||
await fs.writeFile(`${appDataDir}\\achievements.json`, JSON.stringify(achievement));
|
||||
await fs.writeFile(
|
||||
`${appDataDir}\\achievements.json`,
|
||||
JSON.stringify(achievement, null, 2)
|
||||
);
|
||||
await console.log("输出成就数据成功!");
|
||||
},
|
||||
},
|
||||
|
||||
@@ -8,16 +8,45 @@ const useAppStore = defineStore({
|
||||
id: "app",
|
||||
state: () => {
|
||||
return {
|
||||
// 是否加载数据
|
||||
loading: false,
|
||||
// 侧边栏设置
|
||||
sidebar: {
|
||||
expand: true,
|
||||
},
|
||||
// 数据路径
|
||||
dataPath: {
|
||||
app: appDataDir,
|
||||
user: userDataDir,
|
||||
},
|
||||
// 应用数据路径
|
||||
appPath: {
|
||||
achievements: `${appDataDir}\\achievements.json`,
|
||||
achievementSeries: `${appDataDir}\\achievementSeries.json`,
|
||||
},
|
||||
};
|
||||
},
|
||||
actions: {
|
||||
// 初始化配置
|
||||
async init() {
|
||||
// 初始化加载状态
|
||||
this.loading = false;
|
||||
// 初始化侧边栏设置
|
||||
this.sidebar = {
|
||||
expand: true,
|
||||
};
|
||||
// 初始化数据路径
|
||||
this.dataPath = {
|
||||
app: appDataDir,
|
||||
user: userDataDir,
|
||||
};
|
||||
// 初始化应用数据路径
|
||||
this.appPath = {
|
||||
achievements: `${appDataDir}\\achievements.json`,
|
||||
achievementSeries: `${appDataDir}\\achievementSeries.json`,
|
||||
};
|
||||
},
|
||||
},
|
||||
persist: true,
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user