UNPKG

create-chuntianxiaozhu

Version:

春天小猪模板工具

86 lines (81 loc) 2.15 kB
import { ISendMailOptions, MailerService } from '@nestjs-modules/mailer'; import { BadRequestException, Injectable, Logger } from '@nestjs/common'; import { generateCode, isDev } from 'api/utils/common'; @Injectable() export class EmailService { constructor(private readonly mailerService: MailerService) {} /** * 发送注册或者登录验证码 * @param toAddress * @returns */ async sendValidecode(toAddress: string) { if (isDev()) { // 如果是开发环境直接返回code,不发送邮件 return '111111'; } const code = generateCode(); try { const sendMailOptions: ISendMailOptions = { to: toAddress, subject: '邮箱验证', template: 'validate.code.ejs', context: { code, }, }; await this.mailerService.sendMail(sendMailOptions); return code; } catch (e) { console.log(e); throw new BadRequestException('发送邮件错误'); } } /** * 发送成功入驻设计者邮件 * @param toAddress * @param name */ async sendDesignerSettleIn(toAddress: string, name: string) { try { const sendMailOptions: ISendMailOptions = { to: toAddress, subject: '成功入驻春天小猪设计者', template: 'designer.settlein.ejs', context: { name, }, }; this.mailerService.sendMail(sendMailOptions); } catch { Logger.error('发送邮件错误'); } } /** * 入驻验证码 * @param toAddress * @returns */ async sendSettleInCode(toAddress: string) { if (isDev()) { // 如果是开发环境直接返回code,不发送邮件 return '111111'; } const code = generateCode(); try { const sendMailOptions: ISendMailOptions = { to: toAddress, subject: '入驻验证', template: 'settlein.code.ejs', context: { code, }, }; await this.mailerService.sendMail(sendMailOptions); return code; } catch (e) { console.log(e); throw new BadRequestException('发送邮件错误'); } } }