截流自动化的商城平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TcSms.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop开源商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | gitee下载:https://gitee.com/likeshop_gitee
  7. // | github下载:https://github.com/likeshop-github
  8. // | 访问官网:https://www.likeshop.cn
  9. // | 访问社区:https://home.likeshop.cn
  10. // | 访问手册:http://doc.likeshop.cn
  11. // | 微信公众号:likeshop技术社区
  12. // | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
  13. // | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
  14. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  15. // | likeshop团队版权所有并拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshop.cn.team
  18. // +----------------------------------------------------------------------
  19. namespace app\common\server\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 TcSms
  29. */
  30. class TcSms extends Server
  31. {
  32. protected $config;
  33. protected $mobile; //下发手机号码
  34. protected $template_id; //模板id
  35. protected $template_param ; //模板参数
  36. public function __construct($config)
  37. {
  38. if (empty($config)) {
  39. $this->error = '请联系管理员配置参数';
  40. return false;
  41. }
  42. $this->config = $config;
  43. }
  44. /**
  45. * Notes: 设置手机号
  46. * @param string $mobile
  47. * @author 段誉(2021/5/18 17:41)
  48. * @return $this
  49. */
  50. public function setMobile($mobile = '')
  51. {
  52. $this->mobile = $mobile;
  53. return $this;
  54. }
  55. /**
  56. * Notes: 设置模板id
  57. * @param null $template_id
  58. * @author 段誉(2021/5/18 17:41)
  59. * @return $this
  60. */
  61. public function setTemplateId($template_id = null)
  62. {
  63. $this->template_id = $template_id;
  64. return $this;
  65. }
  66. /**
  67. * Notes: 设置模板参数
  68. * @param null $template_param
  69. * @author 段誉(2021/5/18 17:42)
  70. * @return $this
  71. */
  72. public function setTemplateParam($template_param = null)
  73. {
  74. $this->template_param = $template_param;
  75. return $this;
  76. }
  77. /**
  78. * @notes 发送 (此处$config[app_key]为系统统一命名,原腾讯云命名为secret_id)
  79. * @return false|mixed
  80. * @author 段誉
  81. * @date 2021/8/4 10:40
  82. */
  83. public function send()
  84. {
  85. try {
  86. $cred = new Credential($this->config['app_key'], $this->config['secret_key']);
  87. $httpProfile = new HttpProfile();
  88. $httpProfile->setEndpoint("sms.tencentcloudapi.com");
  89. $clientProfile = new ClientProfile();
  90. $clientProfile->setHttpProfile($httpProfile);
  91. $client = new SmsClient($cred, "", $clientProfile);
  92. $req = new SendSmsRequest();
  93. $params = [
  94. 'PhoneNumberSet' => ['+86'.$this->mobile],
  95. 'TemplateID' => $this->template_id,
  96. 'Sign' => $this->config['sign'],
  97. 'TemplateParamSet' => $this->template_param,
  98. 'SmsSdkAppid' => $this->config['app_id'],
  99. ];
  100. $req->fromJsonString(json_encode($params));
  101. $resp = json_decode($client->SendSms($req)->toJsonString(), true);
  102. if (isset($resp['SendStatusSet']) && $resp['SendStatusSet'][0]['Code'] == 'Ok') {
  103. return $resp;
  104. } else {
  105. $message = $res['SendStatusSet'][0]['Message'] ?? json_encode($resp);
  106. throw new \Exception('短信错误:' . $message);
  107. }
  108. } catch (TencentCloudSDKException $e) {
  109. $this->error = $e->getMessage();
  110. return false;
  111. } catch (\Exception $e) {
  112. $this->error = $e->getMessage();
  113. return false;
  114. }
  115. }
  116. }