Files
certd/packages/ui/certd-server/src/controller/user/monitor/site-info-controller.ts
T

225 lines
7.8 KiB
TypeScript
Raw Normal View History

2025-05-25 23:38:25 +08:00
import { ALL, Body, Controller, Inject, Post, Provide, Query } from "@midwayjs/core";
import { Constants, CrudController } from "@certd/lib-server";
import { AuthService } from "../../../modules/sys/authority/service/auth-service.js";
import { SiteInfoService } from "../../../modules/monitor/service/site-info-service.js";
import { UserSiteMonitorSetting } from "../../../modules/mine/service/models.js";
import { merge } from "lodash-es";
2026-05-31 01:41:33 +08:00
import { SiteIpService } from "../../../modules/monitor/service/site-ip-service.js";
import { utils } from "@certd/basic";
2026-03-15 14:01:34 +08:00
import { ApiTags } from "@midwayjs/swagger";
2024-12-22 14:00:46 +08:00
/**
*/
@Provide()
2026-05-31 01:41:33 +08:00
@Controller("/api/monitor/site")
@ApiTags(["monitor"])
export class SiteInfoController extends CrudController<SiteInfoService> {
2024-12-22 14:00:46 +08:00
@Inject()
service: SiteInfoService;
2024-12-22 14:00:46 +08:00
@Inject()
authService: AuthService;
2025-05-28 13:57:31 +08:00
@Inject()
siteIpService: SiteIpService;
2024-12-22 14:00:46 +08:00
getService(): SiteInfoService {
2024-12-22 14:00:46 +08:00
return this.service;
}
2026-05-31 01:41:33 +08:00
@Post("/page", { description: Constants.per.authOnly, summary: "查询站点监控分页列表" })
2024-12-22 14:00:46 +08:00
async page(@Body(ALL) body: any) {
body.query = body.query ?? {};
2026-05-31 01:41:33 +08:00
const { projectId, userId } = await this.getProjectUserIdRead();
body.query.projectId = projectId;
2026-02-13 00:41:40 +08:00
body.query.userId = userId;
2025-03-17 00:16:56 +08:00
const certDomains = body.query.certDomains;
const domain = body.query.domain;
const name = body.query.name;
delete body.query.certDomains;
delete body.query.domain;
delete body.query.name;
2024-12-22 14:00:46 +08:00
const res = await this.service.page({
query: body.query,
page: body.page,
sort: body.sort,
2026-05-31 01:41:33 +08:00
buildQuery: bq => {
2025-03-17 00:16:56 +08:00
if (domain) {
2026-05-31 01:41:33 +08:00
bq.andWhere("domain like :domain", { domain: `%${domain}%` });
2025-03-17 00:16:56 +08:00
}
if (certDomains) {
2026-05-31 01:41:33 +08:00
bq.andWhere("cert_domains like :cert_domains", { cert_domains: `%${certDomains}%` });
2025-03-17 00:16:56 +08:00
}
if (name) {
2026-05-31 01:41:33 +08:00
bq.andWhere("name like :name", { name: `%${name}%` });
2025-03-17 00:16:56 +08:00
}
2026-05-31 01:41:33 +08:00
},
2024-12-22 14:00:46 +08:00
});
return this.ok(res);
}
2026-05-31 01:41:33 +08:00
@Post("/list", { description: Constants.per.authOnly, summary: "查询站点监控列表" })
2024-12-22 14:00:46 +08:00
async list(@Body(ALL) body: any) {
body.query = body.query ?? {};
2026-05-31 01:41:33 +08:00
const { projectId, userId } = await this.getProjectUserIdRead();
body.query.projectId = projectId;
2026-02-13 00:41:40 +08:00
body.query.userId = userId;
2024-12-22 14:00:46 +08:00
return await super.list(body);
}
2026-05-31 01:41:33 +08:00
@Post("/add", { description: Constants.per.authOnly, summary: "添加站点监控" })
2024-12-22 14:00:46 +08:00
async add(@Body(ALL) bean: any) {
2026-05-31 01:41:33 +08:00
const { projectId, userId } = await this.getProjectUserIdWrite();
bean.projectId = projectId;
2026-02-13 00:41:40 +08:00
bean.userId = userId;
2024-12-24 01:12:12 +08:00
const res = await this.service.add(bean);
2025-05-28 15:49:48 +08:00
const entity = await this.service.info(res.id);
if (entity.disabled) {
this.service.check(entity.id, true, 0);
}
2024-12-24 01:12:12 +08:00
return this.ok(res);
2024-12-22 14:00:46 +08:00
}
2026-05-31 01:41:33 +08:00
@Post("/update", { description: Constants.per.authOnly, summary: "更新站点监控" })
2024-12-22 14:00:46 +08:00
async update(@Body(ALL) bean) {
2026-05-31 01:41:33 +08:00
await this.checkOwner(this.service, bean.id, "write");
2024-12-22 14:00:46 +08:00
delete bean.userId;
2026-02-13 21:28:17 +08:00
delete bean.projectId;
2024-12-24 01:12:12 +08:00
await this.service.update(bean);
2025-05-28 15:49:48 +08:00
const entity = await this.service.info(bean.id);
if (entity.disabled) {
this.service.check(entity.id, true, 0);
}
2024-12-24 01:12:12 +08:00
return this.ok();
2024-12-22 14:00:46 +08:00
}
2026-05-31 01:41:33 +08:00
@Post("/info", { description: Constants.per.authOnly, summary: "查询站点监控详情" })
async info(@Query("id") id: number) {
await this.checkOwner(this.service, id, "read");
2024-12-22 14:00:46 +08:00
return await super.info(id);
}
2026-05-31 01:41:33 +08:00
@Post("/delete", { description: Constants.per.authOnly, summary: "删除站点监控" })
async delete(@Query("id") id: number) {
await this.checkOwner(this.service, id, "write");
2024-12-22 14:00:46 +08:00
return await super.delete(id);
}
2026-05-31 01:41:33 +08:00
@Post("/batchDelete", { description: Constants.per.authOnly, summary: "批量删除站点监控" })
2025-10-21 00:01:28 +08:00
async batchDelete(@Body(ALL) body: any) {
2026-05-31 01:41:33 +08:00
const { projectId, userId } = await this.getProjectUserIdWrite();
await this.service.batchDelete(body.ids, userId, projectId);
2025-10-21 00:01:28 +08:00
return this.ok();
}
2026-05-31 01:41:33 +08:00
@Post("/check", { description: Constants.per.authOnly, summary: "检查站点监控" })
async check(@Body("id") id: number) {
await this.checkOwner(this.service, id, "read");
await this.service.check(id, true, 0);
await utils.sleep(1000);
return this.ok();
}
2026-05-31 01:41:33 +08:00
@Post("/checkAll", { description: Constants.per.authOnly, summary: "检查所有站点监控" })
async checkAll() {
2026-05-31 01:41:33 +08:00
const { projectId, userId } = await this.getProjectUserIdWrite();
this.service.triggerJobOnce(userId, projectId);
return this.ok();
2024-12-22 14:00:46 +08:00
}
2026-05-31 01:41:33 +08:00
@Post("/import", { description: Constants.per.authOnly, summary: "导入站点监控" })
async doImport(@Body(ALL) body: any) {
2026-05-31 01:41:33 +08:00
const { projectId, userId } = await this.getProjectUserIdWrite();
await this.service.doImport({
2026-05-31 01:41:33 +08:00
text: body.text,
groupId: body.groupId,
2026-02-13 00:41:40 +08:00
userId,
2026-05-31 01:41:33 +08:00
projectId,
});
return this.ok();
}
@Post("/import/save", { description: Constants.per.authOnly, summary: "保存站点证书监控导入任务" })
async siteInfoImportSave(@Body(ALL) body: any) {
const { projectId, userId } = await this.getProjectUserIdWrite();
const { dnsProviderType, dnsProviderAccessId, key, groupId } = body;
const item = await this.service.saveSiteInfoImportTask({
userId: userId,
projectId: projectId,
dnsProviderType,
dnsProviderAccessId,
key,
groupId,
});
return this.ok(item);
}
@Post("/import/status", { description: Constants.per.authOnly, summary: "查询站点证书监控导入任务状态" })
async siteInfoImportStatus() {
const { projectId, userId } = await this.getProjectUserIdRead();
const task = await this.service.getSiteInfoImportTaskStatus({
userId: userId,
projectId: projectId,
});
return this.ok(task);
}
@Post("/import/delete", { description: Constants.per.authOnly, summary: "删除站点证书监控导入任务" })
async siteInfoImportDelete(@Body(ALL) body: any) {
const { projectId, userId } = await this.getProjectUserIdWrite();
const { key } = body;
await this.service.deleteSiteInfoImportTask({
userId: userId,
projectId: projectId,
key,
});
return this.ok();
}
@Post("/import/start", { description: Constants.per.authOnly, summary: "开始站点证书监控导入任务" })
async siteInfoImportStart(@Body(ALL) body: any) {
const { projectId, userId } = await this.getProjectUserIdWrite();
const { key } = body;
await this.service.startSiteInfoImportTask({
key,
userId: userId,
projectId: projectId,
});
return this.ok();
}
2026-05-31 01:41:33 +08:00
@Post("/ipCheckChange", { description: Constants.per.authOnly, summary: "修改IP检查设置" })
2025-05-28 13:57:31 +08:00
async ipCheckChange(@Body(ALL) bean: any) {
2026-05-31 01:41:33 +08:00
await this.checkOwner(this.service, bean.id, "read");
2025-05-28 13:57:31 +08:00
await this.service.ipCheckChange({
id: bean.id,
2026-05-31 01:41:33 +08:00
ipCheck: bean.ipCheck,
2025-05-28 13:57:31 +08:00
});
return this.ok();
}
2025-05-25 23:38:25 +08:00
2026-05-31 01:41:33 +08:00
@Post("/disabledChange", { description: Constants.per.authOnly, summary: "修改禁用状态" })
2025-05-28 15:49:48 +08:00
async disabledChange(@Body(ALL) bean: any) {
2026-05-31 01:41:33 +08:00
await this.checkOwner(this.service, bean.id, "write");
2025-05-28 15:49:48 +08:00
await this.service.disabledChange({
id: bean.id,
2026-05-31 01:41:33 +08:00
disabled: bean.disabled,
2025-05-28 15:49:48 +08:00
});
return this.ok();
}
2025-05-25 23:38:25 +08:00
2026-03-15 18:26:49 +08:00
@Post("/setting/get", { description: Constants.per.authOnly, summary: "获取站点监控设置" })
2025-05-25 23:38:25 +08:00
async get() {
2026-05-31 01:41:33 +08:00
const { userId, projectId } = await this.getProjectUserIdRead();
const setting = await this.service.getSetting(userId, projectId);
2025-05-25 23:38:25 +08:00
return this.ok(setting);
}
2026-03-15 18:26:49 +08:00
@Post("/setting/save", { description: Constants.per.authOnly, summary: "保存站点监控设置" })
2025-05-25 23:38:25 +08:00
async save(@Body(ALL) bean: any) {
2026-05-31 01:41:33 +08:00
const { userId, projectId } = await this.getProjectUserIdWrite();
2025-05-25 23:38:25 +08:00
const setting = new UserSiteMonitorSetting();
merge(setting, bean);
2026-05-31 01:41:33 +08:00
await this.service.saveSetting(userId, projectId, setting);
2025-05-25 23:38:25 +08:00
return this.ok({});
}
2024-12-22 14:00:46 +08:00
}