Qcloudsms.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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_free($mobile,$params,$templateID = '')
  39. {
  40. $this->ConfigInit();
  41. try {
  42. //普通短信发送
  43. $this->sender = new SmsSingleSender($this->appid, $this->appkey);
  44. $result = $this->sender->sendWithParam("86", $mobile, $templateID, $params, $this->config['sign'], "", "");
  45. $rsp = json_decode($result, true);
  46. if ($rsp['result'] == 0 && $rsp['errmsg'] == 'OK') {
  47. return true;
  48. } else {
  49. //记录错误信息
  50. $this->setError($rsp);
  51. return false;
  52. }
  53. } catch (\Exception $e) {
  54. $this->setError($e->getMessage());
  55. }
  56. return false;
  57. }
  58. /**
  59. * 短信发送行为
  60. * @param Sms $params
  61. * @return boolean
  62. */
  63. public function smsSend(&$params)
  64. {
  65. $this->ConfigInit();
  66. try {
  67. if ($this->config['isTemplateSender'] == 1) {
  68. $templateID = $this->config['template'][$params->event];
  69. if ($this->config['isVoice'] != 1) {
  70. //普通短信发送
  71. $this->sender = new SmsSingleSender($this->appid, $this->appkey);
  72. $result = $this->sender->sendWithParam("86", $params['mobile'], $templateID, ["{$params->code}"], $this->config['sign'], "", "");
  73. } else {
  74. //语音短信发送
  75. $this->sender = new TtsVoiceSender($this->appid, $this->appkey);
  76. //参数: 国家码,手机号、模板ID、模板参数、播放次数(可选字段)、用户的session内容,服务器端原样返回(可选字段)
  77. $result = $this->sender->send("86", $params['mobile'], $templateID, [$params->code]);
  78. }
  79. } else {
  80. //判断是否是语音短信
  81. if ($this->config['isVoice'] != 1) {
  82. $this->sender = new SmsSingleSender($this->appid, $this->appkey);
  83. //参数:短信类型{1营销短信,0普通短信 }、国家码、手机号、短信内容、扩展码(可留空)、服务的原样返回的参数
  84. $result = $this->sender->send($params['type'], '86', $params['mobile'], $params['msg'], "", "");
  85. } else {
  86. $this->sender = new SmsVoiceVerifyCodeSender($this->appid, $this->appkey);
  87. //参数:国家码、手机号、短信内容、播放次数(默认2次)、服务的原样返回的参数
  88. $result = $this->sender->send('86', $params['mobile'], $params['msg']);
  89. }
  90. }
  91. $rsp = json_decode($result, true);
  92. if ($rsp['result'] == 0 && $rsp['errmsg'] == 'OK') {
  93. return true;
  94. } else {
  95. //记录错误信息
  96. $this->setError($rsp);
  97. return false;
  98. }
  99. } catch (\Exception $e) {
  100. $this->setError($e->getMessage());
  101. }
  102. return false;
  103. }
  104. /**
  105. * 短信发送通知
  106. * @param array $params
  107. * @return boolean
  108. */
  109. public function smsNotice(&$params)
  110. {
  111. $this->ConfigInit();
  112. try {
  113. if ($this->config['isTemplateSender'] == 1) {
  114. $templateID = $this->config['template'][$params['template']];
  115. if ($this->config['isVoice'] != 1) {
  116. //普通短信发送
  117. $this->sender = new SmsSingleSender($this->appid, $this->appkey);
  118. $result = $this->sender->sendWithParam("86", $params['mobile'], $templateID, ["{$params['msg']}"], $this->config['sign'], "", "");
  119. } else {
  120. //语音短信发送
  121. $this->sender = new TtsVoiceSender($this->appid, $this->appkey);
  122. //参数: 国家码,手机号、模板ID、模板参数、播放次数(可选字段)、用户的session内容,服务器端原样返回(可选字段)
  123. $result = $this->sender->send("86", $params['mobile'], $templateID, [$params['msg']]);
  124. }
  125. } else {
  126. //判断是否是语音短信
  127. if ($this->config['isVoice'] != 1) {
  128. $this->sender = new SmsSingleSender($this->appid, $this->appkey);
  129. //参数:短信类型{1营销短信,0普通短信 }、国家码、手机号、短信内容、扩展码(可留空)、服务的原样返回的参数
  130. $result = $this->sender->send($params['type'], '86', $params['mobile'], $params['msg'], "", "");
  131. } else {
  132. $this->sender = new SmsVoicePromptSender($this->appid, $this->appkey);
  133. //参数:国家码、手机号、语音类型(目前固定为2)、短信内容、播放次数(默认2次)、服务的原样返回的参数
  134. $result = $this->sender->send('86', $params['mobile'], 2, $params['msg']);
  135. }
  136. }
  137. $rsp = (array)json_decode($result, true);
  138. if ($rsp['result'] == 0 && $rsp['errmsg'] == 'OK') {
  139. return true;
  140. } else {
  141. //记录错误信息
  142. $this->setError($rsp);
  143. return false;
  144. }
  145. } catch (\Exception $e) {
  146. var_dump($e);
  147. exit();
  148. }
  149. }
  150. /**
  151. * 记录失败信息
  152. * @param [type] $err [description]
  153. */
  154. private function setError($err)
  155. {
  156. $this->sendError = $err;
  157. }
  158. /**
  159. * 获取失败信息
  160. * @return [type] [description]
  161. */
  162. public function getError()
  163. {
  164. return $this->sendError;
  165. }
  166. /**
  167. * 检测验证是否正确
  168. * @param Sms $params
  169. * @return boolean
  170. */
  171. public function smsCheck(&$params)
  172. {
  173. return true;
  174. }
  175. /**
  176. * 插件安装方法
  177. * @return bool
  178. */
  179. public function install()
  180. {
  181. return true;
  182. }
  183. /**
  184. * 插件卸载方法
  185. * @return bool
  186. */
  187. public function uninstall()
  188. {
  189. return true;
  190. }
  191. /**
  192. * 插件启用方法
  193. * @return bool
  194. */
  195. public function enable()
  196. {
  197. return true;
  198. }
  199. /**
  200. * 插件禁用方法
  201. * @return bool
  202. */
  203. public function disable()
  204. {
  205. return true;
  206. }
  207. }