add sendSmsUniCloud

This commit is contained in:
陆柯 2022-03-22 15:50:25 +08:00
parent a7d45c3473
commit dba319adde
2 changed files with 43 additions and 9 deletions

View File

@ -10,15 +10,20 @@ module.exports = {
tls: { rejectUnauthorized: false } tls: { rejectUnauthorized: false }
}, },
SMS: { SMS: {
dxton:{ UNICLOUD: {
appid: '',
smsSecret: '',
smsKey: '',
},
DXTON: {
urlChina:'http://sms.106jiekou.com/utf8/sms.aspx?account=&password=', urlChina:'http://sms.106jiekou.com/utf8/sms.aspx?account=&password=',
urlWorld:'http://sms.106jiekou.com/utf8/worldapi.aspx?account=&password=', urlWorld:'http://sms.106jiekou.com/utf8/worldapi.aspx?account=&password=',
}, },
aliyun:{ ALIYUN: {
accessKeyId:'', accessKeyId:'',
secretAccessKey:'', secretAccessKey:'',
}, },
tencent:{ // https://cloud.tencent.com/document/product/382/43197 TENCENT: { // https://cloud.tencent.com/document/product/382/43197
credential: { credential: {
/* secretIdsecretKey /* secretIdsecretKey
* 这里采用的是从环境变量读取的方式需要在环境变量中先设置这两个值 * 这里采用的是从环境变量读取的方式需要在环境变量中先设置这两个值

View File

@ -16,16 +16,22 @@ module.exports = {
return await util.promisify(my.smtpTransporter.sendMail).call(my.smtpTransporter, option) return await util.promisify(my.smtpTransporter.sendMail).call(my.smtpTransporter, option)
}, },
async sendSms ({ phone, vendor, msg, msgParam, msgTemplate, signName }={}) { async sendSms ({ phone, vendor,
msg, // for vendor==='DXTON'
msgParam, msgTemplate, // for ['ALIYUN','UNICLOUD'].includes(vendor)
signName // for vendor==='ALIYUN'
} = {}) {
// 通过option对象对外提供统一的调用参数格式 // 通过option对象对外提供统一的调用参数格式
if (/^\+\d+-\d+$/.test(phone)) { if (/^\+\d+-\d+$/.test(phone)) {
if (vendor === 'dxton' && msg) { if (vendor === 'DXTON' && msg) {
return await this.sendSmsDxton(phone, msg) return await this.sendSmsDxton(phone, msg)
} else if (vendor === 'aliyun' && msgParam && msgTemplate && signName) { } else if (vendor === 'ALIYUN' && msgParam && msgTemplate && signName) {
return await this.sendSmsAliyun(phone, msgParam, msgTemplate, signName) return await this.sendSmsAliyun(phone, msgParam, msgTemplate, signName)
} else if (vendor === 'UNICLOUD' && msgParam && msgTemplate) {
return await this.sendSmsUnicloud({phone, msgParam, msgTemplate})
} }
} }
return null // 手机号格式错误,或者 option.vendor 错误。 return null // 手机号格式错误,或者 vendor 错误。
}, },
/* 使用 dxton.com 的短信接口 http://www.dxton.com/help_detail/38.html /* 使用 dxton.com 的短信接口 http://www.dxton.com/help_detail/38.html
@ -38,10 +44,10 @@ module.exports = {
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 = wo.envi.SMS.dxton.urlChina smsUrl = wo.envi.SMS.DXTON.urlChina
smsNumber = matches[1] smsNumber = matches[1]
} else { } else {
smsUrl = wo.envi.SMS.dxton.urlWorld // 国际短信不需要签名、模板,可发送任意内容。 smsUrl = wo.envi.SMS.DXTON.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))
@ -95,4 +101,27 @@ module.exports = {
} }
) )
}, },
async sendSmsUniCloud({ phone, msgTemplate, msgParam, appid, smsKey, smsSecret }) {
try {
const result = await uniCloud.sendSms({
appid: appid || my.envi.SMS.UNICLOUD.appid || wo.envi.SMS.UNICLOUD.appid,
smsKey: smsKey || my.envi.SMS.UNICLOUD.smsKey || wo.envi.SMS.UNICLOUD.smsKey,
smsSecret: smsSecret || my.envi.SMS.UNICLOUD.smsSecret || wo.envi.SMS.UNICLOUD.smsSecret,
phone,
templateId: msgTemplate || 'uni_sms_test',
data: msgParam // 模版中的变量的值,例如 { passcode: '234345', purpose: '注册' }
})
// 调用成功,请注意这时不代表发送成功
return Object.assign(result, { _state:'SMS_SENT_SUCCESS' }) // { code:0, errCode:0, success:true }
} catch (error) {
// 调用失败
// {"code":undefined,"msg":"短信发送失败:账户余额不足"}
return {
_state: 'SMS_SEND_FAIL',
error // { errCode, errMsg }
}
}
}
} }