mirror of
https://github.com/certd/certd.git
synced 2026-04-15 05:00:52 +08:00
perf: 邮箱设置改为系统设置,普通用户无需配置发件邮箱
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
|
||||
import type { EmailSend } from '@certd/pipeline';
|
||||
import { IEmailService, isPlus } from '@certd/pipeline';
|
||||
import { IEmailService, isPlus, logger } from '@certd/pipeline';
|
||||
import nodemailer from 'nodemailer';
|
||||
import type SMTPConnection from 'nodemailer/lib/smtp-connection';
|
||||
import { logger } from '@certd/pipeline';
|
||||
import { UserSettingsService } from '../../mine/service/user-settings-service.js';
|
||||
import { PlusService } from '@certd/lib-server';
|
||||
import { PlusService, SysEmailConf, SysSettingsService } from '@certd/lib-server';
|
||||
import { merge } from 'lodash-es';
|
||||
|
||||
export type EmailConfig = {
|
||||
host: string;
|
||||
@@ -27,6 +27,9 @@ export type EmailConfig = {
|
||||
export class EmailService implements IEmailService {
|
||||
@Inject()
|
||||
settingsService: UserSettingsService;
|
||||
|
||||
@Inject()
|
||||
sysSettingsService: SysSettingsService;
|
||||
@Inject()
|
||||
plusService: PlusService;
|
||||
|
||||
@@ -55,21 +58,30 @@ export class EmailService implements IEmailService {
|
||||
/**
|
||||
*/
|
||||
async send(email: EmailSend) {
|
||||
console.log('sendEmail', email);
|
||||
logger.info('sendEmail', email);
|
||||
|
||||
const emailConfigEntity = await this.settingsService.getByKey('email', email.userId);
|
||||
if (emailConfigEntity == null || !emailConfigEntity.setting) {
|
||||
const emailConf = await this.sysSettingsService.getSetting<SysEmailConf>(SysEmailConf);
|
||||
if (!emailConf.host && emailConf.usePlus != null) {
|
||||
const emailConfigEntity = await this.settingsService.getByKey('email', 1);
|
||||
if (emailConfigEntity) {
|
||||
const emailConfig = JSON.parse(emailConfigEntity.setting) as EmailConfig;
|
||||
merge(emailConf, emailConfig);
|
||||
await this.sysSettingsService.saveSetting(emailConf);
|
||||
}
|
||||
}
|
||||
|
||||
if (!emailConf.host) {
|
||||
if (isPlus()) {
|
||||
//自动使用plus发邮件
|
||||
return await this.sendByPlus(email);
|
||||
}
|
||||
throw new Error('email settings 未设置');
|
||||
throw new Error('email settings 还未设置');
|
||||
}
|
||||
const emailConfig = JSON.parse(emailConfigEntity.setting) as EmailConfig;
|
||||
if (emailConfig.usePlus && isPlus()) {
|
||||
|
||||
if (emailConf.usePlus && isPlus()) {
|
||||
return await this.sendByPlus(email);
|
||||
}
|
||||
await this.sendByCustom(emailConfig, email);
|
||||
await this.sendByCustom(emailConf, email);
|
||||
logger.info('sendEmail complete: ', email);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user