From f8121d504c46d9f618b36dfc659cc78708c7bd78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9B=AE=E6=A3=83?= Date: Wed, 16 Oct 2024 18:41:07 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=E4=BF=AE=E5=A4=8D=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E5=88=9D=E5=A7=8B=E5=8C=96=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.vue | 14 ++++++++++++++ src/plugins/Sqlite/index.ts | 25 ++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) 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