Files
certd/packages/ui/certd-client/src/store/modules/settings.ts
T

234 lines
6.3 KiB
TypeScript
Raw Normal View History

import { defineStore } from "pinia";
2025-03-06 21:11:07 +08:00
import { Modal, notification } from "ant-design-vue";
2024-11-08 23:43:19 +08:00
import * as _ from "lodash-es";
// @ts-ignore
import { LocalStorage } from "/src/utils/util.storage";
2024-06-16 02:47:41 +08:00
import * as basicApi from "/@/api/modules/api.basic";
2024-12-25 00:52:39 +08:00
import { HeaderMenus, PlusInfo, SiteEnv, SiteInfo, SuiteSetting, SysInstallInfo, SysPublicSetting } from "/@/api/modules/api.basic";
2024-09-24 02:42:08 +08:00
import { useUserStore } from "/@/store/modules/user";
import { mitter } from "/@/utils/util.mitt";
2024-10-05 01:46:25 +08:00
import { env } from "/@/utils/util.env";
2025-03-07 18:01:51 +08:00
import { updatePreferences } from "/@/vben/preferences";
2025-03-09 01:14:44 +08:00
import { useTitle } from "@vueuse/core";
import { utils } from "/@/utils";
import { cloneDeep } from "lodash-es";
export interface SettingState {
sysPublic?: SysPublicSetting;
2024-08-25 01:55:34 +08:00
installInfo?: {
siteId: string;
2024-09-22 02:06:34 +08:00
installTime?: number;
2024-09-24 02:42:08 +08:00
bindUserId?: number;
bindUrl?: string;
accountServerBaseUrl?: string;
appKey?: string;
};
2024-10-05 01:46:25 +08:00
siteInfo: SiteInfo;
plusInfo?: PlusInfo;
2024-10-10 18:38:22 +08:00
siteEnv?: SiteEnv;
2024-10-25 23:56:24 +08:00
headerMenus?: HeaderMenus;
2024-10-12 14:59:12 +08:00
inited?: boolean;
2024-12-25 00:52:39 +08:00
suiteSetting?: SuiteSetting;
}
2024-10-12 16:49:49 +08:00
const defaultSiteInfo: SiteInfo = {
2024-10-05 01:46:25 +08:00
title: env.TITLE || "Certd",
slogan: env.SLOGAN || "让你的证书永不过期",
logo: env.LOGO || "/static/images/logo/logo.svg",
loginLogo: env.LOGIN_LOGO || "/static/images/logo/rect-block.svg",
licenseTo: "",
licenseToUrl: ""
};
export const useSettingStore = defineStore({
id: "app.setting",
state: (): SettingState => ({
2024-10-05 01:46:25 +08:00
plusInfo: {
isPlus: false,
2024-10-12 14:59:12 +08:00
vipType: "free",
isComm: false
2024-10-05 01:46:25 +08:00
},
sysPublic: {
registerEnabled: false,
managerOtherUserPipeline: false,
2024-10-12 16:49:49 +08:00
icpNo: env.ICP_NO || ""
2024-08-25 01:55:34 +08:00
},
installInfo: {
2024-09-24 02:42:08 +08:00
siteId: "",
bindUserId: null,
bindUrl: "",
accountServerBaseUrl: "",
appKey: ""
},
2024-10-10 18:38:22 +08:00
siteInfo: defaultSiteInfo,
siteEnv: {
agent: {
enabled: undefined,
contactText: "",
contactLink: ""
}
2024-10-12 14:59:12 +08:00
},
2024-10-25 23:56:24 +08:00
headerMenus: {
menus: []
},
2024-12-25 00:52:39 +08:00
suiteSetting: { enabled: false },
2024-10-12 14:59:12 +08:00
inited: false
}),
getters: {
getSysPublic(): SysPublicSetting {
return this.sysPublic;
2024-08-25 01:55:34 +08:00
},
getInstallInfo(): SysInstallInfo {
return this.installInfo;
2024-10-05 01:46:25 +08:00
},
isPlus(): boolean {
return this.plusInfo?.isPlus && this.plusInfo?.expireTime > new Date().getTime();
},
isComm(): boolean {
return this.plusInfo?.isComm && this.plusInfo?.expireTime > new Date().getTime();
},
2024-10-12 14:59:12 +08:00
isAgent(): boolean {
return this.siteEnv?.agent?.enabled === true;
},
isCommOrAgent() {
return this.isComm || this.isAgent;
},
2024-10-05 01:46:25 +08:00
vipLabel(): string {
const vipLabelMap: any = {
2024-10-23 16:33:53 +08:00
free: "基础版",
2024-10-05 01:46:25 +08:00
plus: "专业版",
comm: "商业版"
};
return vipLabelMap[this.plusInfo?.vipType || "free"];
2024-10-27 02:51:56 +08:00
},
getHeaderMenus(): any[] {
2024-11-06 01:17:36 +08:00
// @ts-ignore
let menus = this.headerMenus?.menus || [];
menus = cloneDeep(menus);
return utils.tree.treeMap(menus, (menu: any) => {
return {
...menu,
name: menu.title,
2025-03-11 01:07:44 +08:00
path: menu.path ?? "/" + menu.title,
meta: {
title: menu.title,
icon: menu.icon,
link: menu.path,
order: 99999
}
};
});
2024-12-25 00:52:39 +08:00
},
2024-12-25 17:05:24 +08:00
isSuiteEnabled(): boolean {
2024-12-25 00:52:39 +08:00
// @ts-ignore
return this.suiteSetting?.enabled === true;
}
},
actions: {
2024-10-05 01:46:25 +08:00
checkPlus() {
if (!this.isPlus) {
notification.warn({
message: "此为专业版功能,请先升级到专业版"
});
throw new Error("此为专业版功能,请升级到专业版");
}
},
async loadSysSettings() {
2024-10-12 14:59:12 +08:00
const allSettings = await basicApi.loadAllSettings();
_.merge(this.sysPublic, allSettings.sysPublic || {});
_.merge(this.installInfo, allSettings.installInfo || {});
_.merge(this.siteEnv, allSettings.siteEnv || {});
_.merge(this.plusInfo, allSettings.plusInfo || {});
2024-10-27 02:51:56 +08:00
_.merge(this.headerMenus, allSettings.headerMenus || {});
2024-12-25 00:52:39 +08:00
_.merge(this.suiteSetting, allSettings.suiteSetting || {});
2024-10-12 14:59:12 +08:00
//@ts-ignore
this.initSiteInfo(allSettings.siteInfo || {});
2024-09-24 02:42:08 +08:00
},
2024-10-12 14:59:12 +08:00
initSiteInfo(siteInfo: SiteInfo) {
//@ts-ignore
if (this.isComm) {
2024-10-05 01:46:25 +08:00
if (siteInfo.logo) {
siteInfo.logo = `/api/basic/file/download?key=${siteInfo.logo}`;
}
if (siteInfo.loginLogo) {
siteInfo.loginLogo = `/api/basic/file/download?key=${siteInfo.loginLogo}`;
}
}
this.siteInfo = _.merge({}, defaultSiteInfo, siteInfo);
2025-03-06 21:11:07 +08:00
if (this.siteInfo.logo) {
2025-03-07 18:01:51 +08:00
updatePreferences({
logo: {
source: this.siteInfo.logo
}
});
2025-03-06 21:11:07 +08:00
}
2025-03-09 01:14:44 +08:00
if (this.siteInfo.title) {
updatePreferences({
app: {
name: this.siteInfo.title
}
});
useTitle(this.siteInfo.title);
}
2024-10-05 01:46:25 +08:00
},
2024-09-24 02:42:08 +08:00
async checkUrlBound() {
const userStore = useUserStore();
2024-10-05 01:46:25 +08:00
const settingStore = useSettingStore();
2024-12-23 23:33:13 +08:00
if (!userStore.isAdmin) {
2024-09-24 02:42:08 +08:00
return;
}
const bindUrl = this.installInfo.bindUrl;
function getBaseUrl() {
let url = window.location.href;
//只要hash前面的部分
url = url.split("#")[0];
return url;
}
const doBindUrl = async (url: string) => {
await basicApi.bindUrl({ url });
2024-10-12 14:59:12 +08:00
await this.loadSysSettings();
2024-09-24 02:42:08 +08:00
};
2024-10-12 14:59:12 +08:00
2024-09-24 02:42:08 +08:00
const baseUrl = getBaseUrl();
if (!bindUrl) {
//绑定url
await doBindUrl(baseUrl);
} else {
//检查当前url 是否与绑定的url一致
const url = window.location.href;
if (!url.startsWith(bindUrl)) {
Modal.confirm({
title: "URL地址有变化",
content: "以后都用这个新地址访问本系统吗?",
onOk: async () => {
await doBindUrl(baseUrl);
},
okText: "是的,继续",
cancelText: "不是,回到原来的地址",
onCancel: () => {
window.location.href = bindUrl;
}
});
}
}
},
async init() {
await this.loadSysSettings();
2024-10-12 14:59:12 +08:00
},
async initOnce() {
if (this.inited) {
return;
}
await this.init();
this.inited = true;
}
}
});
2024-09-24 02:42:08 +08:00
mitter.on("app.login", async () => {
await useSettingStore().init();
});