TencentCloudService.php 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace app\api\service;
  3. use TencentCloud\Common\Credential;
  4. use TencentCloud\Common\Exception\TencentCloudSDKException;
  5. use TencentCloud\Common\Profile\ClientProfile;
  6. use TencentCloud\Common\Profile\HttpProfile;
  7. use TencentCloud\Sms\V20210111\Models\SendSmsRequest;
  8. use TencentCloud\Sms\V20210111\SmsClient;
  9. use TencentCloud\Vms\V20200902\Models\SendTtsVoiceRequest;
  10. use TencentCloud\Vms\V20200902\VmsClient;
  11. use think\Env;
  12. // 导入 VMS 的 client
  13. class TencentCloudService extends BaseService
  14. {
  15. public static $MASSAGE_APPLY = 1913772;
  16. // TODO: 更换短信配置
  17. public static function tencent_cloud_sms_send($templateId, $mobile, $params)
  18. {
  19. try {
  20. $cred = new Credential(
  21. Env::get("sms.tencent_cloud_secret_id"),
  22. Env::get("sms.tencent_cloud_secret_key")
  23. );
  24. $httpProfile = new HttpProfile();
  25. $httpProfile->setReqMethod("GET"); // post请求(默认为post请求)
  26. $httpProfile->setReqTimeout(30); // 请求超时时间,单位为秒(默认60秒)
  27. $httpProfile->setEndpoint("sms.tencentcloudapi.com"); // 指定接入地域域名(默认就近接入)
  28. $clientProfile = new ClientProfile();
  29. $clientProfile->setSignMethod("TC3-HMAC-SHA256");
  30. $clientProfile->setHttpProfile($httpProfile);
  31. $client = new SmsClient($cred, "ap-guangzhou", $clientProfile);
  32. $req = new SendSmsRequest();
  33. $req->SmsSdkAppId = Env::get("sms.tencent_cloud_sms_sdk_app_id");
  34. $req->SignName = Env::get("sms.tencent_cloud_sms_sign_name");
  35. $req->TemplateId = $templateId;
  36. $req->TemplateParamSet = $params;
  37. $req->PhoneNumberSet = array("+86{$mobile}");
  38. // 通过client对象调用SendSms方法发起请求。注意请求方法名与请求对象是对应的
  39. // 返回的resp是一个SendSmsResponse类的实例,与请求对象对应
  40. $resp = $client->SendSms($req);
  41. $resp_data = $resp->toJsonString();
  42. if (false === $resp_data)
  43. return new \SResult(0, "请求发送失败");
  44. $fmt_data = json_decode($resp_data, true);
  45. return new \SResult("Ok" === $fmt_data["SendStatusSet"][0]["Code"], "请求发送失败!", $fmt_data);
  46. } catch (TencentCloudSDKException $e) {
  47. return new \SResult(0, $e->getMessage());
  48. }
  49. }
  50. public static function tencent_cloud_vms_send($mobile, $template_id, $params = null)
  51. {
  52. try {
  53. $cred = new Credential(
  54. config("site.tencent_cloud_secret_id"),
  55. config("site.tencent_cloud_secret_key")
  56. );
  57. $httpProfile = new HttpProfile();
  58. $httpProfile->setReqMethod("POST");
  59. $httpProfile->setReqTimeout(30);
  60. $httpProfile->setEndpoint("vms.tencentcloudapi.com");
  61. $clientProfile = new ClientProfile();
  62. $clientProfile->setSignMethod("TC3-HMAC-SHA256");
  63. $clientProfile->setHttpProfile($httpProfile);
  64. $client = new VmsClient($cred, "ap-guangzhou", $clientProfile);
  65. $req = new SendTtsVoiceRequest();
  66. $req->TemplateId = $template_id;
  67. if (!is_null($params)) {
  68. $req->TemplateParamSet = $params;
  69. }
  70. $req->CalledNumber = "+86{$mobile}";
  71. $req->VoiceSdkAppid = config("site.tencent_cloud_vms_sdk_app_id");
  72. $req->PlayTimes = 2;
  73. $resp = $client->SendTtsVoice($req);
  74. $resp_data = $resp->toJsonString();
  75. if (false === $resp_data)
  76. return new \SResult(0, "请求发送失败");
  77. $fmt_data = json_decode($resp_data, true);
  78. return new \SResult(isset($fmt_data["RequestId"]), "请求发送失败!");
  79. } catch (TencentCloudSDKException $e) {
  80. return new \SResult(0, $e->getMessage());
  81. }
  82. }
  83. }