Files
certd/packages/ui/certd-server/src/controller/sys/settings/sys-settings-controller.ts
T

229 lines
7.5 KiB
TypeScript
Raw Normal View History

import { ALL, Body, Controller, Inject, Post, Provide, Query, RequestIP } from "@midwayjs/core";
2026-05-31 01:41:33 +08:00
import { addonRegistry, AddonService, CrudController, SysPrivateSettings, SysPublicSettings, SysSafeSetting, SysSettingsEntity, SysSettingsService } 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";
2026-06-20 00:35:13 +08:00
import { RuntimeDepsService } from "../../../modules/runtime-deps/runtime-deps-service.js";
2023-06-07 23:36:42 +08:00
/**
*/
@Provide()
2026-05-31 01:41:33 +08:00
@Controller("/api/sys/settings")
export class SysSettingsController extends CrudController<SysSettingsService> {
2023-06-07 23:36:42 +08:00
@Inject()
service: SysSettingsService;
@Inject()
userSettingsService: UserSettingsService;
@Inject()
pipelineService: PipelineService;
2024-11-30 01:57:09 +08:00
@Inject()
codeService: CodeService;
2026-05-31 01:41:33 +08:00
@Inject()
2025-12-14 01:36:20 +08:00
addonService: AddonService;
2026-06-20 00:35:13 +08:00
@Inject()
runtimeDepsService: RuntimeDepsService;
2023-06-07 23:36:42 +08:00
getService() {
return this.service;
}
2026-05-31 01:41:33 +08:00
@Post("/page", { description: "sys:settings:view" })
2023-06-07 23:36:42 +08:00
async page(@Body(ALL) body) {
return super.page(body);
}
2026-05-31 01:41:33 +08:00
@Post("/list", { description: "sys:settings:view" })
2023-06-07 23:36:42 +08:00
async list(@Body(ALL) body) {
return super.list(body);
}
2026-05-31 01:41:33 +08:00
@Post("/add", { description: "sys:settings:edit" })
2023-06-07 23:36:42 +08:00
async add(@Body(ALL) bean) {
return super.add(bean);
}
2026-05-31 01:41:33 +08:00
@Post("/update", { description: "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);
}
2026-05-31 01:41:33 +08:00
@Post("/info", { description: "sys:settings:view" })
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);
}
2026-05-31 01:41:33 +08:00
@Post("/delete", { description: "sys:settings:edit" })
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);
}
2026-05-31 01:41:33 +08:00
@Post("/save", { description: "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({});
}
2026-05-31 01:41:33 +08:00
@Post("/get", { description: "sys:settings:view" })
async get(@Query("key") key: string) {
const entity = await this.service.getByKey(key);
2023-06-25 15:30:18 +08:00
return this.ok(entity);
}
// savePublicSettings
2026-05-31 01:41:33 +08:00
@Post("/getEmailSettings", { description: "sys:settings:view" })
async getEmailSettings(@Body(ALL) body) {
2024-10-11 03:40:24 +08:00
const conf = await getEmailSettings(this.service, this.userSettingsService);
return this.ok(conf);
}
2026-05-31 01:41:33 +08:00
@Post("/getEmailTemplates", { description: "sys:settings:view" })
2025-12-14 01:36:20 +08:00
async getEmailTemplates(@Body(ALL) body) {
const conf = await getEmailSettings(this.service, this.userSettingsService);
2026-05-31 01:41:33 +08:00
const templates = conf.templates || {};
2025-12-14 01:36:20 +08:00
2026-05-31 01:41:33 +08:00
const emailTemplateProviders = await this.addonService.getDefineList("emailTemplate");
2025-12-14 01:36:20 +08:00
2026-05-31 01:41:33 +08:00
const proviers = [];
2025-12-14 01:36:20 +08:00
for (const item of emailTemplateProviders) {
2026-05-31 01:41:33 +08:00
const templateConf = templates[item.name] || {};
2025-12-14 01:36:20 +08:00
proviers.push({
name: item.name,
title: item.title,
2026-05-31 01:41:33 +08:00
addonId: templateConf.addonId,
});
2025-12-14 01:36:20 +08:00
}
return this.ok(proviers);
}
2026-05-31 01:41:33 +08:00
@Post("/saveEmailSettings", { description: "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);
}
2026-05-31 01:41:33 +08:00
@Post("/getSysSettings", { description: "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 });
}
// savePublicSettings
2026-05-31 01:41:33 +08:00
@Post("/saveSysSettings", { description: "sys:settings:edit" })
2024-10-12 16:49:49 +08:00
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);
return this.ok({});
}
2026-05-31 01:41:33 +08:00
@Post("/stopOtherUserTimer", { description: "sys:settings:edit" })
async stopOtherUserTimer(@Body(ALL) body) {
await this.pipelineService.stopOtherUserPipeline(1);
return this.ok({});
}
2024-10-12 18:30:40 +08:00
2026-05-31 01:41:33 +08:00
@Post("/testProxy", { description: "sys:settings:edit" })
2024-10-12 18:30:40 +08:00
async testProxy(@Body(ALL) body) {
2026-05-31 01:41:33 +08:00
const google = "https://www.google.com/";
const baidu = "https://www.baidu.com/";
2024-10-12 18:30:40 +08:00
let googleRes = false;
try {
await http.request({
url: google,
2026-05-31 01:41:33 +08:00
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;
2026-05-31 01:41:33 +08:00
logger.info("test google error:", e);
2024-10-12 18:30:40 +08:00
}
let baiduRes = false;
try {
await http.request({
url: baidu,
2026-05-31 01:41:33 +08:00
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;
2026-05-31 01:41:33 +08:00
logger.info("test baidu error:", e);
2024-10-12 18:30:40 +08:00
}
return this.ok({
google: googleRes,
baidu: baiduRes,
});
}
2024-11-30 01:57:09 +08:00
2026-05-31 01:41:33 +08:00
@Post("/testSms", { description: "sys:settings:edit" })
2024-11-30 01:57:09 +08:00
async testSms(@Body(ALL) body) {
2026-05-31 01:41:33 +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
2026-05-31 01:41:33 +08:00
@Post("/getSmsTypeDefine", { description: "sys:settings:view" })
async getSmsTypeDefine(@Body("type") type: string) {
const define = await SmsServiceFactory.getDefine(type);
2025-08-28 17:35:17 +08:00
return this.ok(define);
2024-12-02 15:11:29 +08:00
}
@Post("/safe/get", { description: "sys:settings:view" })
async safeGet() {
const res = await this.service.getSetting<SysSafeSetting>(SysSafeSetting);
2026-05-31 01:41:33 +08:00
const clone: SysSafeSetting = cloneDeep(res);
delete clone.hidden?.openPassword;
return this.ok(clone);
}
@Post("/safe/save", { description: "sys:settings:edit" })
async safeSave(@Body(ALL) body: any) {
2026-05-31 01:41:33 +08:00
if (body.hidden.openPassword) {
body.hidden.openPassword = utils.hash.md5(body.hidden.openPassword);
}
2026-05-31 01:41:33 +08:00
const blankSetting = new SysSafeSetting();
const setting = await this.service.getSetting<SysSafeSetting>(SysSafeSetting);
2026-05-31 01:41:33 +08:00
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({});
}
@Post("/captchaTest", { description: "sys:settings:edit" })
2026-05-31 01:41:33 +08:00
async captchaTest(@Body(ALL) body: any, @RequestIP() remoteIp: string) {
await this.codeService.checkCaptcha(body, { remoteIp });
return this.ok({});
}
2025-11-27 01:59:22 +08:00
2026-05-31 01:41:33 +08:00
@Post("/oauth/providers", { description: "sys:settings:view" })
2025-11-27 01:59:22 +08:00
async oauthProviders() {
const list = await addonRegistry.getDefineList("oauth");
return this.ok(list);
}
2026-06-20 00:35:13 +08:00
@Post("/clearRuntimeDeps", { description: "sys:settings:edit" })
async clearRuntimeDeps() {
await this.runtimeDepsService.clearRuntimeDeps();
return this.ok(true);
}
2023-06-07 23:36:42 +08:00
}