/** * @file store/modules/app.ts * @description App store module * @since Beta v0.6.8 */ import { path } from "@tauri-apps/api"; import { defineStore } from "pinia"; import { ref } from "vue"; import { getInitDeviceInfo } from "@/utils/toolFunc.js"; import { type AnnoLang, AnnoServer } from "@/web/request/hk4eReq.js"; // 用于存储用户数据的路径 const userDataDir: Readonly = `${await path.appLocalDataDir()}${path.sep()}userData`; // 用于存放数据库的路径 const dbDataPath: Readonly = `${await path.appConfigDir()}${path.sep()}TeyvatGuide.db`; // 用于存放日志的路径 const logDataDir: Readonly = await path.appLogDir(); export enum NewsTypeEnum { notice = "1", activity = "2", news = "3", } export type NewsType = keyof typeof NewsTypeEnum; export const useAppStore = defineStore( "app", () => { // 应用打包时间 const buildTime = ref(""); // 侧边栏设置 const sidebar = ref({ collapse: true }); // 开发者模式 const devMode = ref(false); // 应用主题 const theme = ref("default"); // 是否登录 const isLogin = ref(false); // 用户数据目录 const userDir = ref(userDataDir); // 数据库路径 const dbPath = ref>(dbDataPath); // 日志目录 const logDir = ref(logDataDir); // 游戏安装目录 const gameDir = ref("未设置"); // 设备信息 const deviceInfo = ref(getInitDeviceInfo()); // 服务器 const server = ref(AnnoServer.CN_ISLAND); // 语言 const lang = ref("zh-cn"); // 最近的咨讯类型 const recentNewsType = ref("notice"); // 是否开启分辨率回正 const needResize = ref("true"); // 分享图生成默认设置,为0表示默认保存到文件,为数字表示当大小超过xMB时保存到文件,否则保存到剪贴板 const shareDefaultFile = ref(10); // 图像压缩质量 const imageQualityPercent = ref(80); // 无痕浏览 const incognito = ref(true); // 初始化 function init(): void { devMode.value = false; theme.value = "default"; isLogin.value = false; sidebar.value.collapse = true; server.value = AnnoServer.CN_ISLAND; lang.value = "zh-cn"; recentNewsType.value = "notice"; needResize.value = "true"; gameDir.value = "未设置"; shareDefaultFile.value = 10; imageQualityPercent.value = 10; incognito.value = true; initDevice(); } function changeTheme(): void { if (theme.value === "default") theme.value = "dark"; else theme.value = "default"; } function initDevice(): void { deviceInfo.value = getInitDeviceInfo(); } function getImageUrl(url: string): string { if (url.endsWith(".gif") || imageQualityPercent.value === 100) return url; return `${url}?x-oss-process=image/format,jpg/quality,Q_${imageQualityPercent.value}`; } return { theme, buildTime, sidebar, devMode, deviceInfo, isLogin, userDir, dbPath, logDir, server, lang, recentNewsType, needResize, gameDir, shareDefaultFile, imageQualityPercent, incognito, init, changeTheme, getImageUrl, }; }, { persist: [ { key: "appPath", storage: window.localStorage, pick: ["userDir", "dbPath", "logDir", "gameDir"], }, { key: "app", storage: window.localStorage, pick: [ "devMode", "loading", "buildTime", "isLogin", "needResize", "shareDefaultFile", "imageQualityPercent", "incognito", ], }, { key: "sidebar", storage: window.localStorage, pick: ["sidebar"], }, { key: "theme", storage: window.localStorage, pick: ["theme", "server", "lang", "recentNewsType"], }, { key: "deviceInfo", storage: window.localStorage, pick: ["deviceInfo"], }, ], }, );