mirror of
https://github.com/certd/certd.git
synced 2026-04-24 04:17:25 +08:00
chore: lib-server
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
|
||||
|
||||
/**
|
||||
*/
|
||||
@Entity('sys_settings')
|
||||
export class SysSettingsEntity {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
@Column({ comment: 'key', length: 100 })
|
||||
key: string;
|
||||
@Column({ comment: '名称', length: 100 })
|
||||
title: string;
|
||||
|
||||
@Column({ name: 'setting', comment: '设置', length: 1024, nullable: true })
|
||||
setting: string;
|
||||
|
||||
// public 公开读,私有写, private 私有读,私有写
|
||||
@Column({ name: 'access', comment: '访问权限' })
|
||||
access: string;
|
||||
|
||||
@Column({
|
||||
name: 'create_time',
|
||||
comment: '创建时间',
|
||||
default: () => 'CURRENT_TIMESTAMP',
|
||||
})
|
||||
createTime: Date;
|
||||
@Column({
|
||||
name: 'update_time',
|
||||
comment: '修改时间',
|
||||
default: () => 'CURRENT_TIMESTAMP',
|
||||
})
|
||||
updateTime: Date;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export * from './service/sys-settings-service.js';
|
||||
export * from './service/models.js';
|
||||
export * from './entity/sys-settings.js';
|
||||
@@ -0,0 +1,55 @@
|
||||
export class BaseSettings {
|
||||
static __key__: string;
|
||||
static __title__: string;
|
||||
static __access__ = 'private';
|
||||
|
||||
static getCacheKey() {
|
||||
return 'settings.' + this.__key__;
|
||||
}
|
||||
}
|
||||
|
||||
export class SysPublicSettings extends BaseSettings {
|
||||
static __key__ = 'sys.public';
|
||||
static __title__ = '系统公共设置';
|
||||
static __access__ = 'public';
|
||||
registerEnabled = false;
|
||||
managerOtherUserPipeline = false;
|
||||
// triggerOnStartup = false;
|
||||
}
|
||||
|
||||
export class SysPrivateSettings extends BaseSettings {
|
||||
static __title__ = '系统私有设置';
|
||||
static __access__ = 'private';
|
||||
static __key__ = 'sys.private';
|
||||
jwtKey?: string;
|
||||
encryptSecret?: string;
|
||||
}
|
||||
|
||||
export class SysInstallInfo extends BaseSettings {
|
||||
static __title__ = '系统安装信息';
|
||||
static __key__ = 'sys.install';
|
||||
static __access__ = 'private';
|
||||
installTime: number;
|
||||
siteId?: string;
|
||||
bindUserId?: number;
|
||||
bindUrl?: string;
|
||||
accountServerBaseUrl?: string;
|
||||
appKey?: string;
|
||||
}
|
||||
|
||||
export class SysLicenseInfo extends BaseSettings {
|
||||
static __title__ = '授权许可信息';
|
||||
static __key__ = 'sys.license';
|
||||
static __access__ = 'private';
|
||||
license?: string;
|
||||
}
|
||||
|
||||
export class SysSiteInfo extends BaseSettings {
|
||||
static __title__ = '站点信息';
|
||||
static __key__ = 'sys.site';
|
||||
static __access__ = 'public';
|
||||
TITLE?: string;
|
||||
SLOGAN?: string;
|
||||
LOGO?: string;
|
||||
ICP_NO?: string;
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
|
||||
import { InjectEntityModel } from '@midwayjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { SysSettingsEntity } from '../entity/sys-settings.js';
|
||||
import { CacheManager } from '@midwayjs/cache';
|
||||
import { BaseSettings, SysPrivateSettings, SysPublicSettings } from './models.js';
|
||||
import * as _ from 'lodash-es';
|
||||
import { BaseService } from '../../../basic/index.js';
|
||||
|
||||
/**
|
||||
* 设置
|
||||
*/
|
||||
@Provide()
|
||||
@Scope(ScopeEnum.Singleton)
|
||||
export class SysSettingsService extends BaseService<SysSettingsEntity> {
|
||||
@InjectEntityModel(SysSettingsEntity)
|
||||
repository: Repository<SysSettingsEntity>;
|
||||
|
||||
@Inject()
|
||||
cache: CacheManager; // 依赖注入CacheManager
|
||||
|
||||
getRepository() {
|
||||
return this.repository;
|
||||
}
|
||||
|
||||
async getById(id: any): Promise<SysSettingsEntity | null> {
|
||||
const entity = await this.info(id);
|
||||
if (!entity) {
|
||||
return null;
|
||||
}
|
||||
const setting = JSON.parse(entity.setting);
|
||||
return {
|
||||
id: entity.id,
|
||||
...setting,
|
||||
};
|
||||
}
|
||||
|
||||
async getByKey(key: string): Promise<SysSettingsEntity | null> {
|
||||
if (!key) {
|
||||
return null;
|
||||
}
|
||||
return await this.repository.findOne({
|
||||
where: {
|
||||
key,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async getSettingByKey(key: string): Promise<any | null> {
|
||||
const entity = await this.getByKey(key);
|
||||
if (!entity) {
|
||||
return null;
|
||||
}
|
||||
return JSON.parse(entity.setting);
|
||||
}
|
||||
|
||||
async save(bean: SysSettingsEntity) {
|
||||
const entity = await this.repository.findOne({
|
||||
where: {
|
||||
key: bean.key,
|
||||
},
|
||||
});
|
||||
if (entity) {
|
||||
entity.setting = bean.setting;
|
||||
await this.repository.save(entity);
|
||||
} else {
|
||||
bean.title = bean.key;
|
||||
await this.repository.save(bean);
|
||||
}
|
||||
}
|
||||
|
||||
async getSetting<T>(type: any): Promise<T> {
|
||||
const key = type.__key__;
|
||||
const cacheKey = type.getCacheKey();
|
||||
const settings: T = await this.cache.get(cacheKey);
|
||||
if (settings) {
|
||||
return settings;
|
||||
}
|
||||
let newSetting: T = new type();
|
||||
const savedSettings = await this.getSettingByKey(key);
|
||||
newSetting = _.merge(newSetting, savedSettings);
|
||||
await this.cache.set(cacheKey, newSetting);
|
||||
return newSetting;
|
||||
}
|
||||
|
||||
async saveSetting<T extends BaseSettings>(bean: T) {
|
||||
const type: any = bean.constructor;
|
||||
const key = type.__key__;
|
||||
const cacheKey = type.getCacheKey();
|
||||
|
||||
const entity = await this.getByKey(key);
|
||||
if (entity) {
|
||||
entity.setting = JSON.stringify(bean);
|
||||
await this.repository.save(entity);
|
||||
} else {
|
||||
const newEntity = new SysSettingsEntity();
|
||||
newEntity.key = key;
|
||||
newEntity.title = type.__title__;
|
||||
newEntity.setting = JSON.stringify(bean);
|
||||
newEntity.access = type.__access__;
|
||||
await this.repository.save(newEntity);
|
||||
}
|
||||
|
||||
await this.cache.set(cacheKey, bean);
|
||||
}
|
||||
|
||||
async getPublicSettings(): Promise<SysPublicSettings> {
|
||||
return await this.getSetting(SysPublicSettings);
|
||||
}
|
||||
|
||||
async savePublicSettings(bean: SysPublicSettings) {
|
||||
await this.saveSetting(bean);
|
||||
}
|
||||
|
||||
async savePrivateSettings(bean: SysPrivateSettings) {
|
||||
this.saveSetting(bean);
|
||||
}
|
||||
|
||||
async updateByKey(key: string, setting: any) {
|
||||
const entity = await this.getByKey(key);
|
||||
if (entity) {
|
||||
entity.setting = JSON.stringify(setting);
|
||||
await this.repository.save(entity);
|
||||
} else {
|
||||
throw new Error('该设置不存在');
|
||||
}
|
||||
await this.cache.del(`settings.${key}`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user