Qcloudsms.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace addons\qcloudsms;
  3. use addons\qcloudsms\library\SmsSingleSender;
  4. use addons\qcloudsms\library\SmsVoicePromptSender;
  5. use addons\qcloudsms\library\SmsVoiceverifyCodeSender;
  6. use addons\qcloudsms\library\TtsVoiceSender;
  7. use think\Addons;
  8. use think\Config;
  9. /**
  10. * 插件
  11. */
  12. class Qcloudsms extends Addons
  13. {
  14. private $appid = null;
  15. private $appkey = null;
  16. private $config = null;
  17. private $sender = null;
  18. private $sendError = '';
  19. public function ConfigInit()
  20. {
  21. $this->config = $this->getConfig();
  22. //如果使用语音短信 更换成语音短信模板
  23. if ($this->config['isVoice'] == 1) {
  24. $this->config['template'] = $this->config['voiceTemplate'];
  25. //语音短信 需要另行设置Aappid 与Appkey
  26. $this->appid = $this->config['voiceAppid'];
  27. $this->appkey = $this->config['voiceAppkey'];
  28. } else {
  29. $this->appid = $this->config['appid'];
  30. $this->appkey = $this->config['appkey'];
  31. }
  32. }
  33. /**
  34. * 短信发送行为
  35. * @param Sms $params
  36. * @return boolean
  37. */
  38. public function smsSend(&$params)
  39. {
  40. $this->ConfigInit();
  41. try {
  42. if ($this->config['isTemplateSender'] == 1) {
  43. $templateID = $this->config['template'][$params->event];
  44. if ($this->config['isVoice'] != 1) {
  45. //普通短信发送
  46. $this->sender = new SmsSingleSender($this->appid, $this->appkey);
  47. $result = $this->sender->sendWithParam("86", $params['mobile'], $templateID, ["{$params->code}","5"], $this->config['sign'], "", "");
  48. } else {
  49. //语音短信发送
  50. $this->sender = new TtsVoiceSender($this->appid, $this->appkey);
  51. //参数: 国家码,手机号、模板ID、模板参数、播放次数(可选字段)、用户的session内容,服务器端原样返回(可选字段)
  52. $result = $this->sender->send("86", $params['mobile'], $templateID, [$params->code]);
  53. }
  54. } else {
  55. //判断是否是语音短信
  56. if ($this->config['isVoice'] != 1) {
  57. $this->sender = new SmsSingleSender($this->appid, $this->appkey);
  58. //参数:短信类型{1营销短信,0普通短信 }、国家码、手机号、短信内容、扩展码(可留空)、服务的原样返回的参数
  59. $result = $this->sender->send($params['type'], '86', $params['mobile'], $params['msg'], "", "");
  60. } else {
  61. $this->sender = new SmsVoiceVerifyCodeSender($this->appid, $this->appkey);
  62. //参数:国家码、手机号、短信内容、播放次数(默认2次)、服务的原样返回的参数
  63. $result = $this->sender->send('86', $params['mobile'], $params['msg']);
  64. }
  65. }
  66. $rsp = json_decode($result, true);
  67. if ($rsp['result'] == 0 && $rsp['errmsg'] == 'OK') {
  68. return true;
  69. } else {
  70. //记录错误信息
  71. $this->setError($rsp);
  72. return false;
  73. }
  74. } catch (\Exception $e) {
  75. $this->setError($e->getMessage());
  76. }
  77. return false;
  78. }
  79. /**
  80. * 短信发送通知
  81. * @param array $params
  82. * @return boolean
  83. */
  84. public function smsNotice(&$params)
  85. {
  86. $this->ConfigInit();
  87. try {
  88. if ($this->config['isTemplateSender'] == 1) {
  89. $templateID = $this->config['template'][$params['template']];
  90. if ($this->config['isVoice'] != 1) {
  91. //普通短信发送
  92. $this->sender = new SmsSingleSender($this->appid, $this->appkey);
  93. $result = $this->sender->sendWithParam("86", $params['mobile'], $templateID, ["{$params['msg']}"], $this->config['sign'], "", "");
  94. } else {
  95. //语音短信发送
  96. $this->sender = new TtsVoiceSender($this->appid, $this->appkey);
  97. //参数: 国家码,手机号、模板ID、模板参数、播放次数(可选字段)、用户的session内容,服务器端原样返回(可选字段)
  98. $result = $this->sender->send("86", $params['mobile'], $templateID, [$params['msg']]);
  99. }
  100. } else {
  101. //判断是否是语音短信
  102. if ($this->config['isVoice'] != 1) {
  103. $this->sender = new SmsSingleSender($this->appid, $this->appkey);
  104. //参数:短信类型{1营销短信,0普通短信 }、国家码、手机号、短信内容、扩展码(可留空)、服务的原样返回的参数
  105. $result = $this->sender->send($params['type'], '86', $params['mobile'], $params['msg'], "", "");
  106. } else {
  107. $this->sender = new SmsVoicePromptSender($this->appid, $this->appkey);
  108. //参数:国家码、手机号、语音类型(目前固定为2)、短信内容、播放次数(默认2次)、服务的原样返回的参数
  109. $result = $this->sender->send('86', $params['mobile'], 2, $params['msg']);
  110. }
  111. }
  112. $rsp = (array)json_decode($result, true);
  113. if ($rsp['result'] == 0 && $rsp['errmsg'] == 'OK') {
  114. return true;
  115. } else {
  116. //记录错误信息
  117. $this->setError($rsp);
  118. return false;
  119. }
  120. } catch (\Exception $e) {
  121. var_dump($e);
  122. exit();
  123. }
  124. }
  125. /**
  126. * 记录失败信息
  127. * @param [type] $err [description]
  128. */
  129. private function setError($err)
  130. {
  131. $this->sendError = $err;
  132. }
  133. /**
  134. * 获取失败信息
  135. * @return [type] [description]
  136. */
  137. public function getError()
  138. {
  139. return $this->sendError;
  140. }
  141. /**
  142. * 检测验证是否正确
  143. * @param Sms $params
  144. * @return boolean
  145. */
  146. public function smsCheck(&$params)
  147. {
  148. return true;
  149. }
  150. /**
  151. * 插件安装方法
  152. * @return bool
  153. */
  154. public function install()
  155. {
  156. return true;
  157. }
  158. /**
  159. * 插件卸载方法
  160. * @return bool
  161. */
  162. public function uninstall()
  163. {
  164. return true;
  165. }
  166. /**
  167. * 插件启用方法
  168. * @return bool
  169. */
  170. public function enable()
  171. {
  172. return true;
  173. }
  174. /**
  175. * 插件禁用方法
  176. * @return bool
  177. */
  178. public function disable()
  179. {
  180. return true;
  181. }
  182. }