添加 init(envi) 方法来初始化;把phone参数合并到第二个{...}参数里

This commit is contained in:
Luk Lu
2022-03-09 15:54:33 +08:00
parent 03b896d0fa
commit a7d45c3473
2 changed files with 48 additions and 23 deletions

View File

@@ -1,27 +1,28 @@
const util = require('util')
//const RequestPromise = require('request-promise-native') // request-promise/-native。https://www.npmjs.com/package/request-promise. 还看到一个方法Bluebird.promisifyAll(require("request"));
const http = require('http')
const NodeMailer = require('nodemailer') // 或者 const smtpTransporter=require('nodemailer').createTransport({host:'', port:25, auth:{user:'',pass:''}})
const NodeMailer = require('nodemailer')
let smtpTransporter = null
let smsClient = null // 在调用时,才创建 smsClient防止 wo.envi 还没有建立好。
const my = {}
module.exports = {
init (envi) {
my.envi = envi
my.smtpTransporter = NodeMailer.createTransport(envi.SMTP)
my.smsClient = new (require('@alicloud/sms-sdk'))(envi.SMS.aliyun) // https://www.npmjs.com/package/@alicloud/sms-sdk
},
async sendMail (option) {
// 或者如果smtp参数已经确定就可以直接定义 sendMail: Bluebird.promisify(Smtp.sendMail).bind(Smtp)
smtpTransporter = smtpTransporter || NodeMailer.createTransport(wo.envi.SMTP)
return await util.promisify(smtpTransporter.sendMail).call(smtpTransporter, option)
async sendMail (option) { // 或者如果smtp参数已经确定就可以直接定义 sendMail: Bluebird.promisify(Smtp.sendMail).bind(Smtp)
my.smtpTransporter = my.smtpTransporter || NodeMailer.createTransport(wo.envi.SMTP)
return await util.promisify(my.smtpTransporter.sendMail).call(my.smtpTransporter, option)
},
async sendSms (phone, { vendor, msg, msgParam, templateCode, signName }={}) {
async sendSms ({ phone, vendor, msg, msgParam, msgTemplate, signName }={}) {
// 通过option对象对外提供统一的调用参数格式
if (/^\+\d+-\d+$/.test(phone)) {
if (vendor === 'dxton' && msg) {
return await this.sendSmsDxton(phone, msg)
} else if (vendor === 'aliyun' && msgParam && templateCode && signName) {
return await this.sendSmsAliyun(phone, msgParam, templateCode, signName)
} else if (vendor === 'aliyun' && msgParam && msgTemplate && signName) {
return await this.sendSmsAliyun(phone, msgParam, msgTemplate, signName)
}
}
return null // 手机号格式错误,或者 option.vendor 错误。
@@ -63,15 +64,8 @@ module.exports = {
},
async sendSmsAliyun (phone, msgParam, templateCode, signName) {
// msgParam 是消息模板参数对象,例如 { code: "890353" }
smsClient =
smsClient ||
new (require('@alicloud/sms-sdk'))({
// 在调用时,才创建 smsClient防止 wo.envi 还没有建立好。
accessKeyId: wo.envi.SMS.aliyun.accessKeyId,
secretAccessKey: wo.envi.SMS.aliyun.secretAccessKey,
})
async sendSmsAliyun (phone, msgParam, msgTemplate, signName) {
my.smsClient = my.smsClient || new (require('@alicloud/sms-sdk'))(wo.envi.SMS.aliyun)
var matches = phone.match(/\d+/g)
var smsNumber
@@ -80,11 +74,11 @@ module.exports = {
} else {
smsNumber = '00' + matches[0] + matches[1]
}
return await smsClient
return await my.smsClient
.sendSMS({
PhoneNumbers: smsNumber, //必填:待发送手机号。支持以逗号分隔的形式进行批量调用批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式;发送国际/港澳台消息时接收号码格式为00+国际区号+号码如“0085200000000”
SignName: signName, //必填:短信签名-可在短信控制台中找到
TemplateCode: templateCode, //必填:短信模板-可在短信控制台中找到,发送国际/港澳台消息时,请使用国际/港澳台短信模版
TemplateCode: msgTemplate, //必填:短信模板-可在短信控制台中找到,发送国际/港澳台消息时,请使用国际/港澳台短信模版
TemplateParam: JSON.stringify(msgParam), //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时。
})
.then(