2024-07-15 00:30:33 +08:00
|
|
|
import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
|
2024-06-16 00:20:02 +08:00
|
|
|
import { InjectEntityModel } from '@midwayjs/typeorm';
|
|
|
|
|
import { Repository } from 'typeorm';
|
2024-07-15 00:30:33 +08:00
|
|
|
import { SysSettingsEntity } from '../entity/sys-settings.js';
|
2024-06-16 00:20:02 +08:00
|
|
|
import { CacheManager } from '@midwayjs/cache';
|
2024-10-15 12:59:40 +08:00
|
|
|
import { BaseSettings, SysInstallInfo, SysPrivateSettings, SysPublicSettings, SysSecretBackup } from './models.js';
|
2024-07-15 00:30:33 +08:00
|
|
|
import * as _ from 'lodash-es';
|
2024-10-03 22:03:49 +08:00
|
|
|
import { BaseService } from '../../../basic/index.js';
|
2024-10-15 12:59:40 +08:00
|
|
|
import { logger, setGlobalProxy } from '@certd/basic';
|
2024-10-22 01:01:04 +08:00
|
|
|
import { agents } from '@certd/acme-client';
|
2024-06-16 00:20:02 +08:00
|
|
|
/**
|
|
|
|
|
* 设置
|
|
|
|
|
*/
|
|
|
|
|
@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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-15 00:30:33 +08:00
|
|
|
async getSetting<T>(type: any): Promise<T> {
|
|
|
|
|
const key = type.__key__;
|
|
|
|
|
const cacheKey = type.getCacheKey();
|
2024-09-01 04:49:26 +08:00
|
|
|
const settings: T = await this.cache.get(cacheKey);
|
|
|
|
|
if (settings) {
|
|
|
|
|
return settings;
|
2024-06-16 00:20:02 +08:00
|
|
|
}
|
2024-09-01 04:49:26 +08:00
|
|
|
let newSetting: T = new type();
|
|
|
|
|
const savedSettings = await this.getSettingByKey(key);
|
|
|
|
|
newSetting = _.merge(newSetting, savedSettings);
|
2024-10-11 02:54:42 +08:00
|
|
|
await this.saveSetting(newSetting);
|
2024-09-01 04:49:26 +08:00
|
|
|
await this.cache.set(cacheKey, newSetting);
|
|
|
|
|
return newSetting;
|
2024-06-16 00:20:02 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-15 00:30:33 +08:00
|
|
|
async saveSetting<T extends BaseSettings>(bean: T) {
|
|
|
|
|
const type: any = bean.constructor;
|
|
|
|
|
const key = type.__key__;
|
|
|
|
|
const cacheKey = type.getCacheKey();
|
2024-06-16 00:20:02 +08:00
|
|
|
|
|
|
|
|
const entity = await this.getByKey(key);
|
|
|
|
|
if (entity) {
|
|
|
|
|
entity.setting = JSON.stringify(bean);
|
2024-10-11 02:54:42 +08:00
|
|
|
entity.access = type.__access__;
|
2024-06-16 00:20:02 +08:00
|
|
|
await this.repository.save(entity);
|
|
|
|
|
} else {
|
|
|
|
|
const newEntity = new SysSettingsEntity();
|
|
|
|
|
newEntity.key = key;
|
2024-07-15 00:30:33 +08:00
|
|
|
newEntity.title = type.__title__;
|
2024-06-16 00:20:02 +08:00
|
|
|
newEntity.setting = JSON.stringify(bean);
|
2024-07-15 00:30:33 +08:00
|
|
|
newEntity.access = type.__access__;
|
2024-06-16 00:20:02 +08:00
|
|
|
await this.repository.save(newEntity);
|
|
|
|
|
}
|
2024-07-15 00:30:33 +08:00
|
|
|
|
|
|
|
|
await this.cache.set(cacheKey, bean);
|
2024-06-16 00:20:02 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-15 00:30:33 +08:00
|
|
|
async getPublicSettings(): Promise<SysPublicSettings> {
|
|
|
|
|
return await this.getSetting(SysPublicSettings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async savePublicSettings(bean: SysPublicSettings) {
|
|
|
|
|
await this.saveSetting(bean);
|
2024-06-16 00:20:02 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-12 16:49:49 +08:00
|
|
|
async getPrivateSettings(): Promise<SysPrivateSettings> {
|
|
|
|
|
return await this.getSetting(SysPrivateSettings);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-16 00:20:02 +08:00
|
|
|
async savePrivateSettings(bean: SysPrivateSettings) {
|
2024-10-12 16:49:49 +08:00
|
|
|
await this.saveSetting(bean);
|
|
|
|
|
|
|
|
|
|
//让设置生效
|
|
|
|
|
await this.reloadPrivateSettings();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async reloadPrivateSettings() {
|
|
|
|
|
const bean = await this.getPrivateSettings();
|
|
|
|
|
if (bean.httpProxy || bean.httpsProxy) {
|
2024-10-22 01:01:04 +08:00
|
|
|
const opts = {
|
2024-10-12 16:49:49 +08:00
|
|
|
httpProxy: bean.httpProxy,
|
|
|
|
|
httpsProxy: bean.httpsProxy,
|
2024-10-22 01:01:04 +08:00
|
|
|
};
|
|
|
|
|
setGlobalProxy(opts);
|
|
|
|
|
agents.setGlobalProxy(opts);
|
2024-10-12 16:49:49 +08:00
|
|
|
}
|
2024-07-15 00:30:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async updateByKey(key: string, setting: any) {
|
2024-06-16 00:20:02 +08:00
|
|
|
const entity = await this.getByKey(key);
|
|
|
|
|
if (entity) {
|
2024-07-15 00:30:33 +08:00
|
|
|
entity.setting = JSON.stringify(setting);
|
2024-06-16 00:20:02 +08:00
|
|
|
await this.repository.save(entity);
|
|
|
|
|
} else {
|
2024-07-15 00:30:33 +08:00
|
|
|
throw new Error('该设置不存在');
|
2024-06-16 00:20:02 +08:00
|
|
|
}
|
2024-07-15 00:30:33 +08:00
|
|
|
await this.cache.del(`settings.${key}`);
|
2024-06-16 00:20:02 +08:00
|
|
|
}
|
2024-10-15 12:59:40 +08:00
|
|
|
|
|
|
|
|
async backupSecret() {
|
|
|
|
|
const settings = await this.getSettingByKey(SysSecretBackup.__key__);
|
|
|
|
|
if (settings == null) {
|
|
|
|
|
const backup = new SysSecretBackup();
|
|
|
|
|
const privateSettings = await this.getPrivateSettings();
|
|
|
|
|
const installInfo = await this.getSetting<SysInstallInfo>(SysInstallInfo);
|
|
|
|
|
if (installInfo.siteId == null || privateSettings.encryptSecret == null) {
|
|
|
|
|
logger.error('备份密钥失败,siteId或encryptSecret为空');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
backup.siteId = installInfo.siteId;
|
|
|
|
|
backup.encryptSecret = privateSettings.encryptSecret;
|
|
|
|
|
await this.saveSetting(backup);
|
|
|
|
|
logger.info('备份密钥成功');
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-16 00:20:02 +08:00
|
|
|
}
|