From 1d8eec76ec4b7247cbca5b07c5789b259223bed1 Mon Sep 17 00:00:00 2001 From: BTMuli Date: Sun, 9 Apr 2023 21:12:27 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=A6=20fix(ck):=20cookie=20=E7=94=A8=20?= =?UTF-8?q?store=20=E6=9D=A5=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/Config.vue | 13 ++++++---- src/store/modules/hk4e.ts | 54 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 src/store/modules/hk4e.ts diff --git a/src/pages/Config.vue b/src/pages/Config.vue index 33fa7d30..0752d887 100644 --- a/src/pages/Config.vue +++ b/src/pages/Config.vue @@ -140,6 +140,7 @@ // vue import { onMounted, ref } from "vue"; import { getBuildTime } from "../utils/TGBuild"; + import TLoading from "../components/t-loading.vue"; import TConfirm from "../components/t-confirm.vue"; // tauri @@ -147,6 +148,7 @@ import { dialog, fs, app, os, tauri } from "@tauri-apps/api"; // store import { useAppStore } from "../store/modules/app"; import { useHomeStore } from "../store/modules/home"; +import { useHk4eStore } from "../store/modules/hk4e"; import { useAchievementsStore } from "../store/modules/achievements"; // utils import { WriteTGData } from "../utils/TGIndex"; @@ -172,6 +174,7 @@ const loading = ref(true as boolean); // data const showHome = ref(homeStore.getShowValue() as string[]); +const hk4eStore = useHk4eStore(); // snackbar const snackbar = ref(false as boolean); @@ -329,9 +332,10 @@ function submitHome () { // 获取 Cookie async function readCookie () { + const cookie = hk4eStore.getCookie(); // todo 验证 cookie 是否有效 - const cookie = await tauri.invoke("read_cookie") as string || document.cookie; - if (cookie === null || cookie === "") { + const tryReadCookie = await tauri.invoke("read_cookie") as string || cookie; + if (cookie === null || tryReadCookie === "") { snackbarText.value = "Cookie 为空!"; snackbarColor.value = "error"; snackbar.value = true; @@ -339,10 +343,9 @@ async function readCookie () { snackbarText.value = "Cookie 获取成功!"; snackbarColor.value = "success"; snackbar.value = true; - alert(cookie); + hk4eStore.setCookie(tryReadCookie); + alert(`Cookie 获取成功!\n\n${tryReadCookie}`); } - // 将 cookie 保存到本地 - document.cookie = cookie; } diff --git a/src/store/modules/hk4e.ts b/src/store/modules/hk4e.ts new file mode 100644 index 00000000..987f23df --- /dev/null +++ b/src/store/modules/hk4e.ts @@ -0,0 +1,54 @@ +/** + * @file store modules hk4e.ts + * @description store modules hk4e.ts + * @author BTMuli + * @since Alpha v0.1.2 + */ + +// vue +import { ref } from "vue"; +// pinia +import { defineStore } from "pinia"; + +export const useHk4eStore = defineStore( + "hk4e", + () => { + // cookie + const cookie = ref(""); + // 解析后的 cookie + const cookieParsed = ref>({}); + + // getCookie + function getCookie (): string { + return cookie.value; + } + + // 获取具体的 cookie + function getCookieItem (key: string): string { + return cookieParsed.value[key]; + } + + // setCookie + function setCookie (value: string): void { + cookie.value = value; + // 解析 cookie + const cookieObj: Record = {}; + value.split(";").forEach((item) => { + const [key, value] = item.split("="); + cookieObj[key.trim()] = value; + }); + // 保存 cookie + localStorage.setItem("hk4eCookie", JSON.stringify(cookieObj)); + cookieParsed.value = cookieObj; + } + + return { + getCookie, + setCookie, + getCookieItem, + }; + }, + { + persist: true, + }, +);