Tencent.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace core\sms;
  12. use core\exception\CommonException;
  13. use core\exception\NoticeException;
  14. use Exception;
  15. use TencentCloud\Common\Credential;
  16. use TencentCloud\Common\Profile\ClientProfile;
  17. use TencentCloud\Common\Profile\HttpProfile;
  18. use TencentCloud\Sms\V20190711\Models\SendSmsRequest;
  19. use TencentCloud\Sms\V20190711\SmsClient;
  20. /**
  21. * 腾讯云短信
  22. * Class Tencent
  23. * @package app\core\sms\driver
  24. */
  25. class Tencent extends BaseSms
  26. {
  27. protected $secret_id = '';
  28. protected $secret_key = '';
  29. protected $sign = '';
  30. protected $app_id = '';
  31. /**
  32. * @param array $config
  33. * @return void
  34. */
  35. protected function initialize(array $config = [])
  36. {
  37. parent::initialize($config);
  38. $this->secret_id = $config['secret_id'] ?? '';
  39. $this->secret_key = $config['secret_key'] ?? '';
  40. $this->sign = $config['sign'] ?? '';
  41. $this->app_id = $config['app_id'] ?? '';
  42. }
  43. /**
  44. * 发送短信
  45. * @return bool|mixed
  46. */
  47. public function send(string $mobile, string $template_id, array $data = [])
  48. {
  49. try {
  50. $cred = new Credential($this->secret_id, $this->secret_key);
  51. $httpProfile = new HttpProfile();
  52. $httpProfile->setEndpoint("sms.tencentcloudapi.com");
  53. $clientProfile = new ClientProfile();
  54. $clientProfile->setHttpProfile($httpProfile);
  55. $client = new SmsClient($cred, 'ap-guangzhou', $clientProfile);
  56. $params = [
  57. 'PhoneNumberSet' => ['+86' . $mobile],
  58. 'TemplateID' => $template_id,
  59. 'Sign' => $this->sign,
  60. 'TemplateParamSet' => $data,
  61. 'SmsSdkAppid' => $this->app_id,
  62. ];
  63. $req = new SendSmsRequest();
  64. $req->fromJsonString(json_encode($params, JSON_THROW_ON_ERROR));
  65. $resp = json_decode($client->SendSms($req)->toJsonString(), true, 512, JSON_THROW_ON_ERROR);
  66. if (isset($resp['SendStatusSet']) && $resp['SendStatusSet'][0]['Code'] == 'Ok') {
  67. return $resp;
  68. } else {
  69. $message = $res['SendStatusSet'][0]['Message'] ?? json_encode($resp, JSON_THROW_ON_ERROR);
  70. throw new CommonException($message);
  71. }
  72. } catch ( Exception $e ) {
  73. throw new NoticeException($e->getMessage());
  74. }
  75. }
  76. public function modify(string $sign = null, string $mobile, string $code)
  77. {
  78. }
  79. public function template(int $page = 0, int $limit = 15, int $type = 1)
  80. {
  81. }
  82. public function apply(string $title, string $content, int $type)
  83. {
  84. }
  85. public function localTemplate(int $type, int $page, int $limit)
  86. {
  87. }
  88. public function record($id)
  89. {
  90. }
  91. }