海南旅游SAAS
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.

114 lines
5.0 KiB

4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Service;
  3. use \Illuminate\Support\Facades\Log;
  4. use TencentCloud\Sms\V20210111\SmsClient;
  5. use TencentCloud\Sms\V20210111\Models\SendSmsRequest;
  6. use TencentCloud\Common\Exception\TencentCloudSDKException;
  7. use TencentCloud\Common\Credential;
  8. use TencentCloud\Common\Profile\ClientProfile;
  9. use TencentCloud\Common\Profile\HttpProfile;
  10. class SmsService
  11. {
  12. private $client;
  13. private $cfg;
  14. function __construct()
  15. {
  16. /* 必要步骤:
  17. * 实例化一个认证对象,入参需要传入腾讯云账户密钥对 secretId,secretKey。
  18. * 这里采用的是从环境变量读取的方式,需要在环境变量中先设置这两个值。
  19. * 你也可以直接在代码中写死密钥对,但是小心不要将代码复制、上传或者分享给他人,
  20. * 以免泄露密钥对危及你的财产安全。
  21. * CAM密匙查询: https://console.cloud.tencent.com/cam/capi
  22. */
  23. $this->cfg = config('sms.tencent');
  24. $cred = new Credential($this->cfg['secret_id'], $this->cfg['secret_key']);
  25. // $cred = new Credential(getenv("TENCENTCLOUD_SECRET_ID"), getenv("TENCENTCLOUD_SECRET_KEY"));
  26. // 实例化一个http选项,可选的,没有特殊需求可以跳过
  27. $httpProfile = new HttpProfile();
  28. // 配置代理
  29. // $httpProfile->setProxy("https://ip:port");
  30. $httpProfile->setReqMethod("GET"); // post请求(默认为post请求)
  31. $httpProfile->setReqTimeout(30); // 请求超时时间,单位为秒(默认60秒)
  32. $httpProfile->setEndpoint("sms.tencentcloudapi.com"); // 指定接入地域域名(默认就近接入)
  33. // 实例化一个client选项,可选的,没有特殊需求可以跳过
  34. $clientProfile = new ClientProfile();
  35. $clientProfile->setSignMethod("TC3-HMAC-SHA256"); // 指定签名算法(默认为HmacSHA256)
  36. $clientProfile->setHttpProfile($httpProfile);
  37. // 实例化要请求产品(以sms为例)的client对象,clientProfile是可选的
  38. // 第二个参数是地域信息,可以直接填写字符串 ap-guangzhou,或者引用预设的常量
  39. $this->client = new SmsClient($cred, 'ap-guangzhou', $clientProfile);
  40. }
  41. public function send($templateId, $templateParamSet, $phoneNumberSet, $signName = '')
  42. {
  43. //if (!config('sms.debug') || config('sms.debug') == 'yes') {
  44. // Log::debug([$templateId, $templateParamSet, $phoneNumberSet, $signName]);
  45. // return true;
  46. //}
  47. foreach ($phoneNumberSet as $key => $phone) {
  48. if (!$phoneNumberSet[$key]) {
  49. unset($phoneNumberSet[$key]);
  50. } else {
  51. $phoneNumberSet[$key] = '+86' . $phoneNumberSet[$key];
  52. }
  53. }
  54. if (count($phoneNumberSet) == 0) {
  55. return true;
  56. }
  57. if (!$signName) {
  58. $signName = $this->cfg['sign_name'];
  59. }
  60. try {
  61. // 实例化一个 sms 发送短信请求对象,每个接口都会对应一个request对象。
  62. $req = new SendSmsRequest();
  63. /* 填充请求参数,这里request对象的成员变量即对应接口的入参
  64. * 你可以通过官网接口文档或跳转到request对象的定义处查看请求参数的定义
  65. * 基本类型的设置:
  66. * 帮助链接:
  67. * 短信控制台: https://console.cloud.tencent.com/smsv2
  68. * sms helper: https://cloud.tencent.com/document/product/382/3773
  69. */
  70. /*
  71. * 短信应用ID: 短信SdkAppId在 [短信控制台] 添加应用后生成的实际SdkAppId,示例如1400006666
  72. */
  73. $req->SmsSdkAppId = $this->cfg['sms_sdk_app_id'];
  74. /* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名,签名信息可登录 [短信控制台] 查看 */
  75. $req->SignName = $signName;
  76. /* 短信码号扩展号: 默认未开通,如需开通请联系 [sms helper] */
  77. $req->ExtendCode = "";
  78. /*
  79. 下发手机号码,采用 E.164 标准,+[国家或地区码][手机号]
  80. * 示例如:+8613711112222 其中前面有一个+ ,86为国家码,13711112222为手机号,最多不要超过200个手机号
  81. */
  82. $req->PhoneNumberSet = $phoneNumberSet;
  83. /* 国际/港澳台短信 SenderId: 国内短信填空,默认未开通,如需开通请联系 [sms helper] */
  84. $req->SenderId = "";
  85. /* 用户的 session 内容: 可以携带用户侧 ID 等上下文信息,server 会原样返回 */
  86. $req->SessionContext = "xxx";
  87. /* 模板 ID: 必须填写已审核通过的模板 ID。模板ID可登录 [短信控制台] 查看 */
  88. $req->TemplateId = $this->cfg['templates'][$templateId];
  89. /* 模板参数: 若无模板参数,则设置为空*/
  90. $req->TemplateParamSet = $templateParamSet;
  91. // 通过client对象调用SendSms方法发起请求。注意请求方法名与请求对象是对应的
  92. // 返回的resp是一个SendSmsResponse类的实例,与请求对象对应
  93. $resp = $this->client->SendSms($req);
  94. // 输出json格式的字符串回包
  95. $res = json_decode($resp->toJsonString());
  96. foreach ($res->SendStatusSet as $item) {
  97. if ($item->Code != 'Ok') {
  98. $tpl = $this->cfg['templates'][$templateId];
  99. $param = json_encode($templateParamSet);
  100. Log::error("SMS ERROR $item->Code[$tpl:$item->PhoneNumber:$param]$item->Message");
  101. }
  102. }
  103. return True;
  104. } catch (TencentCloudSDKException $e) {
  105. Log::error($e);
  106. }
  107. return 0;
  108. }
  109. }