优化邮件发送
This commit is contained in:
parent
515d151455
commit
c16817c480
@ -172,19 +172,27 @@ class MailController extends BaseController {
|
|||||||
html,
|
html,
|
||||||
subject,
|
subject,
|
||||||
}
|
}
|
||||||
try {
|
setImmediate(async () => {
|
||||||
let result = await new EmailSvr().sendMail(msgData)
|
try {
|
||||||
record.mailSend = true
|
let { errcode, errmsg, data } = await new EmailSvr().sendMail(msgData)
|
||||||
record.emailId = result.messageId
|
if (errcode) {
|
||||||
record.expiredAt = Date.now() + DEFAULT_EXPIRE_TIME
|
logger.info(`error send mail:: email: ${email}, type: ${type}, errcode: ${errcode}, errmsg: ${errmsg}`)
|
||||||
await record.save()
|
record.status = CodeStatus.FAIL
|
||||||
} catch (err) {
|
} else {
|
||||||
logger.info(`error send mail:: email: ${email}, type: ${type}`)
|
logger.info(`success send mail:: email: ${email}, type: ${type}, messageId: ${data.messageId}`)
|
||||||
logger.error(err)
|
record.mailSend = true
|
||||||
record.status = CodeStatus.FAIL
|
record.emailId = data.messageId
|
||||||
await record.save()
|
record.expiredAt = Date.now() + DEFAULT_EXPIRE_TIME
|
||||||
throw new ZError(14, 'send mail error')
|
}
|
||||||
}
|
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 {}
|
return {}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
@ -28,6 +28,7 @@ export enum CodeStatus {
|
|||||||
*/
|
*/
|
||||||
@dbconn()
|
@dbconn()
|
||||||
@index({ email: 1, type: 1 }, { unique: true, partialFilterExpression: { status: 1 } })
|
@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 } })
|
@index({ code: 1 }, { partialFilterExpression: { status: 1 } })
|
||||||
@modelOptions({
|
@modelOptions({
|
||||||
schemaOptions: { collection: 'code_send_record', timestamps: true },
|
schemaOptions: { collection: 'code_send_record', timestamps: true },
|
||||||
|
@ -27,22 +27,21 @@ export class PlatEmail implements IPlat {
|
|||||||
if (!email || !password || !isEmail(email) || !isCode(password)) {
|
if (!email || !password || !isEmail(email) || !isCode(password)) {
|
||||||
throw new ZError(10, 'params mismatch')
|
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) {
|
if (!recordCode) {
|
||||||
throw new ZError(11, 'code not exists')
|
throw new ZError(11, 'code not exists')
|
||||||
}
|
}
|
||||||
if (recordCode.status !== CodeStatus.PENDING) {
|
if (recordCode.status !== CodeStatus.PENDING) {
|
||||||
throw new ZError(13, 'code expired')
|
throw new ZError(13, 'code expired')
|
||||||
}
|
}
|
||||||
if (recordCode.code !== password) {
|
|
||||||
throw new ZError(14, 'code error')
|
|
||||||
}
|
|
||||||
const openId = sha1(email)
|
const openId = sha1(email)
|
||||||
let data: any = { email, emailReal: email, emailVerified: true }
|
let data: any = { email, emailReal: email, emailVerified: true }
|
||||||
const { api_platform } = req.headers
|
const { api_platform } = req.headers
|
||||||
if (api_platform) {
|
if (api_platform) {
|
||||||
data.platform = api_platform
|
data.platform = api_platform
|
||||||
}
|
}
|
||||||
|
recordCode.status = CodeStatus.SUCCESS
|
||||||
|
await recordCode.save()
|
||||||
|
|
||||||
return { openId, data }
|
return { openId, data }
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user