| <?php |
| |
| use Illuminate\Notifications\Notification; |
| use TencentCloud\Common\Credential; |
| use TencentCloud\Common\Profile\ClientProfile; |
| use TencentCloud\Common\Profile\HttpProfile; |
| use TencentCloud\Common\Exception\TencentCloudSDKException; |
| use TencentCloud\Sms\V20210111\SmsClient; |
| use TencentCloud\Sms\V20210111\Models\SendSmsRequest; |
| |
| class QcloudHandler |
| { |
| protected $client; |
| protected $smsRequest; |
| |
| public function __construct( |
| protected string $secretId = '', |
| protected string $secretKey = '', |
| protected string $signName = '', |
| protected string $smsSdkAppid = '', |
| protected string $regionId = 'ap-guangzhou', |
| ) { |
| |
| } |
| |
| protected function createdQcloud() |
| { |
| $credntial = new Credential($this->secretId, $this->secretKey); |
| |
| $httpProfile = new HttpProfile(); |
| $httpProfile->setEndpoint('sms.tencentcloudapi.com'); |
| |
| $clientProfile = new ClientProfile(); |
| $clientProfile->setHttpProfile($httpProfile); |
| |
| $this->client = new SmsClient($credntial, $this->regionId, $clientProfile); |
| $this->smsRequest = new SendSmsRequest(); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function send($notifiable, Notification $notification) |
| { |
| if (! $to = $notifiable->routeNotificationFor('qcloud', $notification)) { |
| return null; |
| } |
| |
| |
| $message = $notification->toQcloudSms($notifiable); |
| |
| try { |
| $this->createdQcloud(); |
| |
| $phoneNumber = str_starts_with($to, '+86') |
| ? $to |
| : '+86'.$to; |
| |
| $params = [ |
| 'PhoneNumberSet' => [$phoneNumber], |
| 'SmsSdkAppId' => $this->smsSdkAppid, |
| 'SignName' => $this->signName, |
| 'TemplateId' => $message->templateId, |
| 'TemplateParamSet' => $message->templateParamSet |
| ]; |
| |
| $this->smsRequest->fromJsonString(json_encode($params)); |
| |
| return $this->client->SendSms($this->smsRequest); |
| } |
| catch(TencentCloudSDKException $e) { |
| throw $e; |
| } |
| } |
| } |
| |
| <?php |
| |
| |
| class QcloudMessage |
| { |
| public function __construct( |
| public array|string $phoneNumberSet = [], |
| public int|string $templateId = 0, |
| public array $templateParamSet = [], |
| ) { |
| |
| } |
| |
| public function setPhoneNumber(string|array $phoneNumberSet) |
| { |
| $this->phoneNumberSet = is_array($phoneNumberSet) ? $phoneNumberSet : [$phoneNumberSet]; |
| |
| return $this; |
| } |
| |
| public function setTemplateId(string|int $templateId) |
| { |
| $this->templateId = $templateId; |
| |
| return $this; |
| } |
| |
| public function setTemplateParamSet(array $templateParamSet) |
| { |
| $this->templateParamSet = $templateParamSet; |
| |
| return $this; |
| } |
| } |