mirror of
https://github.com/certd/certd.git
synced 2026-04-23 19:57:27 +08:00
perf: 通知标题优化
This commit is contained in:
@@ -12,10 +12,7 @@ export class EmailController extends BaseController {
|
||||
emailService: EmailService;
|
||||
|
||||
@Post('/test', { summary: Constants.per.authOnly })
|
||||
public async test(
|
||||
@Body('receiver')
|
||||
receiver
|
||||
) {
|
||||
public async test(@Body('receiver') receiver) {
|
||||
const userId = super.getUserId();
|
||||
await this.emailService.test(userId, receiver);
|
||||
return this.ok({});
|
||||
|
||||
@@ -56,18 +56,6 @@ export class HandleController extends BaseController {
|
||||
@Post('/notification', { summary: Constants.per.authOnly })
|
||||
async notificationRequest(@Body(ALL) body: NotificationRequestHandleReq) {
|
||||
const input = body.input.body;
|
||||
// if (body.input.id > 0) {
|
||||
// const oldEntity = await this.notificationService.info(body.input.id);
|
||||
// if (oldEntity) {
|
||||
// if (oldEntity.userId !== this.getUserId()) {
|
||||
// throw new Error('notification not found');
|
||||
// }
|
||||
// const param: any = {
|
||||
// type: body.typeName,
|
||||
// setting: JSON.stringify(body.input.access),
|
||||
// };
|
||||
// }
|
||||
// }
|
||||
|
||||
const notification = await newNotification(body.typeName, input, {
|
||||
http,
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import { Inject, Provide } from '@midwayjs/core';
|
||||
import { cache, isDev, randomNumber } from '@certd/basic';
|
||||
import { SysSettingsService } from '@certd/lib-server';
|
||||
import { SysSettingsService, SysSiteInfo } from '@certd/lib-server';
|
||||
import { SmsServiceFactory } from '../sms/factory.js';
|
||||
import { ISmsService } from '../sms/api.js';
|
||||
import { CodeErrorException } from '@certd/lib-server/dist/basic/exception/code-error-exception.js';
|
||||
import { EmailService } from './email-service.js';
|
||||
import { AccessService } from '../../pipeline/service/access-service.js';
|
||||
import { AccessSysGetter } from '../../pipeline/service/access-sys-getter.js';
|
||||
import { isComm } from '@certd/plus-core';
|
||||
|
||||
// {data: '<svg.../svg>', text: 'abcd'}
|
||||
/**
|
||||
@@ -99,9 +100,17 @@ export class CodeService {
|
||||
throw new Error('randomStr不能为空');
|
||||
}
|
||||
|
||||
let siteTitle = 'Certd';
|
||||
if (isComm()) {
|
||||
const siteInfo = await this.sysSettingsService.getSetting<SysSiteInfo>(SysSiteInfo);
|
||||
if (siteInfo) {
|
||||
siteTitle = siteInfo.title || siteTitle;
|
||||
}
|
||||
}
|
||||
|
||||
const code = randomNumber(4);
|
||||
await this.emailService.send({
|
||||
subject: '【Certd】验证码',
|
||||
subject: `【${siteTitle}】验证码`,
|
||||
content: `您的验证码是${code},请勿泄露`,
|
||||
receivers: [email],
|
||||
});
|
||||
|
||||
@@ -91,7 +91,7 @@ export class EmailService implements IEmailService {
|
||||
const mailOptions = {
|
||||
from: `${sysTitle} <${emailConfig.sender}>`,
|
||||
to: email.receivers.join(', '), // list of receivers
|
||||
subject: email.subject,
|
||||
subject: `【${sysTitle}】${email.subject}`,
|
||||
text: email.content,
|
||||
};
|
||||
await transporter.sendMail(mailOptions);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { INotificationService } from '@certd/pipeline';
|
||||
import { INotificationService, NotificationSendReq } from '@certd/pipeline';
|
||||
import { NotificationService } from './notification-service.js';
|
||||
|
||||
export class NotificationGetter implements INotificationService {
|
||||
@@ -17,4 +17,8 @@ export class NotificationGetter implements INotificationService {
|
||||
async getById(id: any) {
|
||||
return await this.notificationService.getById(id, this.userId);
|
||||
}
|
||||
|
||||
async send(req: NotificationSendReq): Promise<void> {
|
||||
return await this.notificationService.send(req, this.userId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
import { Provide, Scope, ScopeEnum } from '@midwayjs/core';
|
||||
import { BaseService, ValidateException } from '@certd/lib-server';
|
||||
import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
|
||||
import { BaseService, SysSettingsService, SysSiteInfo, ValidateException } from '@certd/lib-server';
|
||||
import { InjectEntityModel } from '@midwayjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { NotificationEntity } from '../entity/notification.js';
|
||||
import { NotificationInstanceConfig, notificationRegistry } from '@certd/pipeline';
|
||||
import { NotificationInstanceConfig, notificationRegistry, NotificationSendReq, sendNotification } from '@certd/pipeline';
|
||||
import { http, utils } from '@certd/basic';
|
||||
import { EmailService } from '../../basic/service/email-service.js';
|
||||
import { isComm } from '@certd/plus-core';
|
||||
|
||||
@Provide()
|
||||
@Scope(ScopeEnum.Singleton)
|
||||
@@ -11,6 +14,12 @@ export class NotificationService extends BaseService<NotificationEntity> {
|
||||
@InjectEntityModel(NotificationEntity)
|
||||
repository: Repository<NotificationEntity>;
|
||||
|
||||
@Inject()
|
||||
emailService: EmailService;
|
||||
|
||||
@Inject()
|
||||
sysSettingsService: SysSettingsService;
|
||||
|
||||
//@ts-ignore
|
||||
getRepository() {
|
||||
return this.repository;
|
||||
@@ -124,4 +133,54 @@ export class NotificationService extends BaseService<NotificationEntity> {
|
||||
});
|
||||
return this.buildNotificationInstanceConfig(res);
|
||||
}
|
||||
|
||||
async send(req: NotificationSendReq, userId?: number) {
|
||||
const logger = req.logger;
|
||||
let notifyConfig: NotificationInstanceConfig = null;
|
||||
if (req.id && req.id > 0) {
|
||||
notifyConfig = await this.getById(req.id, userId);
|
||||
if (!notifyConfig) {
|
||||
logger.warn(`未找到通知配置<${req.id}>`);
|
||||
}
|
||||
}
|
||||
if (!notifyConfig) {
|
||||
if (req.id === 0 || req.useDefault) {
|
||||
notifyConfig = await this.getDefault(userId);
|
||||
if (!notifyConfig) {
|
||||
logger.warn(`未找到默认通知配置`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (notifyConfig) {
|
||||
//发送通知
|
||||
logger.info('发送通知, 使用通知渠道:' + notifyConfig.name);
|
||||
|
||||
let siteTitle = 'Certd';
|
||||
if (isComm()) {
|
||||
const siteInfo = await this.sysSettingsService.getSetting<SysSiteInfo>(SysSiteInfo);
|
||||
siteTitle = siteInfo?.title || siteTitle;
|
||||
}
|
||||
req.body.title = `【${siteTitle}】${req.body.title}`;
|
||||
await sendNotification({
|
||||
config: notifyConfig,
|
||||
ctx: {
|
||||
http: http,
|
||||
logger: logger,
|
||||
utils: utils,
|
||||
emailService: this.emailService,
|
||||
},
|
||||
body: req.body,
|
||||
});
|
||||
} else {
|
||||
if (req.useEmail && req.emailAddress) {
|
||||
logger.info('使用邮件通知');
|
||||
await this.emailService.send({
|
||||
receivers: [req.emailAddress],
|
||||
subject: req.body.title,
|
||||
content: req.body.content,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user