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

168 lines
5.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";
2024-12-22 14:00:46 +08:00
/**
*/
@Provide()
@Controller('/api/monitor/site')
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;
}
@Post('/page', { summary: Constants.per.authOnly })
async page(@Body(ALL) body: any) {
body.query = body.query ?? {};
body.query.userId = this.getUserId();
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);
}
@Post('/list', { summary: Constants.per.authOnly })
async list(@Body(ALL) body: any) {
body.query = body.query ?? {};
body.query.userId = this.getUserId();
return await super.list(body);
}
@Post('/add', { summary: Constants.per.authOnly })
async add(@Body(ALL) bean: any) {
bean.userId = this.getUserId();
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
}
@Post('/update', { summary: Constants.per.authOnly })
async update(@Body(ALL) bean) {
await this.service.checkUserId(bean.id, this.getUserId());
delete bean.userId;
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
}
@Post('/info', { summary: Constants.per.authOnly })
async info(@Query('id') id: number) {
await this.service.checkUserId(id, this.getUserId());
return await super.info(id);
}
@Post('/delete', { summary: Constants.per.authOnly })
async delete(@Query('id') id: number) {
await this.service.checkUserId(id, this.getUserId());
return await super.delete(id);
}
2025-10-21 00:01:28 +08:00
@Post('/batchDelete', { summary: Constants.per.authOnly })
async batchDelete(@Body(ALL) body: any) {
const userId = this.getUserId();
await this.service.batchDelete(body.ids,userId);
return this.ok();
}
@Post('/check', { summary: Constants.per.authOnly })
async check(@Body('id') id: number) {
await this.service.checkUserId(id, this.getUserId());
await this.service.check(id, true, 0);
return this.ok();
}
@Post('/checkAll', { summary: Constants.per.authOnly })
async checkAll() {
const userId = this.getUserId();
await this.service.checkAllByUsers(userId);
return this.ok();
2024-12-22 14:00:46 +08:00
}
@Post('/import', { summary: Constants.per.authOnly })
async doImport(@Body(ALL) body: any) {
const userId = this.getUserId();
await this.service.doImport({
text:body.text,
groupId:body.groupId,
userId
})
return this.ok();
}
2025-05-28 13:57:31 +08:00
@Post('/ipCheckChange', { summary: Constants.per.authOnly })
async ipCheckChange(@Body(ALL) bean: any) {
const userId = this.getUserId();
await this.service.checkUserId(bean.id, userId)
await this.service.ipCheckChange({
id: bean.id,
ipCheck: bean.ipCheck
});
return this.ok();
}
2025-05-25 23:38:25 +08:00
2025-05-28 15:49:48 +08:00
@Post('/disabledChange', { summary: Constants.per.authOnly })
async disabledChange(@Body(ALL) bean: any) {
const userId = this.getUserId();
await this.service.checkUserId(bean.id, userId)
await this.service.disabledChange({
id: bean.id,
disabled: bean.disabled
});
return this.ok();
}
2025-05-25 23:38:25 +08:00
@Post("/setting/get", { summary: Constants.per.authOnly })
async get() {
const userId = this.getUserId();
const setting = await this.service.getSetting(userId)
return this.ok(setting);
}
@Post("/setting/save", { summary: Constants.per.authOnly })
async save(@Body(ALL) bean: any) {
const userId = this.getUserId();
const setting = new UserSiteMonitorSetting();
merge(setting, bean);
await this.service.saveSetting(userId, setting);
return this.ok({});
}
2024-12-22 14:00:46 +08:00
}