diff --git a/src/App.vue b/src/App.vue index 8cc3fad7..944bebf2 100644 --- a/src/App.vue +++ b/src/App.vue @@ -100,6 +100,7 @@ onMounted(async () => { function listenOnInit(): void { console.info("[App][listenOnInit] 监听初始化事件!"); event.listen("initApp", async () => { + await checkAppLoad(); await checkDeviceFp(); try { await checkUserLoad(); @@ -112,6 +113,19 @@ function listenOnInit(): void { }); } +async function checkAppLoad(): Promise { + let checkDB = false; + try { + checkDB = await TGSqlite.check(); + } catch (error) { + if (error instanceof Error) { + await TGLogger.Error(`[App][checkAppLoad] ${error.name}: ${error.message}`); + } else console.error(error); + } + if (!checkDB) await TGSqlite.update(); + else await TGLogger.Info("[App][checkAppLoad] 数据库已成功加载!"); +} + // 检测 deviceFp async function checkDeviceFp(): Promise { const appData = await TGSqlite.getAppData(); diff --git a/src/plugins/Sqlite/index.ts b/src/plugins/Sqlite/index.ts index 6ce83e82..8f254a14 100644 --- a/src/plugins/Sqlite/index.ts +++ b/src/plugins/Sqlite/index.ts @@ -1,12 +1,14 @@ /** * @file plugins/Sqlite/index.ts * @description Sqlite 数据库操作类 - * @since Beta v0.6.0 + * @since Beta v0.6.1 */ import { app } from "@tauri-apps/api"; import Database from "@tauri-apps/plugin-sql"; +import TGLogger from "../../utils/TGLogger.js"; + import initDataSql from "./sql/initData.js"; import { insertAppData } from "./sql/insertData.js"; @@ -55,6 +57,27 @@ class Sqlite { return this.db; } + /** + * @description 检测是否需要创建数据库 + * @since Beta v0.6.1 + * @returns {Promise} + */ + public async check(): Promise { + try { + const db = await this.getDB(); + let isVerified = false; + const sqlT = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;"; + const res: Array<{ name: string }> = await db.select(sqlT); + if (this.tables.every((item) => res.map((i) => i.name).includes(item))) { + isVerified = true; + } + return isVerified; + } catch (e) { + await TGLogger.Error(JSON.stringify(e)); + return false; + } + } + /** * @description 初始化数据库 * @since Beta v0.4.5