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

310 lines
8.1 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";
2025-04-12 23:59:03 +08:00
import * as basicApi from "./api.basic";
import { AppInfo, HeaderMenus, PlusInfo, SiteEnv, SiteInfo, SuiteSetting, SysInstallInfo, SysPublicSetting } from "./api.basic";
import { useUserStore } from "../user";
2024-09-24 02:42:08 +08:00
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";
2025-04-27 22:51:47 +08:00
import { cloneDeep, merge } from "lodash-es";
2025-06-29 17:41:54 +08:00
import { useI18n } from "/src/locales";
2025-08-10 23:48:40 +08:00
import dayjs from "dayjs";
import { $t } from "/src/locales";
export interface SettingState {
skipReset?: boolean; // 注销登录时,不清空此store的状态
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;
2025-03-24 18:39:22 +08:00
app: {
version?: string;
time?: number;
deltaTime?: number;
};
2025-04-27 22:51:47 +08:00
productInfo: {
notice?: string;
plus: {
name: string;
price: number;
price3: number;
tooltip?: string;
};
comm: {
name: string;
price: number;
price3: number;
tooltip?: string;
};
2025-07-23 00:10:15 +08:00
app?: {
minVersion?: string;
minVersionTip?: string;
};
2025-04-27 22:51:47 +08:00
};
}
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: "",
2025-03-24 18:39:22 +08:00
licenseToUrl: "",
2024-10-05 01:46:25 +08:00
};
export const useSettingStore = defineStore({
id: "app.setting",
state: (): SettingState => ({
skipReset: true,
2024-10-05 01:46:25 +08:00
plusInfo: {
isPlus: false,
2024-10-12 14:59:12 +08:00
vipType: "free",
2025-03-24 18:39:22 +08:00
isComm: false,
2024-10-05 01:46:25 +08:00
},
sysPublic: {
registerEnabled: false,
managerOtherUserPipeline: false,
2025-03-24 18:39:22 +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: "",
2025-03-24 18:39:22 +08:00
appKey: "",
2024-09-24 02:42:08 +08:00
},
2024-10-10 18:38:22 +08:00
siteInfo: defaultSiteInfo,
siteEnv: {
agent: {
enabled: undefined,
contactText: "",
2025-03-24 18:39:22 +08:00
contactLink: "",
},
2024-10-12 14:59:12 +08:00
},
2024-10-25 23:56:24 +08:00
headerMenus: {
2025-03-24 18:39:22 +08:00
menus: [],
2024-10-25 23:56:24 +08:00
},
2024-12-25 00:52:39 +08:00
suiteSetting: { enabled: false },
2025-03-24 18:39:22 +08:00
inited: false,
app: {
version: "",
time: 0,
deltaTime: 0,
},
2025-04-27 22:51:47 +08:00
productInfo: {
notice: "",
plus: {
name: "专业版",
price: 29.9,
price3: 89.9,
},
comm: {
name: "商业版",
price: 399,
price3: 899,
},
2025-07-23 00:10:15 +08:00
app: {
minVersion: "",
minVersionTip: "",
},
2025-04-27 22:51:47 +08:00
},
}),
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
},
2025-11-09 00:12:31 +08:00
isPerpetual(): boolean {
return this.plusInfo?.isPlus && this.plusInfo?.expireTime === -1;
},
2024-10-05 01:46:25 +08:00
isPlus(): boolean {
2025-08-10 02:07:48 +08:00
return this.plusInfo?.isPlus && (this.plusInfo?.expireTime === -1 || this.plusInfo?.expireTime > new Date().getTime());
2024-10-05 01:46:25 +08:00
},
isComm(): boolean {
2025-08-10 02:07:48 +08:00
return this.plusInfo?.isComm && (this.plusInfo?.expireTime === -1 || this.plusInfo?.expireTime > new Date().getTime());
2024-10-05 01:46:25 +08:00
},
2024-10-12 14:59:12 +08:00
isAgent(): boolean {
return this.siteEnv?.agent?.enabled === true;
},
isCommOrAgent() {
return this.isComm || this.isAgent;
},
2025-08-10 02:07:48 +08:00
expiresText() {
if (this.plusInfo?.expireTime == null) {
return "";
}
if (this.plusInfo?.expireTime === -1) {
return "永久";
}
return dayjs(this.plusInfo?.expireTime).format("YYYY-MM-DD");
2025-08-10 02:07:48 +08:00
},
isForever() {
return this.isPlus && this.plusInfo?.expireTime === -1;
},
2024-10-05 01:46:25 +08:00
vipLabel(): string {
2025-06-29 17:41:54 +08:00
const { t } = useI18n();
2024-10-05 01:46:25 +08:00
const vipLabelMap: any = {
2025-06-29 17:41:54 +08:00
free: t("vip.label.free"),
plus: t("vip.label.plus"),
comm: t("vip.label.comm"),
2024-10-05 01:46:25 +08:00
};
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,
2025-03-24 18:39:22 +08:00
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;
2025-03-24 18:39:22 +08:00
},
},
actions: {
2024-10-05 01:46:25 +08:00
checkPlus() {
if (!this.isPlus) {
notification.warn({
message: $t("vip.needVipTip"),
2024-10-05 01:46:25 +08:00
});
throw new Error($t("vip.needVipTip"));
2024-10-05 01:46:25 +08:00
}
},
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 || {});
merge(this.headerMenus, allSettings.headerMenus || {});
merge(this.suiteSetting, allSettings.suiteSetting || {});
2024-10-12 14:59:12 +08:00
//@ts-ignore
this.initSiteInfo(allSettings.siteInfo || {});
2025-03-24 18:39:22 +08:00
this.initAppInfo(allSettings.app || {});
},
initAppInfo(appInfo: AppInfo) {
this.app.time = appInfo.time;
this.app.version = appInfo.version;
this.app.deltaTime = new Date().getTime() - this.app.time;
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}`;
2024-10-05 01:46:25 +08:00
}
if (siteInfo.loginLogo) {
siteInfo.loginLogo = `api/basic/file/download?key=${siteInfo.loginLogo}`;
2024-10-05 01:46:25 +08:00
}
}
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: {
2025-03-24 18:39:22 +08:00
source: this.siteInfo.logo,
},
2025-03-07 18:01:51 +08:00
});
2025-03-06 21:11:07 +08:00
}
2025-03-09 01:14:44 +08:00
if (this.siteInfo.title) {
updatePreferences({
app: {
2025-03-24 18:39:22 +08:00
name: this.siteInfo.title,
},
2025-03-09 01:14:44 +08:00
});
useTitle(this.siteInfo.title);
}
2024-10-05 01:46:25 +08:00
},
2025-04-19 11:48:23 +08:00
getBaseUrl() {
let url = window.location.href;
//只要hash前面的部分
url = url.split("#")[0];
return url;
},
async doBindUrl() {
const url = this.getBaseUrl();
await basicApi.bindUrl({ url });
await this.loadSysSettings();
},
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;
if (!bindUrl) {
//绑定url
2025-04-19 11:48:23 +08:00
await this.doBindUrl();
2024-09-24 02:42:08 +08:00
} else {
//检查当前url 是否与绑定的url一致
const url = window.location.href;
if (!url.startsWith(bindUrl)) {
Modal.confirm({
title: "URL地址有变化",
content: "以后都用这个新地址访问本系统吗?",
onOk: async () => {
2025-04-19 11:48:23 +08:00
await this.doBindUrl();
2024-09-24 02:42:08 +08:00
},
okText: "是的,继续",
cancelText: "不是,回到原来的地址",
onCancel: () => {
window.location.href = bindUrl;
2025-03-24 18:39:22 +08:00
},
2024-09-24 02:42:08 +08:00
});
}
}
},
2025-04-27 22:51:47 +08:00
async loadProductInfo() {
try {
const productInfo = await basicApi.getProductInfo();
merge(this.productInfo, productInfo);
} catch (e) {
console.error(e);
}
},
async init() {
await this.loadSysSettings();
2024-10-12 14:59:12 +08:00
},
async initOnce() {
if (this.inited) {
return;
}
await this.init();
2025-04-27 22:51:47 +08:00
this.loadProductInfo();
2024-10-12 14:59:12 +08:00
this.inited = true;
2025-03-24 18:39:22 +08:00
},
},
});
2024-09-24 02:42:08 +08:00
mitter.on("app.login", async () => {
await useSettingStore().init();
});