TencentSms.php 4.0 KB

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