From 1033aad7f38d483e3de126036a506dd8ec253bde Mon Sep 17 00:00:00 2001 From: BTMuli Date: Mon, 22 May 2023 16:09:13 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E6=94=AF=E6=8C=81=20Cookie=20?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=A4=87=E4=BB=BD&=E6=81=A2=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/Config.vue | 28 ++++++++++++++++++++-------- src/web/utils/backupData.ts | 20 ++++++++++++++++++++ src/web/utils/restoreData.ts | 24 ++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 src/web/utils/backupData.ts create mode 100644 src/web/utils/restoreData.ts diff --git a/src/pages/Config.vue b/src/pages/Config.vue index ecfa30fd..de0872c8 100644 --- a/src/pages/Config.vue +++ b/src/pages/Config.vue @@ -190,6 +190,8 @@ import { useAchievementsStore } from "../store/modules/achievements"; import { useUserStore } from "../store/modules/user"; // utils import { backupUiafData, restoreUiafData } from "../utils/UIAF"; +import { backupCookieData } from "../web/utils/backupData"; +import { restoreCookieData } from "../web/utils/restoreData"; import TGSqlite from "../utils/TGSqlite"; import TGRequest from "../web/request/TGRequest"; @@ -367,6 +369,8 @@ async function backupData () { loading.value = true; const achievements = await TGSqlite.getUIAF(); await backupUiafData(achievements); + const cookie = await TGSqlite.getCookie(); + await backupCookieData(cookie); loading.value = false; snackbarText.value = "数据已备份!"; snackbarColor.value = "success"; @@ -376,17 +380,25 @@ async function backupData () { async function restoreData () { loadingTitle.value = "正在恢复数据..."; loading.value = true; - const res = await restoreUiafData(); - loading.value = false; - if (res) { + const fail = []; + let res = await restoreUiafData(); + if (!res) { + fail.push("成就数据"); + } + res = await restoreCookieData(); + if (!res) { + fail.push("Cookie"); + } + if (fail.length > 0) { + snackbarText.value = `${fail.join("、")} 恢复失败!`; + snackbarColor.value = "error"; + } else { snackbarText.value = "数据已恢复!"; snackbarColor.value = "success"; - snackbar.value = true; - } else { - snackbarText.value = "未检测到备份数据!"; - snackbarColor.value = "error"; - snackbar.value = true; } + const cookie = await TGSqlite.getCookie(); + userStore.initCookie(cookie); + loading.value = false; } async function delTempData () { diff --git a/src/web/utils/backupData.ts b/src/web/utils/backupData.ts new file mode 100644 index 00000000..bee82819 --- /dev/null +++ b/src/web/utils/backupData.ts @@ -0,0 +1,20 @@ +/** + * @file web utils backupData.ts + * @description 数据备份 + * @author BTMuli + * @since Alpha v0.2.0 + */ + +// tauri +import { fs, path } from "@tauri-apps/api"; + +/** + * @description 备份 Cookie 数据 + * @since Alpha v0.2.0 + * @param {Record} cookie cookie + * @returns {Promise} + */ +export async function backupCookieData (cookie: Record): Promise { + const savePath = `${await path.appLocalDataDir()}\\userData\\cookie.json`; + await fs.writeTextFile(savePath, JSON.stringify(cookie, null, 2)); +} diff --git a/src/web/utils/restoreData.ts b/src/web/utils/restoreData.ts new file mode 100644 index 00000000..5fd04647 --- /dev/null +++ b/src/web/utils/restoreData.ts @@ -0,0 +1,24 @@ +/** + * @file web utils restoreData.ts + * @description 数据恢复 + * @author BTMuli + * @since Alpha v0.2.0 + */ + +// tauri +import { fs, path } from "@tauri-apps/api"; +// utils +import TGSqlite from "../../utils/TGSqlite"; + +/** + * @description 恢复 Cookie 数据 + * @since Alpha v0.2.0 + * @returns {Promise} + */ +export async function restoreCookieData (): Promise { + const cookiePath = `${await path.appLocalDataDir()}\\userData\\cookie.json`; + if (!await fs.exists(cookiePath)) return false; + const cookieData = await fs.readTextFile(cookiePath); + await TGSqlite.saveAppData("cookie", JSON.stringify(JSON.parse(cookieData))); + return true; +}