import { getSysConf } from '@/api'; import { CACHE_LOCAL_I18N } from '@/constants/cache-keys-constants'; import { RESULT_CODE_SUCCESS } from '@/constants/result-constants'; import { removeToken } from '@/plugins/auth-token'; import { parseUrlPath } from '@/plugins/file-static-url'; import { localGet, localSet } from '@/utils/cache-local-utils'; import { defineStore } from 'pinia'; /**应用参数类型 */ type AppStore = { /**应用名称 */ appName: string; /**应用标识 */ appCode: string; /**应用版本 */ appVersion: string; /**服务版本 */ version: string; buildTime: string; /**系统引导使用 */ bootloader: boolean; // 用户登录认证 loginAuth: boolean; // 序列号 serialNum: string; /**应用版权声明 */ copyright: string; /**LOGO显示类型 */ logoType: 'brand' | 'icon'; /**LOGO文件路径 */ filePathIcon: string; filePathBrand: string; /**开启用户注册 */ registerUser: boolean; /**登录界面背景 */ loginBackground: string; /**系统使用手册 */ helpDoc: string; /**官方网址 */ officialUrl: string; /**国际化切换 */ i18nOpen: boolean; /**国际化默认语言 */ i18nDefault: string; }; const useAppStore = defineStore('app', { state: (): AppStore => ({ appName: import.meta.env.VITE_APP_NAME, appCode: import.meta.env.VITE_APP_CODE, appVersion: import.meta.env.VITE_APP_VERSION, version: `-`, buildTime: `-`, bootloader: false, loginAuth: false, serialNum: `-`, copyright: `Copyright ©2023 For ${import.meta.env.VITE_APP_NAME}`, logoType: 'icon', filePathIcon: '', filePathBrand: '', registerUser: false, loginBackground: '', helpDoc: '', officialUrl: '', i18nOpen: false, i18nDefault: 'en_US', }), getters: {}, actions: { /**设置网页标题 */ setTitle(title?: string) { if (title && title.indexOf('router.') === -1) { document.title = `${title} - ${this.appName}`; } else { document.title = this.appName; } }, // 获取系统配置信息 async fnSysConf() { const res = await getSysConf(); if (res.code === RESULT_CODE_SUCCESS && res.data) { this.version = res.data.version; this.buildTime = res.data.buildTime; this.bootloader = res.data.bootloader === 'true'; // 引导时 if (this.bootloader) { removeToken(); } this.loginAuth = res.data.loginAuth === 'true'; this.serialNum = res.data.serialNum; this.appName = res.data.title; this.copyright = res.data.copyright; this.logoType = res.data.logoType; this.filePathIcon = res.data.filePathIcon; this.filePathBrand = res.data.filePathBrand; // 修改html内容-小图当作favicon.ico if (this.logoType) { const iconDom = document.querySelector("link[rel~='icon']"); if (iconDom) { let url = parseUrlPath(this.filePathIcon); // 语言参数替换 if (url.indexOf('{language}') !== -1) { const local = localGet(CACHE_LOCAL_I18N) || 'en_US'; const lang = local.split('_')[0]; url = url.replace('{language}', lang); } iconDom.setAttribute('href', url); } } this.registerUser = res.data.registerUser === 'true'; this.loginBackground = res.data.loginBackground; this.helpDoc = res.data.helpDoc; this.officialUrl = res.data.officialUrl; this.i18nOpen = res.data.i18nOpen === 'true'; this.i18nDefault = res.data.i18nDefault; // 切换禁用时,设置默认语言 const localI18n = localGet(CACHE_LOCAL_I18N); if (localI18n == null || (!this.i18nOpen && this.i18nDefault)) { localSet(CACHE_LOCAL_I18N, this.i18nDefault); } } return res; }, }, }); export default useAppStore;