Files
fe.ems.vue3/src/store/modules/app.ts
2023-10-28 19:43:54 +08:00

150 lines
4.0 KiB
TypeScript

import { getSysConf } from '@/api';
import defaultLOGOIcon from '@/assets/logo_icon.png';
import defaultLOGOBrand from '@/assets/logo_brand.png';
import { RESULT_CODE_SUCCESS } from '@/constants/result-constants';
import { validHttp } from '@/utils/regular-utils';
import { defineStore } from 'pinia';
/**应用参数类型 */
type AppStore = {
/**应用名称 */
appName: string;
/**应用标识 */
appCode: string;
/**应用版本 */
appVersion: string;
/**应用版权声明 */
copyright: string;
/**LOGO显示类型 */
logoType: 'brand' | 'icon';
/**LOGO文件路径 */
filePathIcon: string;
filePathBrand: string;
/**开启用户注册 */
registerUser: boolean;
/**登录界面背景 */
loginBackground: 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,
copyright: 'Copyright ©2023 For AGrand 千通科技',
logoType: 'icon',
filePathIcon: '',
filePathBrand: '',
registerUser: false,
loginBackground: '',
}),
getters: {
/**
* 获取正确LOGO地址-icon
* @param state 内部属性不用传入
* @returns LOGO地址url
*/
getLOGOIcon(state) {
const path = state.filePathIcon;
if (!path) {
return defaultLOGOIcon;
}
if (validHttp(path)) {
return path;
}
const baseApi = import.meta.env.VITE_API_BASE_URL;
return `${baseApi}${path}`;
},
/**
* 获取正确LOGO地址-brand
* @param state 内部属性不用传入
* @returns LOGO地址url
*/
getLOGOBrand(state) {
const path = state.filePathBrand;
if (!path) {
return defaultLOGOBrand;
}
if (validHttp(path)) {
return path;
}
const baseApi = import.meta.env.VITE_API_BASE_URL;
return `${baseApi}${path}`;
},
/**
* 获取正确登录背景地址
* @param state 内部属性不用传入
* @returns 背景地址url
*/
getLoginBackground(state) {
const path = state.loginBackground;
if (!path) {
return '';
}
if (validHttp(path)) {
return path;
}
const baseApi = import.meta.env.VITE_API_BASE_URL;
return `${baseApi}${path}`;
},
},
actions: {
/**设置网页标题 */
setTitle(title?: string) {
if (title) {
document.title = `${title} - ${this.appName}`;
} else {
document.title = this.appName;
}
},
/**设置版权声明 */
setCopyright(text: string) {
this.copyright = text;
},
/**设置LOGO */
setLOGO(type: 'brand' | 'icon', filePath: string) {
this.logoType = type;
if (type === 'brand') {
this.filePathBrand = filePath;
}
if (type === 'icon') {
this.filePathIcon = filePath;
}
},
// 获取系统配置信息
async fnSysConf() {
const res = await getSysConf();
if (res.code === RESULT_CODE_SUCCESS && res.data) {
if (res.data.title) {
this.appName = res.data.title;
}
if (res.data.copyright) {
this.copyright = res.data.copyright;
}
this.filePathIcon = res.data.filePathIcon;
this.filePathBrand = res.data.filePathBrand;
if (res.data.logoType) {
this.logoType = res.data.logoType;
// 修改html内容
const iconDom = document.querySelector("link[rel~='icon']");
if (iconDom) {
if (this.logoType === 'icon') {
iconDom.setAttribute('href', this.getLOGOIcon);
}
if (this.logoType === 'brand') {
iconDom.setAttribute('href', this.getLOGOBrand);
}
}
}
this.registerUser = res.data.registerUser === 'true';
if (res.data.loginBackground) {
this.loginBackground = res.data.loginBackground;
}
}
return res;
},
},
});
export default useAppStore;