Tencent.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services\sms\storage;
  12. use crmeb\services\sms\BaseSms;
  13. use crmeb\services\HttpService;
  14. /**
  15. * 腾讯云短信
  16. * Class Tencent
  17. * @package crmeb\services\sms\storage
  18. */
  19. class Tencent extends BaseSms
  20. {
  21. //接口请求地址
  22. const API_URL = 'https://sms.tencentcloudapi.com';
  23. /**
  24. * 发送模板id
  25. * @var array
  26. */
  27. protected $templates = [];
  28. /**
  29. * @var string[]
  30. */
  31. protected $header = ['Content-Type:application/json'];
  32. /**
  33. * @var string
  34. */
  35. protected $secretId = '';
  36. /**
  37. * @var string
  38. */
  39. protected $secretKey = '';
  40. /**
  41. * 短信SDKid
  42. * @var string
  43. */
  44. protected $smsSdkAppId = '';
  45. /**
  46. * 短信签名
  47. * @var string
  48. */
  49. protected $signName = '';
  50. /**
  51. * @var string
  52. */
  53. protected $region = "ap-guangzhou";
  54. /**
  55. * 版本号
  56. * @var string
  57. */
  58. protected $version = "2021-01-11";
  59. /**
  60. * 加密方式
  61. * @var string
  62. */
  63. protected $algorithm = 'TC3-HMAC-SHA256';
  64. /**
  65. * 产品名称
  66. * @var string
  67. */
  68. protected $service = 'sms';
  69. /**
  70. * @param array $config
  71. * @return mixed|void
  72. */
  73. protected function initialize(array $config = [])
  74. {
  75. parent::initialize($config);
  76. $this->smsSdkAppId = $config['tencent_sms_app_id'] ?? '';
  77. $this->secretId = $config['tencent_sms_secret_id'] ?? '';
  78. $this->secretKey = $config['tencent_sms_secret_key'] ?? '';
  79. $this->signName = $config['tencent_sms_sign_name'] ?? '';
  80. $this->region = $config['tencent_sms_region'] ?? '';
  81. }
  82. /**
  83. * @param string $phone
  84. * @param string $templateId
  85. * @param array $data
  86. * @return mixed|void
  87. */
  88. public function send(string $phone, string $templateId, array $data)
  89. {
  90. $body = json_encode([
  91. 'PhoneNumberSet' => [$phone],
  92. 'SmsSdkAppId' => $this->smsSdkAppId,
  93. 'SignName' => $this->signName,
  94. 'TemplateId' => $templateId,
  95. 'TemplateParamSet' => $data,
  96. ]);
  97. $res = HttpService::request(self::API_URL, 'post', $body, $this->getHeader($body));
  98. $res = json_decode($res, true);
  99. if (!empty($res['Response']['Error'])) {
  100. return $this->setError($res['Response']['Message']);
  101. }
  102. if ($res['Response']['SendStatusSet'][0]['Code'] != 'Ok') {
  103. return $this->setError($res['Response']['SendStatusSet'][0]['Message']);
  104. }
  105. return $res;
  106. }
  107. /**
  108. * 获取请求header
  109. * @param string $boby
  110. * @return string[]
  111. */
  112. protected function getHeader(string $boby)
  113. {
  114. $host = str_replace(['https://', 'http://'], '', self::API_URL);
  115. $header = [
  116. 'Content-Type:application/json; charset=utf-8',
  117. 'Host:' . $host,
  118. 'X-TC-Action:SendSms'
  119. ];
  120. $httpRequestMethod = 'POST';
  121. $canonicalUri = '/';
  122. $canonicalQueryString = '';
  123. $canonicalHeaders = "content-type:application/json; charset=utf-8\n" . "host:" . $host . "\n";
  124. $signedHeaders = 'content-type;host';
  125. $hashedRequestPayload = hash("SHA256", $boby);
  126. $canonicalRequest = $httpRequestMethod . "\n"
  127. . $canonicalUri . "\n"
  128. . $canonicalQueryString . "\n"
  129. . $canonicalHeaders . "\n"
  130. . $signedHeaders . "\n"
  131. . $hashedRequestPayload;
  132. $service = $this->service;
  133. $timestamp = time();
  134. $date = gmdate("Y-m-d", $timestamp);
  135. $credentialScope = $date . "/" . $service . "/tc3_request";
  136. $hashedCanonicalRequest = hash("SHA256", $canonicalRequest);
  137. $stringToSign = $this->algorithm . "\n"
  138. . $timestamp . "\n"
  139. . $credentialScope . "\n"
  140. . $hashedCanonicalRequest;
  141. $secretDate = hash_hmac("SHA256", $date, "TC3" . $this->secretKey, true);
  142. $secretService = hash_hmac("SHA256", $service, $secretDate, true);
  143. $secretSigning = hash_hmac("SHA256", "tc3_request", $secretService, true);
  144. $signature = hash_hmac("SHA256", $stringToSign, $secretSigning);
  145. $authorization = $this->algorithm
  146. . " Credential=" . $this->secretId . "/" . $credentialScope
  147. . ", SignedHeaders=content-type;host, Signature=" . $signature;
  148. array_push($header, 'Authorization:' . $authorization, 'X-TC-Timestamp:' . $timestamp, 'X-TC-Version:' . $this->version,
  149. 'X-TC-Region:' . $this->region);
  150. return $header;
  151. }
  152. public function open()
  153. {
  154. // TODO: Implement open() method.
  155. }
  156. public function modify(string $sign = null, string $phone = '', string $code = '')
  157. {
  158. // TODO: Implement modify() method.
  159. }
  160. public function info()
  161. {
  162. // TODO: Implement info() method.
  163. }
  164. public function temps(int $page, int $limit, int $type)
  165. {
  166. // TODO: Implement temps() method.
  167. }
  168. public function apply(string $title, string $content, int $type)
  169. {
  170. // TODO: Implement apply() method.
  171. }
  172. public function applys(int $tempType, int $page, int $limit)
  173. {
  174. // TODO: Implement applys() method.
  175. }
  176. public function record($record_id)
  177. {
  178. // TODO: Implement record() method.
  179. }
  180. }