diff --git a/src/controllers/mail.controller.ts b/src/controllers/mail.controller.ts index 5d7ac94..ebbc5eb 100644 --- a/src/controllers/mail.controller.ts +++ b/src/controllers/mail.controller.ts @@ -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 {} } /** diff --git a/src/modules/CodeRecord.ts b/src/modules/CodeRecord.ts index d8e1ca4..fefb972 100644 --- a/src/modules/CodeRecord.ts +++ b/src/modules/CodeRecord.ts @@ -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 }, diff --git a/src/plats/PlatEmail.ts b/src/plats/PlatEmail.ts index b946490..2f04029 100644 --- a/src/plats/PlatEmail.ts +++ b/src/plats/PlatEmail.ts @@ -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 } }