perf: 支持企业微信群聊机器人通知

This commit is contained in:
xiaojunnuo
2024-11-23 23:58:31 +08:00
parent 5450246f06
commit b805a29259
19 changed files with 163 additions and 41 deletions
@@ -6,7 +6,7 @@ export class NotificationEntity {
id: number;
@Column({ name: 'user_id', comment: 'UserId' })
userId: string;
userId: number;
@Column({ name: 'type', comment: '通知类型' })
type: string;
@@ -0,0 +1,14 @@
import { INotificationService } from '@certd/pipeline';
export class NotificationGetter implements INotificationService {
userId: number;
getter: <T>(id: any, userId?: number) => Promise<T>;
constructor(userId: number, getter: (id: any, userId: number) => Promise<any>) {
this.userId = userId;
this.getter = getter;
}
async getById<T = any>(id: any) {
return await this.getter<T>(id, this.userId);
}
}
@@ -3,7 +3,7 @@ import { BaseService, ValidateException } from '@certd/lib-server';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { NotificationEntity } from '../entity/notification.js';
import { notificationRegistry } from '@certd/pipeline';
import { NotificationInstanceConfig, notificationRegistry } from '@certd/pipeline';
@Provide()
@Scope(ScopeEnum.Singleton)
@@ -35,4 +35,29 @@ export class NotificationService extends BaseService<NotificationEntity> {
getDefineByType(type: string) {
return notificationRegistry.getDefine(type);
}
async getById(id: number, userId: number): Promise<NotificationInstanceConfig> {
if (!id) {
throw new ValidateException('id不能为空');
}
if (!userId) {
throw new ValidateException('userId不能为空');
}
const res = await this.repository.findOne({
where: {
id,
userId,
},
});
if (!res) {
throw new ValidateException('通知配置不存在');
}
const setting = JSON.parse(res.setting);
return {
id: res.id,
type: res.type,
userId: res.userId,
setting,
};
}
}
@@ -23,6 +23,9 @@ import dayjs from 'dayjs';
import { DbAdapter } from '../../db/index.js';
import { isPlus } from '@certd/plus-core';
import { logger } from '@certd/basic';
import { UrlService } from './url-service.js';
import { NotificationService } from './notification-service.js';
import { NotificationGetter } from './notification-getter.js';
const runningTasks: Map<string | number, Executor> = new Map();
const freeCount = 10;
@@ -63,6 +66,12 @@ export class PipelineService extends BaseService<PipelineEntity> {
@Config('certd')
private certdConfig: any;
@Inject()
urlService: UrlService;
@Inject()
notificationService: NotificationService;
@Inject()
dbAdapter: DbAdapter;
@@ -384,6 +393,7 @@ export class PipelineService extends BaseService<PipelineEntity> {
};
const accessGetter = new AccessGetter(userId, this.accessService.getById.bind(this.accessService));
const cnameProxyService = new CnameProxyService(userId, this.cnameRecordService.getWithAccessByDomain.bind(this.cnameRecordService));
const notificationGetter = new NotificationGetter(userId, this.notificationService.getById.bind(this.notificationService));
const executor = new Executor({
user,
pipeline,
@@ -393,6 +403,8 @@ export class PipelineService extends BaseService<PipelineEntity> {
pluginConfigService: this.pluginConfigGetter,
storage: new DbStorage(userId, this.storageService),
emailService: this.emailService,
urlService: this.urlService,
notificationService: notificationGetter,
fileRootDir: this.certdConfig.fileRootDir,
});
try {
@@ -0,0 +1,19 @@
import { IUrlService } from '@certd/pipeline';
import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
import { SysInstallInfo, SysSettingsService } from '@certd/lib-server';
@Provide()
@Scope(ScopeEnum.Singleton)
export class UrlService implements IUrlService {
@Inject()
sysSettingsService: SysSettingsService;
async getPipelineDetailUrl(pipelineId: number, historyId: number): Promise<string> {
const installInfo = await this.sysSettingsService.getSetting<SysInstallInfo>(SysInstallInfo);
let baseUrl = 'http://127.0.0.1:7001';
if (installInfo.bindUrl) {
baseUrl = installInfo.bindUrl;
}
return `${baseUrl}#/certd/pipeline/detail?id=${pipelineId}&historyId=${historyId}`;
}
}
@@ -42,9 +42,10 @@ export class QywxNotification extends BaseNotification {
await this.http.request({
url: this.webhook,
method: 'POST',
data: {
msgtype: 'markdown',
text: {
markdown: {
content: `# ${body.title}\n\n${body.content}\n[查看详情](${body.url})`,
mentioned_list: this.mentionedList,
},