From c16817c480d653261bbf620aef4704a82d0e29f7 Mon Sep 17 00:00:00 2001 From: CounterFire2023 <136581895+CounterFire2023@users.noreply.github.com> Date: Wed, 15 May 2024 16:03:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=82=AE=E4=BB=B6=E5=8F=91?= =?UTF-8?q?=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/mail.controller.ts | 34 ++++++++++++++++++------------ src/modules/CodeRecord.ts | 1 + src/plats/PlatEmail.ts | 7 +++--- 3 files changed, 25 insertions(+), 17 deletions(-) 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 } }