From dba319addeab8956907faac34338be11334eebb8 Mon Sep 17 00:00:00 2001 From: Luk Lu Date: Tue, 22 Mar 2022 15:50:25 +0800 Subject: [PATCH] add sendSmsUniCloud --- ConfigTemplate.js | 11 ++++++++--- messenger.js | 41 +++++++++++++++++++++++++++++++++++------ 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/ConfigTemplate.js b/ConfigTemplate.js index 83caa04..321cf92 100644 --- a/ConfigTemplate.js +++ b/ConfigTemplate.js @@ -10,15 +10,20 @@ module.exports = { tls: { rejectUnauthorized: false } }, SMS: { - dxton:{ + UNICLOUD: { + appid: '????', + smsSecret: '????', + smsKey: '????', + }, + DXTON: { urlChina:'http://sms.106jiekou.com/utf8/sms.aspx?account=????&password=????', urlWorld:'http://sms.106jiekou.com/utf8/worldapi.aspx?account=????&password=????', }, - aliyun:{ + ALIYUN: { accessKeyId:'????', secretAccessKey:'????', }, - tencent:{ // https://cloud.tencent.com/document/product/382/43197 + TENCENT: { // https://cloud.tencent.com/document/product/382/43197 credential: { /* 必填:腾讯云账户密钥对secretId,secretKey。 * 这里采用的是从环境变量读取的方式,需要在环境变量中先设置这两个值。 diff --git a/messenger.js b/messenger.js index cb9532c..0e799f1 100644 --- a/messenger.js +++ b/messenger.js @@ -16,16 +16,22 @@ module.exports = { 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对象,对外提供统一的调用参数格式 if (/^\+\d+-\d+$/.test(phone)) { - if (vendor === 'dxton' && msg) { + if (vendor === 'DXTON' && 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) + } 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 @@ -38,10 +44,10 @@ module.exports = { var matches = phone.match(/\d+/g) var smsNumber, smsUrl if (matches[0] === '86') { - smsUrl = wo.envi.SMS.dxton.urlChina + smsUrl = wo.envi.SMS.DXTON.urlChina smsNumber = matches[1] } else { - smsUrl = wo.envi.SMS.dxton.urlWorld // 国际短信不需要签名、模板,可发送任意内容。 + smsUrl = wo.envi.SMS.DXTON.urlWorld // 国际短信不需要签名、模板,可发送任意内容。 smsNumber = matches[0] + matches[1] } // 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 } + } + } + } + }