优化邮件发送

This commit is contained in:
CounterFire2023 2024-05-15 16:03:09 +08:00
parent 515d151455
commit c16817c480
3 changed files with 25 additions and 17 deletions

View File

@ -172,19 +172,27 @@ class MailController extends BaseController {
html,
subject,
}
try {
let result = await new EmailSvr().sendMail(msgData)
record.mailSend = true
record.emailId = result.messageId
record.expiredAt = Date.now() + DEFAULT_EXPIRE_TIME
await record.save()
} catch (err) {
logger.info(`error send mail:: email: ${email}, type: ${type}`)
logger.error(err)
record.status = CodeStatus.FAIL
await record.save()
throw new ZError(14, 'send mail error')
}
setImmediate(async () => {
try {
let { errcode, errmsg, data } = await new EmailSvr().sendMail(msgData)
if (errcode) {
logger.info(`error send mail:: email: ${email}, type: ${type}, errcode: ${errcode}, errmsg: ${errmsg}`)
record.status = CodeStatus.FAIL
} else {
logger.info(`success send mail:: email: ${email}, type: ${type}, messageId: ${data.messageId}`)
record.mailSend = true
record.emailId = data.messageId
record.expiredAt = Date.now() + DEFAULT_EXPIRE_TIME
}
await record.save()
} catch (err) {
logger.info(`error send mail:: email: ${email}, type: ${type}`)
logger.error(err)
record.status = CodeStatus.FAIL
await record.save()
throw new ZError(14, 'send mail error')
}
})
return {}
}
/**

View File

@ -28,6 +28,7 @@ export enum CodeStatus {
*/
@dbconn()
@index({ email: 1, type: 1 }, { unique: true, partialFilterExpression: { status: 1 } })
@index({ email: 1, type: 1, code: 1 }, { unique: false })
@index({ code: 1 }, { partialFilterExpression: { status: 1 } })
@modelOptions({
schemaOptions: { collection: 'code_send_record', timestamps: true },

View File

@ -27,22 +27,21 @@ export class PlatEmail implements IPlat {
if (!email || !password || !isEmail(email) || !isCode(password)) {
throw new ZError(10, 'params mismatch')
}
let recordCode = await CodeRecord.findByEmail(email, CodeType.LOGIN)
let recordCode = await CodeRecord.findOne({ email, type: CodeType.LOGIN, code: password })
if (!recordCode) {
throw new ZError(11, 'code not exists')
}
if (recordCode.status !== CodeStatus.PENDING) {
throw new ZError(13, 'code expired')
}
if (recordCode.code !== password) {
throw new ZError(14, 'code error')
}
const openId = sha1(email)
let data: any = { email, emailReal: email, emailVerified: true }
const { api_platform } = req.headers
if (api_platform) {
data.platform = api_platform
}
recordCode.status = CodeStatus.SUCCESS
await recordCode.save()
return { openId, data }
}