重构,接受 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 ({
|
||||
phone,
|
||||
vendor = my.envar?.SMS?.vendor || wo.envar?.SMS?.vendor,
|
||||
msg, // for vendor==='DXTON'
|
||||
config = my.envar?.SMS || wo.envar?.SMS || {}, // ['ALIYUN','UNICLOUD','TENCENT'].includes(config.vendor)
|
||||
msg, // for 'DXTON'
|
||||
msgParam,
|
||||
msgTemplate, // for ['ALIYUN','UNICLOUD'].includes(vendor)
|
||||
signName, // for vendor==='ALIYUN', 'TENCENT'
|
||||
appid, // for vendor==='UNICLOUD', 'TENCENT'
|
||||
msgTemplate,
|
||||
signName, // for 'ALIYUN', 'TENCENT'
|
||||
appid, // for 'UNICLOUD', 'TENCENT'
|
||||
} = {}) {
|
||||
if (/^\+\d+-\d+$/.test(phone)) {
|
||||
if (vendor === 'DXTON' && msg) {
|
||||
return await this.sendSmsDxton(phone, msg)
|
||||
} else if (vendor === 'ALIYUN' && msgParam && msgTemplate) {
|
||||
return await this.sendSmsAliyun(phone, msgParam, msgTemplate, signName)
|
||||
} else if (vendor === 'UNICLOUD' && msgParam && msgTemplate) {
|
||||
return await this.sendSmsUnicloud({ phone, msgParam, msgTemplate, appid })
|
||||
} else if (vendor === 'TENCENT' && msgParam && msgTemplate) {
|
||||
return await this.sendSmsTencent({ phone, msgParam, msgTemplate, appid, signName })
|
||||
if (/^\+\d+-\d+$/.test(phone) && config.vendor) {
|
||||
if (config.vendor === 'DXTON' && msg) {
|
||||
return await this.sendSmsDxton(phone, msg, config)
|
||||
} else if (config.vendor === 'ALIYUN' && msgParam && msgTemplate) {
|
||||
return await this.sendSmsAliyun(phone, msgParam, msgTemplate, signName, config)
|
||||
} else if (config.vendor === 'UNICLOUD' && msgParam && msgTemplate) {
|
||||
return await this.sendSmsUnicloud({ phone, msgParam, msgTemplate, appid, config })
|
||||
} else if (config.vendor === 'TENCENT' && msgParam && msgTemplate) {
|
||||
return await this.sendSmsTencent({ phone, msgParam, msgTemplate, appid, signName, config })
|
||||
} else {
|
||||
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} 感谢您的注册!
|
||||
- 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 smsNumber, smsUrl
|
||||
if (matches[0] === '86') {
|
||||
smsUrl = dxton.urlChina
|
||||
smsUrl = config.urlChina
|
||||
smsNumber = matches[1]
|
||||
} else {
|
||||
smsUrl = dxton.urlWorld // 国际短信不需要签名、模板,可发送任意内容。
|
||||
smsUrl = config.urlWorld // 国际短信不需要签名、模板,可发送任意内容。
|
||||
smsNumber = matches[0] + matches[1]
|
||||
}
|
||||
// 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) {
|
||||
sender.smsClientAliyun = sender.smsClientAliyun || new (require('@alicloud/sms-sdk'))(aliyun) // https://www.npmjs.com/package/@alicloud/sms-sdk
|
||||
async sendSmsAliyun (phone, msgParam, msgTemplate, signName, config) {
|
||||
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 smsNumber = countryCode === '86' ? callNumber : `00${countryCode}${callNumber}`
|
||||
@ -92,8 +92,8 @@ module.exports = {
|
||||
return await sender.smsClientAliyun
|
||||
.sendSMS({
|
||||
PhoneNumbers: smsNumber, //必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式;发送国际/港澳台消息时,接收号码格式为00+国际区号+号码,如“0085200000000”
|
||||
SignName: aliyun.signName, //必填:短信签名-可在短信控制台中找到
|
||||
TemplateCode: msgTemplate, //必填:短信模板-可在短信控制台中找到,发送国际/港澳台消息时,请使用国际/港澳台短信模版
|
||||
SignName: signName || config.signName, //必填:短信签名-可在短信控制台中找到
|
||||
TemplateCode: msgTemplate || config.msgTemplate, //必填:短信模板-可在短信控制台中找到,发送国际/港澳台消息时,请使用国际/港澳台短信模版
|
||||
TemplateParam: JSON.stringify(msgParam), //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时。
|
||||
})
|
||||
.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 {
|
||||
const result = await uniCloud.sendSms({
|
||||
appid: unicloud.appid,
|
||||
smsKey: unicloud.smsKey,
|
||||
smsSecret: unicloud.smsSecret,
|
||||
appid: appid || config.smsAppid,
|
||||
smsKey: config.smsKey,
|
||||
smsSecret: config.smsSecret,
|
||||
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: '注册' }
|
||||
})
|
||||
|
||||
@ -132,16 +132,16 @@ module.exports = {
|
||||
}
|
||||
},
|
||||
|
||||
async sendSmsTencent ({ phone, msgTemplate, msgParam, tencent = my.envar?.SMS?.TENCENT || wo?.envar?.SMS?.TENCENT } = {}) {
|
||||
sender.smsClientTencent = sender.smsClientTencent || new (require('tencentcloud-sdk-nodejs').sms.v20210111.Client)(tencent) // https://cloud.tencent.com/document/product/382/43197
|
||||
async sendSmsTencent ({ phone, msgTemplate, msgParam, appid, signName, config } = {}) {
|
||||
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
|
||||
.SendSms({
|
||||
// API: https://cloud.tencent.com/document/product/382/55981
|
||||
PhoneNumberSet: [phone.replace('-', '')],
|
||||
SmsSdkAppId: tencent.appid,
|
||||
SignName: tencent.signName,
|
||||
TemplateId: msgTemplate,
|
||||
SmsSdkAppId: appid || config.appid,
|
||||
SignName: signName || config.signName,
|
||||
TemplateId: msgTemplate || config.msgTemplate,
|
||||
TemplateParamSet: Object.values(msgParam),
|
||||
})
|
||||
.then(
|
||||
|
Loading…
Reference in New Issue
Block a user