TencentSms.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\common\service\sms\engine;
  20. use TencentCloud\Sms\V20190711\SmsClient;
  21. use TencentCloud\Sms\V20190711\Models\SendSmsRequest;
  22. use TencentCloud\Common\Exception\TencentCloudSDKException;
  23. use TencentCloud\Common\Credential;
  24. use TencentCloud\Common\Profile\ClientProfile;
  25. use TencentCloud\Common\Profile\HttpProfile;
  26. /**
  27. * 腾讯云短信
  28. * Class TencentSms
  29. * @package app\common\service\sms\engine
  30. */
  31. class TencentSms
  32. {
  33. protected $error = null;
  34. protected $config;
  35. protected $mobile;
  36. protected $templateId;
  37. protected $templateParams;
  38. /**
  39. * @notes 架构方法
  40. * @param $config
  41. * @author Tab
  42. * @date 2021/8/19 17:46
  43. */
  44. public function __construct($config)
  45. {
  46. if(empty($config)) {
  47. $this->error = '请联系管理员配置参数';
  48. return false;
  49. }
  50. $this->config = $config;
  51. }
  52. /**
  53. * @notes 设置手机号
  54. * @param $mobile
  55. * @return $this
  56. * @author Tab
  57. * @date 2021/8/19 16:52
  58. */
  59. public function setMobile($mobile)
  60. {
  61. $this->mobile = $mobile;
  62. return $this;
  63. }
  64. /**
  65. * @notes 设置模板id
  66. * @param $templateId
  67. * @return $this
  68. * @author Tab
  69. * @date 2021/8/19 16:54
  70. */
  71. public function setTemplateId($templateId)
  72. {
  73. $this->templateId = $templateId;
  74. return $this;
  75. }
  76. /**
  77. * @notes 设置模板参数
  78. * @param $templateParams
  79. * @return $this
  80. * @author Tab
  81. * @date 2021/8/19 16:56
  82. */
  83. public function setTemplateParams($templateParams)
  84. {
  85. $this->templateParams = $templateParams;
  86. return $this;
  87. }
  88. /**
  89. * @notes 获取错误信息
  90. * @return mixed
  91. * @author Tab
  92. * @date 2021/8/19 18:12
  93. */
  94. public function getError()
  95. {
  96. return $this->error;
  97. }
  98. /**
  99. * @notes 发送短信
  100. * @return false|mixed
  101. * @author Tab
  102. * @date 2021/8/19 17:46
  103. */
  104. public function send()
  105. {
  106. try {
  107. $cred = new Credential($this->config['secret_id'], $this->config['secret_key']);
  108. $httpProfile = new HttpProfile();
  109. $httpProfile->setEndpoint("sms.tencentcloudapi.com");
  110. $clientProfile = new ClientProfile();
  111. $clientProfile->setHttpProfile($httpProfile);
  112. $client = new SmsClient($cred, 'ap-guangzhou', $clientProfile);
  113. $params = [
  114. 'PhoneNumberSet' => ['+86' . $this->mobile],
  115. 'TemplateID' => $this->templateId,
  116. 'Sign' => $this->config['sign'],
  117. 'TemplateParamSet' => $this->templateParams,
  118. 'SmsSdkAppid' => $this->config['app_id'],
  119. ];
  120. outFileLog($params,'noticeLogic','send_message_$params');
  121. $req = new SendSmsRequest();
  122. $req->fromJsonString(json_encode($params));
  123. $resp = json_decode($client->SendSms($req)->toJsonString(), true);
  124. if (isset($resp['SendStatusSet']) && $resp['SendStatusSet'][0]['Code'] == 'Ok') {
  125. return $resp;
  126. } else {
  127. $message = $res['SendStatusSet'][0]['Message'] ?? json_encode($resp);
  128. throw new \Exception('腾讯云短信错误:' . $message);
  129. }
  130. } catch(\Exception $e) {
  131. $this->error = $e->getMessage();
  132. return false;
  133. }
  134. }
  135. }