fix(data): 优化数据加载逻辑

This commit is contained in:
BTMuli
2023-03-08 19:46:38 +08:00
parent fcafaeec44
commit 3bb1be7bad
4 changed files with 62 additions and 103 deletions

View File

@@ -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>