This commit is contained in:
Luk 2025-03-15 14:14:24 +08:00
parent 292e138732
commit d27531ea00

View File

@ -14,8 +14,8 @@ module.exports = {
// 或者如果smtp参数已经确定就可以直接定义 sendMail: Bluebird.promisify(Smtp.sendMail).bind(Smtp)
async sendMail (messageObject, smtp = my.envar?.SMTP || wo?.envar?.SMTP) {
if (!smtp?.host) {
console.error({ _at: new Date().toJSON(), _state: 'SMTP_CONFIG_MALFORMED' }, '\n,')
return { _state: 'SMTP_CONFIG_MALFORMED' }
globalThis.wo?.ccerror?.({ _state: 'SMTP_CONFIG_MALFORMED', smtp })
return { _state: 'SMTP_CONFIG_MALFORMED', smtp }
}
// messageObject: { from, to, cc, bcc, subject, text, html, sender, replyTo, inReplyTo }
sender.smtpTransporter = sender.smtpTransporter || NodeMailer.createTransport(smtp)
@ -35,15 +35,15 @@ module.exports = {
}
*/
if (result.messageId && result.response === '250 Ok') {
console.log({ _at: new Date().toJSON(), _state: 'MSG_SENT_SUCCESS', subject: messageObject.subject, ...result.envelope }, '\n,')
globalThis.wo?.cclog?.({ _state: 'MSG_SENT_SUCCESS', subject: messageObject.subject, ...result.envelope })
return { _state: 'MSG_SENT_SUCCESS' }
} else {
console.log({ _at: new Date().toJSON(), _state: 'MSG_SEND_FAIL', subject: messageObject.subject, ...result }, '\n,')
globalThis.wo?.cclog?.({ _state: 'MSG_SEND_FAIL', subject: messageObject.subject, ...result })
return { _state: 'MSG_SEND_FAIL' }
}
})
.catch((error) => {
console.error({ _at: new Date().toJSON(), _state: 'MSG_SEND_ERROR', subject: messageObject.subject, error }, '\n,')
globalThis.wo?.ccerror?.({ _state: 'MSG_SEND_ERROR', subject: messageObject.subject, error })
return { _state: 'MSG_SEND_ERROR' }
})
},
@ -58,24 +58,28 @@ module.exports = {
signName, // for 'ALIYUN', 'TENCENT'
appid, // for 'UNICLOUD', 'TENCENT'
} = {}) {
let result
if (/^\+\d+-\d+$/.test(phone)) {
if (!config?.vendor) {
console.error({ _at: new Date().toJSON(), _state: 'SMS_CONFIG_MALFORMED', config }, '\n,')
return { _state: 'SMS_CONFIG_MALFORMED', phone, config }
result = { _state: 'SMS_CONFIG_MALFORMED', phone, config }
} else if (config.vendor === 'DXTON') {
return await this.sendSmsDxton({ phone, config, msg })
result = await this.sendSmsDxton({ phone, config, msg })
} else if (config.vendor === 'ALIYUN') {
return await this.sendSmsAliyun({ phone, config, msgParam, msgTemplate, signName })
result = await this.sendSmsAliyun({ phone, config, msgParam, msgTemplate, signName })
} else if (config.vendor === 'TENCENT') {
return await this.sendSmsTencent({ phone, config, msgParam, msgTemplate, appid, signName })
result = await this.sendSmsTencent({ phone, config, msgParam, msgTemplate, appid, signName })
} else if (config.vendor === 'UNICLOUD') {
return await this.sendSmsUnicloud({ phone, config, msgParam, msgTemplate, appid })
result = await this.sendSmsUnicloud({ phone, config, msgParam, msgTemplate, appid })
} else {
return { _state: 'SMS_UNKNOWN_VENDOR', error: { unknownVendor: config.vendor } }
result = { _state: 'SMS_UNKNOWN_VENDOR', error: { unknownVendor: config.vendor } }
}
} else {
return { _state: 'SMS_INVALID_PHONE', error: {} }
result = { _state: 'SMS_INVALID_PHONE', error: {} }
}
globalThis.wo?.cclog?.({ phone, signName, msgParam, msgTemplate, ...result })
return result
},
/* 使用 dxton.com 的短信接口 http://www.dxton.com/help_detail/38.html
@ -136,12 +140,10 @@ module.exports = {
if (Code === 'OK') {
return { _state: 'MSG_SENT_SUCCESS' }
} else {
console.error({ _at: new Date().toJSON(), _state: 'MSG_SEND_FAIL', error: result }, '\n,')
return { _state: 'MSG_SEND_FAIL', error: result }
}
},
function (error) {
console.error({ _at: new Date().toJSON(), _state: 'MSG_SEND_ERROR', error }, '\n,')
return { _state: 'MSG_SEND_ERROR', error }
}
)
@ -165,12 +167,10 @@ module.exports = {
if (Code === 'Ok') {
return { _state: 'MSG_SENT_SUCCESS' }
} else {
console.error({ _at: new Date().toJSON(), _state: 'MSG_SEND_FAIL', error: { SendStatusSet, RequestId } }, '\n,')
return { _state: 'MSG_SEND_FAIL', error: { SendStatusSet, RequestId } }
}
},
function (error) {
console.error({ _at: new Date().toJSON(), _state: 'MSG_SEND_ERROR', error }, '\n,')
return { _state: 'MSG_SEND_ERROR', error }
}
)
@ -191,12 +191,10 @@ module.exports = {
return { _state: 'MSG_SENT_SUCCESS', result } // { code:0, errCode:0, success:true }
} else {
// 错误码参见 https://doc.dcloud.net.cn/uniCloud/sms/dev.html
console.error({ _at: new Date().toJSON(), _state: 'MSG_SEND_FAIL', error: result }, '\n,')
return { _state: 'MSG_SEND_FAIL', result }
}
} catch (error) {
// 调用失败 例如 {"code":undefined,"msg":"短信发送失败:账户余额不足"}
console.error({ _at: new Date().toJSON(), _state: 'MSG_SEND_ERROR', error }, '\n,')
return {
_state: 'MSG_SEND_ERROR',
error, // { errCode, errMsg }