2025-09-13 23:01:14 +08:00
|
|
|
import { ALL, Body, Controller, Inject, Post, Provide, Query } from "@midwayjs/core";
|
2025-04-14 17:40:23 +08:00
|
|
|
import {
|
2025-11-27 01:59:22 +08:00
|
|
|
addonRegistry,
|
2025-12-14 01:36:20 +08:00
|
|
|
AddonService,
|
2025-04-14 17:40:23 +08:00
|
|
|
CrudController,
|
|
|
|
|
SysPrivateSettings,
|
|
|
|
|
SysPublicSettings,
|
|
|
|
|
SysSafeSetting,
|
|
|
|
|
SysSettingsEntity,
|
|
|
|
|
SysSettingsService
|
2025-09-13 23:01:14 +08:00
|
|
|
} from "@certd/lib-server";
|
|
|
|
|
import { cloneDeep, merge } from "lodash-es";
|
|
|
|
|
import { PipelineService } from "../../../modules/pipeline/service/pipeline-service.js";
|
|
|
|
|
import { UserSettingsService } from "../../../modules/mine/service/user-settings-service.js";
|
|
|
|
|
import { getEmailSettings } from "../../../modules/sys/settings/fix.js";
|
|
|
|
|
import { http, logger, utils } from "@certd/basic";
|
|
|
|
|
import { CodeService } from "../../../modules/basic/service/code-service.js";
|
|
|
|
|
import { SmsServiceFactory } from "../../../modules/basic/sms/factory.js";
|
2023-06-07 23:36:42 +08:00
|
|
|
|
2025-03-10 15:45:24 +08:00
|
|
|
|
2023-06-07 23:36:42 +08:00
|
|
|
/**
|
|
|
|
|
*/
|
|
|
|
|
@Provide()
|
|
|
|
|
@Controller('/api/sys/settings')
|
2024-06-16 00:20:02 +08:00
|
|
|
export class SysSettingsController extends CrudController<SysSettingsService> {
|
2023-06-07 23:36:42 +08:00
|
|
|
@Inject()
|
2024-06-16 00:20:02 +08:00
|
|
|
service: SysSettingsService;
|
2024-08-05 12:49:44 +08:00
|
|
|
@Inject()
|
2024-10-11 02:54:42 +08:00
|
|
|
userSettingsService: UserSettingsService;
|
|
|
|
|
@Inject()
|
2024-08-05 12:49:44 +08:00
|
|
|
pipelineService: PipelineService;
|
2024-11-30 01:57:09 +08:00
|
|
|
@Inject()
|
|
|
|
|
codeService: CodeService;
|
2025-12-14 01:36:20 +08:00
|
|
|
@Inject()
|
|
|
|
|
addonService: AddonService;
|
2023-06-07 23:36:42 +08:00
|
|
|
|
|
|
|
|
getService() {
|
|
|
|
|
return this.service;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-16 00:20:02 +08:00
|
|
|
@Post('/page', { summary: 'sys:settings:view' })
|
2023-06-07 23:36:42 +08:00
|
|
|
async page(@Body(ALL) body) {
|
|
|
|
|
return super.page(body);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-16 00:20:02 +08:00
|
|
|
@Post('/list', { summary: 'sys:settings:view' })
|
2023-06-07 23:36:42 +08:00
|
|
|
async list(@Body(ALL) body) {
|
|
|
|
|
return super.list(body);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-16 00:20:02 +08:00
|
|
|
@Post('/add', { summary: 'sys:settings:edit' })
|
2023-06-07 23:36:42 +08:00
|
|
|
async add(@Body(ALL) bean) {
|
|
|
|
|
return super.add(bean);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-16 00:20:02 +08:00
|
|
|
@Post('/update', { summary: 'sys:settings:edit' })
|
2023-06-07 23:36:42 +08:00
|
|
|
async update(@Body(ALL) bean) {
|
2024-10-10 02:15:05 +08:00
|
|
|
await this.service.checkUserId(bean.id, this.getUserId());
|
2023-06-07 23:36:42 +08:00
|
|
|
return super.update(bean);
|
|
|
|
|
}
|
2024-06-16 00:20:02 +08:00
|
|
|
@Post('/info', { summary: 'sys:settings:view' })
|
2024-08-30 18:50:53 +08:00
|
|
|
async info(@Query('id') id: number) {
|
2024-10-10 02:15:05 +08:00
|
|
|
await this.service.checkUserId(id, this.getUserId());
|
2023-06-07 23:36:42 +08:00
|
|
|
return super.info(id);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-16 00:20:02 +08:00
|
|
|
@Post('/delete', { summary: 'sys:settings:edit' })
|
2024-08-30 18:50:53 +08:00
|
|
|
async delete(@Query('id') id: number) {
|
2024-10-10 02:15:05 +08:00
|
|
|
await this.service.checkUserId(id, this.getUserId());
|
2023-06-07 23:36:42 +08:00
|
|
|
return super.delete(id);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-16 00:20:02 +08:00
|
|
|
@Post('/save', { summary: 'sys:settings:edit' })
|
|
|
|
|
async save(@Body(ALL) bean: SysSettingsEntity) {
|
2023-06-25 15:30:18 +08:00
|
|
|
await this.service.save(bean);
|
|
|
|
|
return this.ok({});
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-16 00:20:02 +08:00
|
|
|
@Post('/get', { summary: 'sys:settings:view' })
|
2023-06-25 15:30:18 +08:00
|
|
|
async get(@Query('key') key: string) {
|
2024-06-16 00:20:02 +08:00
|
|
|
const entity = await this.service.getByKey(key);
|
2023-06-25 15:30:18 +08:00
|
|
|
return this.ok(entity);
|
|
|
|
|
}
|
2024-06-16 00:20:02 +08:00
|
|
|
|
2024-10-11 02:54:42 +08:00
|
|
|
// savePublicSettings
|
2025-03-09 16:22:22 +08:00
|
|
|
@Post('/getEmailSettings', { summary: 'sys:settings:view' })
|
2024-10-11 02:54:42 +08:00
|
|
|
async getEmailSettings(@Body(ALL) body) {
|
2024-10-11 03:40:24 +08:00
|
|
|
const conf = await getEmailSettings(this.service, this.userSettingsService);
|
2024-10-11 02:54:42 +08:00
|
|
|
return this.ok(conf);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-14 01:36:20 +08:00
|
|
|
@Post('/getEmailTemplates', { summary: 'sys:settings:view' })
|
|
|
|
|
async getEmailTemplates(@Body(ALL) body) {
|
|
|
|
|
const conf = await getEmailSettings(this.service, this.userSettingsService);
|
|
|
|
|
const templates = conf.templates || {}
|
|
|
|
|
|
|
|
|
|
const emailTemplateProviders = await this.addonService.getDefineList("emailTemplate")
|
|
|
|
|
|
|
|
|
|
const proviers = []
|
|
|
|
|
for (const item of emailTemplateProviders) {
|
|
|
|
|
const templateConf = templates[item.name] || {}
|
|
|
|
|
proviers.push({
|
|
|
|
|
name: item.name,
|
|
|
|
|
title: item.title,
|
|
|
|
|
addonId : templateConf.addonId,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return this.ok(proviers);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-14 00:12:01 +08:00
|
|
|
@Post('/saveEmailSettings', { summary: 'sys:settings:edit' })
|
|
|
|
|
async saveEmailSettings(@Body(ALL) body) {
|
|
|
|
|
const conf = await getEmailSettings(this.service, this.userSettingsService);
|
|
|
|
|
merge(conf, body);
|
|
|
|
|
await this.service.saveSetting(conf);
|
|
|
|
|
return this.ok(conf);
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-09 16:22:22 +08:00
|
|
|
@Post('/getSysSettings', { summary: 'sys:settings:view' })
|
2024-10-12 16:49:49 +08:00
|
|
|
async getSysSettings() {
|
|
|
|
|
const publicSettings = await this.service.getPublicSettings();
|
2024-10-15 12:59:40 +08:00
|
|
|
let privateSettings = await this.service.getPrivateSettings();
|
|
|
|
|
privateSettings = privateSettings.removeSecret();
|
2024-10-12 16:49:49 +08:00
|
|
|
return this.ok({ public: publicSettings, private: privateSettings });
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-16 00:20:02 +08:00
|
|
|
// savePublicSettings
|
2024-10-12 16:49:49 +08:00
|
|
|
@Post('/saveSysSettings', { summary: 'sys:settings:edit' })
|
|
|
|
|
async saveSysSettings(@Body(ALL) body: { public: SysPublicSettings; private: SysPrivateSettings }) {
|
|
|
|
|
const publicSettings = await this.service.getPublicSettings();
|
|
|
|
|
const privateSettings = await this.service.getPrivateSettings();
|
2025-03-10 15:45:24 +08:00
|
|
|
merge(publicSettings, body.public);
|
|
|
|
|
merge(privateSettings, body.private);
|
2024-10-12 16:49:49 +08:00
|
|
|
await this.service.savePublicSettings(publicSettings);
|
|
|
|
|
await this.service.savePrivateSettings(privateSettings);
|
2024-06-16 00:20:02 +08:00
|
|
|
return this.ok({});
|
|
|
|
|
}
|
2024-08-05 12:49:44 +08:00
|
|
|
@Post('/stopOtherUserTimer', { summary: 'sys:settings:edit' })
|
|
|
|
|
async stopOtherUserTimer(@Body(ALL) body) {
|
|
|
|
|
await this.pipelineService.stopOtherUserPipeline(1);
|
|
|
|
|
return this.ok({});
|
|
|
|
|
}
|
2024-10-12 18:30:40 +08:00
|
|
|
|
2024-11-30 01:57:09 +08:00
|
|
|
@Post('/testProxy', { summary: 'sys:settings:edit' })
|
2024-10-12 18:30:40 +08:00
|
|
|
async testProxy(@Body(ALL) body) {
|
|
|
|
|
const google = 'https://www.google.com/';
|
|
|
|
|
const baidu = 'https://www.baidu.com/';
|
|
|
|
|
let googleRes = false;
|
|
|
|
|
try {
|
|
|
|
|
await http.request({
|
|
|
|
|
url: google,
|
|
|
|
|
method: 'GET',
|
2024-10-26 23:54:49 +08:00
|
|
|
timeout: 5000,
|
|
|
|
|
logRes: false,
|
|
|
|
|
logParams: false,
|
2024-10-12 18:30:40 +08:00
|
|
|
});
|
|
|
|
|
googleRes = true;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
googleRes = e.message;
|
|
|
|
|
logger.info('test google error:', e);
|
|
|
|
|
}
|
|
|
|
|
let baiduRes = false;
|
|
|
|
|
try {
|
|
|
|
|
await http.request({
|
|
|
|
|
url: baidu,
|
|
|
|
|
method: 'GET',
|
2024-10-26 23:54:49 +08:00
|
|
|
timeout: 5000,
|
|
|
|
|
logRes: false,
|
|
|
|
|
logParams: false,
|
2024-10-12 18:30:40 +08:00
|
|
|
});
|
|
|
|
|
baiduRes = true;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
baiduRes = e.message;
|
|
|
|
|
logger.info('test baidu error:', e);
|
|
|
|
|
}
|
|
|
|
|
return this.ok({
|
|
|
|
|
google: googleRes,
|
|
|
|
|
baidu: baiduRes,
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-11-30 01:57:09 +08:00
|
|
|
|
|
|
|
|
@Post('/testSms', { summary: 'sys:settings:edit' })
|
|
|
|
|
async testSms(@Body(ALL) body) {
|
2025-09-13 23:01:14 +08:00
|
|
|
await this.codeService.sendSmsCode(body.phoneCode, body.mobile );
|
2024-11-30 01:57:09 +08:00
|
|
|
return this.ok({});
|
|
|
|
|
}
|
2024-12-02 15:11:29 +08:00
|
|
|
|
|
|
|
|
@Post('/getSmsTypeDefine', { summary: 'sys:settings:view' })
|
|
|
|
|
async getSmsTypeDefine(@Body('type') type: string) {
|
2025-08-28 17:35:17 +08:00
|
|
|
const define =await SmsServiceFactory.getDefine(type);
|
|
|
|
|
return this.ok(define);
|
2024-12-02 15:11:29 +08:00
|
|
|
}
|
2025-04-14 17:40:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Post("/safe/get", { summary: "sys:settings:view" })
|
|
|
|
|
async safeGet() {
|
|
|
|
|
const res = await this.service.getSetting<SysSafeSetting>(SysSafeSetting);
|
|
|
|
|
const clone:SysSafeSetting = cloneDeep(res);
|
|
|
|
|
delete clone.hidden?.openPassword;
|
|
|
|
|
return this.ok(clone);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post("/safe/save", { summary: "sys:settings:edit" })
|
|
|
|
|
async safeSave(@Body(ALL) body: any) {
|
|
|
|
|
if(body.hidden.openPassword){
|
|
|
|
|
body.hidden.openPassword = utils.hash.md5(body.hidden.openPassword);
|
|
|
|
|
}
|
|
|
|
|
const blankSetting = new SysSafeSetting()
|
|
|
|
|
const setting = await this.service.getSetting<SysSafeSetting>(SysSafeSetting);
|
|
|
|
|
const newSetting = merge(blankSetting,cloneDeep(setting), body);
|
|
|
|
|
if(newSetting.hidden?.enabled && !newSetting.hidden?.openPassword){
|
|
|
|
|
throw new Error("首次设置需要填写解锁密码")
|
|
|
|
|
}
|
|
|
|
|
await this.service.saveSetting(blankSetting);
|
|
|
|
|
return this.ok({});
|
|
|
|
|
}
|
2025-09-26 01:21:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@Post("/captchaTest", { summary: "sys:settings:edit" })
|
|
|
|
|
async captchaTest(@Body(ALL) body: any) {
|
|
|
|
|
await this.codeService.checkCaptcha(body)
|
|
|
|
|
return this.ok({});
|
|
|
|
|
}
|
2025-11-27 01:59:22 +08:00
|
|
|
|
|
|
|
|
@Post('/oauth/providers', { summary: 'sys:settings:view' })
|
|
|
|
|
async oauthProviders() {
|
|
|
|
|
const list = await addonRegistry.getDefineList("oauth");
|
|
|
|
|
return this.ok(list);
|
|
|
|
|
}
|
2023-06-07 23:36:42 +08:00
|
|
|
}
|