在运行时检测 my.envar || wo.envar,防止被提前引用导致 my.envar 为空
This commit is contained in:
		
							parent
							
								
									def3aa1b3a
								
							
						
					
					
						commit
						af116ef4d5
					
				
							
								
								
									
										38
									
								
								messenger.js
									
									
									
									
									
								
							
							
						
						
									
										38
									
								
								messenger.js
									
									
									
									
									
								
							@ -3,24 +3,24 @@ const http = require('http')
 | 
				
			|||||||
const NodeMailer = require('nodemailer')
 | 
					const NodeMailer = require('nodemailer')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 注意,unicloud 不支持,本文件里只好不用 ?. 操作符
 | 
					// 注意,unicloud 不支持,本文件里只好不用 ?. 操作符
 | 
				
			||||||
const my = { envar: typeof wo !== 'undefined' ? wo.envar : globalThis.envar || {} }
 | 
					const my = {}
 | 
				
			||||||
const sender = {}
 | 
					const sender = {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports = {
 | 
					module.exports = {
 | 
				
			||||||
  init (envar) {
 | 
					  initMy (envar) {
 | 
				
			||||||
    my.envar = envar
 | 
					    my.envar = envar
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // 或者如果smtp参数已经确定,就可以直接定义 sendMail: Bluebird.promisify(Smtp.sendMail).bind(Smtp)
 | 
					  // 或者如果smtp参数已经确定,就可以直接定义 sendMail: Bluebird.promisify(Smtp.sendMail).bind(Smtp)
 | 
				
			||||||
  async sendMail (messageObject) {
 | 
					  async sendMail (messageObject, smtp = my.envar?.SMTP || wo?.envar?.SMTP) {
 | 
				
			||||||
    // messageObject: { from, to, cc, bcc, subject, text, html, sender, replyTo, inReplyTo }
 | 
					    // messageObject: { from, to, cc, bcc, subject, text, html, sender, replyTo, inReplyTo }
 | 
				
			||||||
    sender.smtpTransporter = sender.smtpTransporter || NodeMailer.createTransport(my.envar.SMTP)
 | 
					    sender.smtpTransporter = sender.smtpTransporter || NodeMailer.createTransport(smtp)
 | 
				
			||||||
    return await util.promisify(sender.smtpTransporter.sendMail).call(sender.smtpTransporter, messageObject)
 | 
					    return await util.promisify(sender.smtpTransporter.sendMail).call(sender.smtpTransporter, messageObject)
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async sendSms ({
 | 
					  async sendSms ({
 | 
				
			||||||
    phone,
 | 
					    phone,
 | 
				
			||||||
    vendor = my.envar.SMS.vendor,
 | 
					    vendor = my.envar?.SMS?.vendor || wo.envar?.SMS?.vendor,
 | 
				
			||||||
    msg, // for vendor==='DXTON'
 | 
					    msg, // for vendor==='DXTON'
 | 
				
			||||||
    msgParam,
 | 
					    msgParam,
 | 
				
			||||||
    msgTemplate, // for ['ALIYUN','UNICLOUD'].includes(vendor)
 | 
					    msgTemplate, // for ['ALIYUN','UNICLOUD'].includes(vendor)
 | 
				
			||||||
@ -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) {
 | 
					  async sendSmsDxton (phone, msg, dxton = my.envar?.SMS?.DXTON || wo?.envar?.SMS?.DXTON) {
 | 
				
			||||||
    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 = my.envar.SMS.DXTON.urlChina
 | 
					      smsUrl = dxton.urlChina
 | 
				
			||||||
      smsNumber = matches[1]
 | 
					      smsNumber = matches[1]
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      smsUrl = my.envar.SMS.DXTON.urlWorld // 国际短信不需要签名、模板,可发送任意内容。
 | 
					      smsUrl = 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))
 | 
				
			||||||
@ -83,8 +83,8 @@ module.exports = {
 | 
				
			|||||||
    })
 | 
					    })
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async sendSmsAliyun (phone, msgParam, msgTemplate, signName = my.envar.SMS.ALIYUN.signName) {
 | 
					  async sendSmsAliyun (phone, msgParam, msgTemplate, aliyun = my.envar?.SMS?.ALIYUN || wo?.envar?.SMS?.ALIYUN) {
 | 
				
			||||||
    sender.smsClientAliyun = sender.smsClientAliyun || new (require('@alicloud/sms-sdk'))(my.envar.SMS.ALIYUN) // https://www.npmjs.com/package/@alicloud/sms-sdk
 | 
					    sender.smsClientAliyun = sender.smsClientAliyun || new (require('@alicloud/sms-sdk'))(aliyun) // 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,7 +92,7 @@ module.exports = {
 | 
				
			|||||||
    return await sender.smsClientAliyun
 | 
					    return await sender.smsClientAliyun
 | 
				
			||||||
      .sendSMS({
 | 
					      .sendSMS({
 | 
				
			||||||
        PhoneNumbers: smsNumber, //必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式;发送国际/港澳台消息时,接收号码格式为00+国际区号+号码,如“0085200000000”
 | 
					        PhoneNumbers: smsNumber, //必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式;发送国际/港澳台消息时,接收号码格式为00+国际区号+号码,如“0085200000000”
 | 
				
			||||||
        SignName: signName, //必填:短信签名-可在短信控制台中找到
 | 
					        SignName: aliyun.signName, //必填:短信签名-可在短信控制台中找到
 | 
				
			||||||
        TemplateCode: msgTemplate, //必填:短信模板-可在短信控制台中找到,发送国际/港澳台消息时,请使用国际/港澳台短信模版
 | 
					        TemplateCode: msgTemplate, //必填:短信模板-可在短信控制台中找到,发送国际/港澳台消息时,请使用国际/港澳台短信模版
 | 
				
			||||||
        TemplateParam: JSON.stringify(msgParam), //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时。
 | 
					        TemplateParam: JSON.stringify(msgParam), //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时。
 | 
				
			||||||
      })
 | 
					      })
 | 
				
			||||||
@ -111,12 +111,12 @@ module.exports = {
 | 
				
			|||||||
      )
 | 
					      )
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async sendSmsUnicloud ({ phone, msgTemplate, msgParam, appid = my.envar.SMS.UNICLOUD.appid } = {}) {
 | 
					  async sendSmsUnicloud ({ phone, msgTemplate, msgParam, unicloud = my.envar?.SMS?.UNICLOUD || wo?.envar?.SMS?.UNICLOUD } = {}) {
 | 
				
			||||||
    try {
 | 
					    try {
 | 
				
			||||||
      const result = await uniCloud.sendSms({
 | 
					      const result = await uniCloud.sendSms({
 | 
				
			||||||
        appid: appid,
 | 
					        appid: unicloud.appid,
 | 
				
			||||||
        smsKey: my.envar.SMS.UNICLOUD.smsKey,
 | 
					        smsKey: unicloud.smsKey,
 | 
				
			||||||
        smsSecret: my.envar.SMS.UNICLOUD.smsSecret,
 | 
					        smsSecret: unicloud.smsSecret,
 | 
				
			||||||
        phone: phone.match(/\d+/g)[1],
 | 
					        phone: phone.match(/\d+/g)[1],
 | 
				
			||||||
        templateId: msgTemplate || 'uni_sms_test',
 | 
					        templateId: msgTemplate || 'uni_sms_test',
 | 
				
			||||||
        data: msgParam, // 模版中的变量的值,例如 { passcode: '234345', purpose: '注册' }
 | 
					        data: msgParam, // 模版中的变量的值,例如 { passcode: '234345', purpose: '注册' }
 | 
				
			||||||
@ -132,15 +132,15 @@ module.exports = {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async sendSmsTencent ({ phone, msgTemplate, msgParam, signName = my.envar.SMS.TENCENT.signName, appid = my.envar.SMS.TENCENT.appid } = {}) {
 | 
					  async sendSmsTencent ({ phone, msgTemplate, msgParam, tentent = my.envar?.SMS?.TENCENT || wo?.envar?.SMS?.TENCENT } = {}) {
 | 
				
			||||||
    sender.smsClientTencent = sender.smsClientTencent || new (require('tencentcloud-sdk-nodejs').sms.v20210111.Client)(my.envar.SMS.TENCENT) // https://cloud.tencent.com/document/product/382/43197
 | 
					    sender.smsClientTencent = sender.smsClientTencent || new (require('tencentcloud-sdk-nodejs').sms.v20210111.Client)(tencent) // 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: appid,
 | 
					        SmsSdkAppId: tencent.appid,
 | 
				
			||||||
        SignName: signName,
 | 
					        SignName: tencent.signName,
 | 
				
			||||||
        TemplateId: msgTemplate,
 | 
					        TemplateId: msgTemplate,
 | 
				
			||||||
        TemplateParamSet: Object.values(msgParam),
 | 
					        TemplateParamSet: Object.values(msgParam),
 | 
				
			||||||
      })
 | 
					      })
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user