Files
fe.ems.vue3/src/store/modules/app.ts

140 lines
4.3 KiB
TypeScript

import { getSysConf } from '@/api';
import {
CACHE_LOCAL_I18N,
CACHE_SESSION_CRYPTO_API,
} from '@/constants/cache-keys-constants';
import { RESULT_CODE_SUCCESS } from '@/constants/result-constants';
import { delAccessToken, delRefreshToken } from '@/plugins/auth-token';
import { parseUrlPath } from '@/plugins/file-static-url';
import { localGet, localSet } from '@/utils/cache-local-utils';
import { sessionSet } from '@/utils/cache-session-utils';
import { defineStore } from 'pinia';
/**应用参数类型 */
type AppStore = {
/**应用名称 */
appName: string;
/**应用标识 */
appCode: string;
/**应用版本 */
appVersion: string;
/**版本号 */
version: string;
/**系统引导使用 */
bootloader: boolean;
/**服务类型 oc 单核心网 mc 多核心网 tc 租户核心网 */
serverType: string;
// 用户登录认证
loginAuth: boolean;
// 用户接口加密
cryptoApi: 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: '-',
bootloader: false,
serverType: '-',
loginAuth: true,
cryptoApi: true,
serialNum: '-',
copyright: `Copyright ©2023-2025 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.serverType = res.data.serverType;
this.bootloader = res.data.bootloader === 'true';
// 引导时
if (this.bootloader) {
delAccessToken();
delRefreshToken();
}
this.loginAuth = res.data.loginAuth !== 'false';
this.cryptoApi = res.data.cryptoApi !== 'false';
sessionSet(CACHE_SESSION_CRYPTO_API, res.data.cryptoApi);
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);
}
}
},
},
});
export default useAppStore;