重构,接受 appid/signName/msgTemplate/config 参数,根据 config.vendor 分流
This commit is contained in:
parent
953eba3fe2
commit
f58f12ff43
62
messenger.js
62
messenger.js
@ -20,22 +20,22 @@ module.exports = {
|
|||||||
|
|
||||||
async sendSms ({
|
async sendSms ({
|
||||||
phone,
|
phone,
|
||||||
vendor = my.envar?.SMS?.vendor || wo.envar?.SMS?.vendor,
|
config = my.envar?.SMS || wo.envar?.SMS || {}, // ['ALIYUN','UNICLOUD','TENCENT'].includes(config.vendor)
|
||||||
msg, // for vendor==='DXTON'
|
msg, // for 'DXTON'
|
||||||
msgParam,
|
msgParam,
|
||||||
msgTemplate, // for ['ALIYUN','UNICLOUD'].includes(vendor)
|
msgTemplate,
|
||||||
signName, // for vendor==='ALIYUN', 'TENCENT'
|
signName, // for 'ALIYUN', 'TENCENT'
|
||||||
appid, // for vendor==='UNICLOUD', 'TENCENT'
|
appid, // for 'UNICLOUD', 'TENCENT'
|
||||||
} = {}) {
|
} = {}) {
|
||||||
if (/^\+\d+-\d+$/.test(phone)) {
|
if (/^\+\d+-\d+$/.test(phone) && config.vendor) {
|
||||||
if (vendor === 'DXTON' && msg) {
|
if (config.vendor === 'DXTON' && msg) {
|
||||||
return await this.sendSmsDxton(phone, msg)
|
return await this.sendSmsDxton(phone, msg, config)
|
||||||
} else if (vendor === 'ALIYUN' && msgParam && msgTemplate) {
|
} else if (config.vendor === 'ALIYUN' && msgParam && msgTemplate) {
|
||||||
return await this.sendSmsAliyun(phone, msgParam, msgTemplate, signName)
|
return await this.sendSmsAliyun(phone, msgParam, msgTemplate, signName, config)
|
||||||
} else if (vendor === 'UNICLOUD' && msgParam && msgTemplate) {
|
} else if (config.vendor === 'UNICLOUD' && msgParam && msgTemplate) {
|
||||||
return await this.sendSmsUnicloud({ phone, msgParam, msgTemplate, appid })
|
return await this.sendSmsUnicloud({ phone, msgParam, msgTemplate, appid, config })
|
||||||
} else if (vendor === 'TENCENT' && msgParam && msgTemplate) {
|
} else if (config.vendor === 'TENCENT' && msgParam && msgTemplate) {
|
||||||
return await this.sendSmsTencent({ phone, msgParam, msgTemplate, appid, signName })
|
return await this.sendSmsTencent({ phone, msgParam, msgTemplate, appid, signName, config })
|
||||||
} else {
|
} else {
|
||||||
return { _state: 'SMS_UNKNOWN_VENDOR', error: {} }
|
return { _state: 'SMS_UNKNOWN_VENDOR', error: {} }
|
||||||
}
|
}
|
||||||
@ -50,14 +50,14 @@ module.exports = {
|
|||||||
- 国外: http://sms.106jiekou.com/utf8/worldapi.aspx?account=9999&password=接口密码&mobile=手机号码&content=尊敬的用户您已经注册成功,用户名:{0} 密码:{1} 感谢您的注册!
|
- 国外: http://sms.106jiekou.com/utf8/worldapi.aspx?account=9999&password=接口密码&mobile=手机号码&content=尊敬的用户您已经注册成功,用户名:{0} 密码:{1} 感谢您的注册!
|
||||||
- response 的 content-type 为 text/html
|
- response 的 content-type 为 text/html
|
||||||
*/
|
*/
|
||||||
async sendSmsDxton (phone, msg, dxton = my.envar?.SMS?.DXTON || wo?.envar?.SMS?.DXTON) {
|
async sendSmsDxton (phone, msg, config) {
|
||||||
var matches = phone.match(/\d+/g)
|
var matches = phone.match(/\d+/g)
|
||||||
var smsNumber, smsUrl
|
var smsNumber, smsUrl
|
||||||
if (matches[0] === '86') {
|
if (matches[0] === '86') {
|
||||||
smsUrl = dxton.urlChina
|
smsUrl = config.urlChina
|
||||||
smsNumber = matches[1]
|
smsNumber = matches[1]
|
||||||
} else {
|
} else {
|
||||||
smsUrl = dxton.urlWorld // 国际短信不需要签名、模板,可发送任意内容。
|
smsUrl = config.urlWorld // 国际短信不需要签名、模板,可发送任意内容。
|
||||||
smsNumber = matches[0] + matches[1]
|
smsNumber = matches[0] + matches[1]
|
||||||
}
|
}
|
||||||
// let returnCode = await RequestPromise.get(smsUrl + '&mobile=' + smsNumber + '&content=' + encodeURIComponent(msg))
|
// let returnCode = await RequestPromise.get(smsUrl + '&mobile=' + smsNumber + '&content=' + encodeURIComponent(msg))
|
||||||
@ -83,8 +83,8 @@ module.exports = {
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
async sendSmsAliyun (phone, msgParam, msgTemplate, aliyun = my.envar?.SMS?.ALIYUN || wo?.envar?.SMS?.ALIYUN) {
|
async sendSmsAliyun (phone, msgParam, msgTemplate, signName, config) {
|
||||||
sender.smsClientAliyun = sender.smsClientAliyun || new (require('@alicloud/sms-sdk'))(aliyun) // https://www.npmjs.com/package/@alicloud/sms-sdk
|
sender.smsClientAliyun = sender.smsClientAliyun || new (require('@alicloud/sms-sdk'))(config) // https://www.npmjs.com/package/@alicloud/sms-sdk
|
||||||
|
|
||||||
const [countryCode, callNumber] = phone.match(/\d+/g)
|
const [countryCode, callNumber] = phone.match(/\d+/g)
|
||||||
const smsNumber = countryCode === '86' ? callNumber : `00${countryCode}${callNumber}`
|
const smsNumber = countryCode === '86' ? callNumber : `00${countryCode}${callNumber}`
|
||||||
@ -92,8 +92,8 @@ module.exports = {
|
|||||||
return await sender.smsClientAliyun
|
return await sender.smsClientAliyun
|
||||||
.sendSMS({
|
.sendSMS({
|
||||||
PhoneNumbers: smsNumber, //必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式;发送国际/港澳台消息时,接收号码格式为00+国际区号+号码,如“0085200000000”
|
PhoneNumbers: smsNumber, //必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式;发送国际/港澳台消息时,接收号码格式为00+国际区号+号码,如“0085200000000”
|
||||||
SignName: aliyun.signName, //必填:短信签名-可在短信控制台中找到
|
SignName: signName || config.signName, //必填:短信签名-可在短信控制台中找到
|
||||||
TemplateCode: msgTemplate, //必填:短信模板-可在短信控制台中找到,发送国际/港澳台消息时,请使用国际/港澳台短信模版
|
TemplateCode: msgTemplate || config.msgTemplate, //必填:短信模板-可在短信控制台中找到,发送国际/港澳台消息时,请使用国际/港澳台短信模版
|
||||||
TemplateParam: JSON.stringify(msgParam), //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时。
|
TemplateParam: JSON.stringify(msgParam), //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时。
|
||||||
})
|
})
|
||||||
.then(
|
.then(
|
||||||
@ -111,14 +111,14 @@ module.exports = {
|
|||||||
)
|
)
|
||||||
},
|
},
|
||||||
|
|
||||||
async sendSmsUnicloud ({ phone, msgTemplate, msgParam, unicloud = my.envar?.SMS?.UNICLOUD || wo?.envar?.SMS?.UNICLOUD } = {}) {
|
async sendSmsUnicloud ({ phone, msgTemplate, msgParam, appid, config } = {}) {
|
||||||
try {
|
try {
|
||||||
const result = await uniCloud.sendSms({
|
const result = await uniCloud.sendSms({
|
||||||
appid: unicloud.appid,
|
appid: appid || config.smsAppid,
|
||||||
smsKey: unicloud.smsKey,
|
smsKey: config.smsKey,
|
||||||
smsSecret: unicloud.smsSecret,
|
smsSecret: config.smsSecret,
|
||||||
phone: phone.match(/\d+/g)[1],
|
phone: phone.match(/\d+/g)[1],
|
||||||
templateId: msgTemplate || 'uni_sms_test',
|
templateId: msgTemplate || config.msgTemplate || 'uni_sms_test', // for test only, max 100 messages for 10 phones at most per day.
|
||||||
data: msgParam, // 模版中的变量的值,例如 { passcode: '234345', purpose: '注册' }
|
data: msgParam, // 模版中的变量的值,例如 { passcode: '234345', purpose: '注册' }
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -132,16 +132,16 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
async sendSmsTencent ({ phone, msgTemplate, msgParam, tencent = my.envar?.SMS?.TENCENT || wo?.envar?.SMS?.TENCENT } = {}) {
|
async sendSmsTencent ({ phone, msgTemplate, msgParam, appid, signName, config } = {}) {
|
||||||
sender.smsClientTencent = sender.smsClientTencent || new (require('tencentcloud-sdk-nodejs').sms.v20210111.Client)(tencent) // https://cloud.tencent.com/document/product/382/43197
|
sender.smsClientTencent = sender.smsClientTencent || new (require('tencentcloud-sdk-nodejs').sms.v20210111.Client)(config) // https://cloud.tencent.com/document/product/382/43197
|
||||||
|
|
||||||
return await sender.smsClientTencent
|
return await sender.smsClientTencent
|
||||||
.SendSms({
|
.SendSms({
|
||||||
// API: https://cloud.tencent.com/document/product/382/55981
|
// API: https://cloud.tencent.com/document/product/382/55981
|
||||||
PhoneNumberSet: [phone.replace('-', '')],
|
PhoneNumberSet: [phone.replace('-', '')],
|
||||||
SmsSdkAppId: tencent.appid,
|
SmsSdkAppId: appid || config.appid,
|
||||||
SignName: tencent.signName,
|
SignName: signName || config.signName,
|
||||||
TemplateId: msgTemplate,
|
TemplateId: msgTemplate || config.msgTemplate,
|
||||||
TemplateParamSet: Object.values(msgParam),
|
TemplateParamSet: Object.values(msgParam),
|
||||||
})
|
})
|
||||||
.then(
|
.then(
|
||||||
|
Loading…
Reference in New Issue
Block a user