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

179 lines
6.0 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";
2025-05-28 13:57:31 +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()
@Controller('/api/monitor/site')
2026-03-15 14:01:34 +08:00
@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-03-15 18:26:49 +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-02-13 00:41:40 +08:00
const { projectId, userId } = await this.getProjectUserIdRead()
body.query.projectId = projectId
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,
2025-03-17 00:16:56 +08:00
buildQuery: (bq) => {
if (domain) {
bq.andWhere('domain like :domain', { domain: `%${domain}%` });
}
if (certDomains) {
bq.andWhere('cert_domains like :cert_domains', { cert_domains: `%${certDomains}%` });
}
if (name) {
bq.andWhere('name like :name', { name: `%${name}%` });
}
}
2024-12-22 14:00:46 +08:00
});
return this.ok(res);
}
2026-03-15 18:26:49 +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-02-13 00:41:40 +08:00
const { projectId, userId } = await this.getProjectUserIdRead()
body.query.projectId = projectId
body.query.userId = userId;
2024-12-22 14:00:46 +08:00
return await super.list(body);
}
2026-03-15 18:26:49 +08:00
@Post('/add', { description: Constants.per.authOnly, summary: "添加站点监控" })
2024-12-22 14:00:46 +08:00
async add(@Body(ALL) bean: any) {
2026-02-13 00:41:40 +08:00
const { projectId, userId } = await this.getProjectUserIdWrite()
bean.projectId = projectId
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-03-15 18:26:49 +08:00
@Post('/update', { description: Constants.per.authOnly, summary: "更新站点监控" })
2024-12-22 14:00:46 +08:00
async update(@Body(ALL) bean) {
2026-02-13 00:41:40 +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-03-15 18:26:49 +08:00
@Post('/info', { description: Constants.per.authOnly, summary: "查询站点监控详情" })
2024-12-22 14:00:46 +08:00
async info(@Query('id') id: number) {
2026-02-13 00:41:40 +08:00
await this.checkOwner(this.service,id,"read");
2024-12-22 14:00:46 +08:00
return await super.info(id);
}
2026-03-15 18:26:49 +08:00
@Post('/delete', { description: Constants.per.authOnly, summary: "删除站点监控" })
2024-12-22 14:00:46 +08:00
async delete(@Query('id') id: number) {
2026-02-13 00:41:40 +08:00
await this.checkOwner(this.service,id,"write");
2024-12-22 14:00:46 +08:00
return await super.delete(id);
}
2025-10-21 00:01:28 +08:00
2026-03-15 18:26:49 +08:00
@Post('/batchDelete', { description: Constants.per.authOnly, summary: "批量删除站点监控" })
2025-10-21 00:01:28 +08:00
async batchDelete(@Body(ALL) body: any) {
2026-02-13 00:41:40 +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-03-15 18:26:49 +08:00
@Post('/check', { description: Constants.per.authOnly, summary: "检查站点监控" })
async check(@Body('id') id: number) {
2026-02-13 00:41:40 +08:00
await this.checkOwner(this.service,id,"read");
await this.service.check(id, true, 0);
await utils.sleep(1000);
return this.ok();
}
2026-03-15 18:26:49 +08:00
@Post('/checkAll', { description: Constants.per.authOnly, summary: "检查所有站点监控" })
async checkAll() {
2026-02-13 00:41:40 +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-03-15 18:26:49 +08:00
@Post('/import', { description: Constants.per.authOnly, summary: "导入站点监控" })
async doImport(@Body(ALL) body: any) {
2026-02-13 00:41:40 +08:00
const { projectId, userId } = await this.getProjectUserIdWrite()
await this.service.doImport({
text:body.text,
groupId:body.groupId,
2026-02-13 00:41:40 +08:00
userId,
projectId
})
return this.ok();
}
2026-02-13 00:41:40 +08:00
2026-03-15 18:26:49 +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-02-13 00:41:40 +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,
ipCheck: bean.ipCheck
});
return this.ok();
}
2025-05-25 23:38:25 +08:00
2026-03-15 18:26:49 +08:00
@Post('/disabledChange', { description: Constants.per.authOnly, summary: "修改禁用状态" })
2025-05-28 15:49:48 +08:00
async disabledChange(@Body(ALL) bean: any) {
2026-02-13 00:41:40 +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,
disabled: bean.disabled
});
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-02-13 22:24:04 +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-02-13 22:24:04 +08:00
const { userId, projectId} = await this.getProjectUserIdWrite()
2025-05-25 23:38:25 +08:00
const setting = new UserSiteMonitorSetting();
merge(setting, bean);
2026-02-13 22:24:04 +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
}