| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- namespace app\api\service;
- use TencentCloud\Common\Credential;
- use TencentCloud\Common\Exception\TencentCloudSDKException;
- use TencentCloud\Common\Profile\ClientProfile;
- use TencentCloud\Common\Profile\HttpProfile;
- use TencentCloud\Sms\V20210111\Models\SendSmsRequest;
- use TencentCloud\Sms\V20210111\SmsClient;
- use TencentCloud\Vms\V20200902\Models\SendTtsVoiceRequest;
- use TencentCloud\Vms\V20200902\VmsClient;
- use think\Env;
- // 导入 VMS 的 client
- class TencentCloudService extends BaseService
- {
- public static $MASSAGE_APPLY = 1913772;
- // TODO: 更换短信配置
- public static function tencent_cloud_sms_send($templateId, $mobile, $params)
- {
- try {
- $cred = new Credential(
- Env::get("sms.tencent_cloud_secret_id"),
- Env::get("sms.tencent_cloud_secret_key")
- );
- $httpProfile = new HttpProfile();
- $httpProfile->setReqMethod("GET"); // post请求(默认为post请求)
- $httpProfile->setReqTimeout(30); // 请求超时时间,单位为秒(默认60秒)
- $httpProfile->setEndpoint("sms.tencentcloudapi.com"); // 指定接入地域域名(默认就近接入)
- $clientProfile = new ClientProfile();
- $clientProfile->setSignMethod("TC3-HMAC-SHA256");
- $clientProfile->setHttpProfile($httpProfile);
- $client = new SmsClient($cred, "ap-guangzhou", $clientProfile);
- $req = new SendSmsRequest();
- $req->SmsSdkAppId = Env::get("sms.tencent_cloud_sms_sdk_app_id");
- $req->SignName = Env::get("sms.tencent_cloud_sms_sign_name");
- $req->TemplateId = $templateId;
- $req->TemplateParamSet = $params;
- $req->PhoneNumberSet = array("+86{$mobile}");
- // 通过client对象调用SendSms方法发起请求。注意请求方法名与请求对象是对应的
- // 返回的resp是一个SendSmsResponse类的实例,与请求对象对应
- $resp = $client->SendSms($req);
- $resp_data = $resp->toJsonString();
- if (false === $resp_data)
- return new \SResult(0, "请求发送失败");
- $fmt_data = json_decode($resp_data, true);
- return new \SResult("Ok" === $fmt_data["SendStatusSet"][0]["Code"], "请求发送失败!", $fmt_data);
- } catch (TencentCloudSDKException $e) {
- return new \SResult(0, $e->getMessage());
- }
- }
- public static function tencent_cloud_vms_send($mobile, $template_id, $params = null)
- {
- try {
- $cred = new Credential(
- config("site.tencent_cloud_secret_id"),
- config("site.tencent_cloud_secret_key")
- );
- $httpProfile = new HttpProfile();
- $httpProfile->setReqMethod("POST");
- $httpProfile->setReqTimeout(30);
- $httpProfile->setEndpoint("vms.tencentcloudapi.com");
- $clientProfile = new ClientProfile();
- $clientProfile->setSignMethod("TC3-HMAC-SHA256");
- $clientProfile->setHttpProfile($httpProfile);
- $client = new VmsClient($cred, "ap-guangzhou", $clientProfile);
- $req = new SendTtsVoiceRequest();
- $req->TemplateId = $template_id;
- if (!is_null($params)) {
- $req->TemplateParamSet = $params;
- }
- $req->CalledNumber = "+86{$mobile}";
- $req->VoiceSdkAppid = config("site.tencent_cloud_vms_sdk_app_id");
- $req->PlayTimes = 2;
- $resp = $client->SendTtsVoice($req);
- $resp_data = $resp->toJsonString();
- if (false === $resp_data)
- return new \SResult(0, "请求发送失败");
- $fmt_data = json_decode($resp_data, true);
- return new \SResult(isset($fmt_data["RequestId"]), "请求发送失败!");
- } catch (TencentCloudSDKException $e) {
- return new \SResult(0, $e->getMessage());
- }
- }
- }
|