sms.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. 'use strict';
  2. /**
  3. * 短信发送相关接口
  4. *
  5. * @author CaiAoLin
  6. * @date 2018/1/25
  7. * @version
  8. */
  9. // const xmlReader = require('xmlreader');
  10. const Core = require('@alicloud/pop-core');
  11. const smsAli = require('../const/sms_alitemplate.js');
  12. class SMS {
  13. /**
  14. * 构造函数
  15. *
  16. * @param {Object} ctx - egg全局变量
  17. * @return {void}
  18. */
  19. constructor(ctx) {
  20. this.ctx = ctx;
  21. this.url = 'https://sms.yunpian.com/v2/sms/batch_send.json';
  22. // this.url = 'http://sms.haotingyun.com/v2/sms/single_send.json';
  23. this.url2 = 'http://sms.haotingyun.com/v2/sms/tpl_single_send.json';
  24. // this.url = 'http://101.132.42.40:7862/sms';
  25. this.client = new Core({
  26. accessKeyId: smsAli.accessKey,
  27. accessKeySecret: smsAli.accessKeySecret,
  28. endpoint: smsAli.endpoint,
  29. apiVersion: '2017-05-25',
  30. });
  31. }
  32. async aliSend(mobile, data, code) {
  33. if (mobile instanceof Array) {
  34. mobile = mobile.join(',');
  35. }
  36. let flag = false;
  37. // const flag = false;
  38. const params = {
  39. RegionId: smsAli.regionId,
  40. PhoneNumbers: mobile,
  41. SignName: smsAli.signName,
  42. TemplateCode: code,
  43. TemplateParam: JSON.stringify(data),
  44. };
  45. const requestOption = {
  46. method: 'POST',
  47. };
  48. // console.log(params);
  49. try {
  50. await this.client.request('SendSms', params, requestOption).then(result => {
  51. if (result.Code === 'OK') {
  52. flag = true;
  53. } else {
  54. throw '短信发送失败!';
  55. }
  56. }, ex => {
  57. console.log(ex);
  58. throw '短信发送失败!';
  59. });
  60. } catch (e) {
  61. console.log(e);
  62. flag = false;
  63. }
  64. return flag;
  65. }
  66. /**
  67. * 发送信息
  68. *
  69. * @param {String|Array} mobile - 发送的电话号码
  70. * @param {String} content - 发送的内容
  71. * @param {String} tplId - 补充的tpl_id(防止模板无法找到发送失败)
  72. * @return {Boolean} - 发送结果
  73. */
  74. async send(mobile, content, tplId = '') {
  75. if (mobile instanceof Array) {
  76. mobile = mobile.join(',');
  77. }
  78. let result = false;
  79. const config = this.ctx.app.config.sms;
  80. const postData = {
  81. // action: 'send',
  82. // account: config.account,
  83. // password: config.password,
  84. apikey: config.authKey,
  85. mobile,
  86. text: content,
  87. // extno: config.extno,
  88. };
  89. if (tplId !== '') {
  90. postData.tpl_id = tplId;
  91. }
  92. const url = tplId !== '' ? this.url2 : this.url;
  93. try {
  94. const response = await this.ctx.helper.sendRequest(url, postData, 'POST');
  95. // const xmlData = await this.xmlParse(response);
  96. // if (xmlData === undefined || xmlData.returnstatus.text() !== 'Success') {
  97. // throw '短信发送失败!';
  98. // }
  99. if (response === undefined && response.code !== 0) {
  100. throw '短信发送失败!';
  101. }
  102. result = true;
  103. } catch (error) {
  104. console.log(error);
  105. result = false;
  106. }
  107. return result;
  108. }
  109. /**
  110. * xml解析
  111. *
  112. * @param {String} xml - xml数据
  113. * @return {Object} - 解析结果
  114. */
  115. // xmlParse(xml) {
  116. // return new Promise(function(resolve, reject) {
  117. // xmlReader.read(xml, function(errors, xmlData) {
  118. // if (errors) {
  119. // reject('');
  120. // } else {
  121. // resolve(xmlData.returnsms);
  122. // }
  123. // });
  124. // });
  125. // }
  126. /**
  127. * 关键字转换,并限制20个字以内
  128. *
  129. * @param {String} content - 内容
  130. * @return {Object} - 解析结果
  131. */
  132. async contentChange(content) {
  133. let str = content.replace(/【/g, '(');
  134. str = str.replace(/】/g, ')');
  135. str = str.replace(/工程款/g, '***');
  136. str = str.length > 20 ? str.substring(0, 17) + '...' : str;
  137. return str;
  138. }
  139. }
  140. module.exports = SMS;