騰訊雲sdk 支援 騰訊雲簡訊 Laravel Notification [最新版]

Bill-Li發表於2021-07-06

因之前的github 第三方包沒有支援最新版的。也沒有打算寫擴充套件包支援!

使用

  • 擴充套件Laravel 原生 Notifications, 在你的AppServiceProvider
protected function register()
    {
        Notification::resolved(
            fn (ChannelManager $channel) => $channel->extend('qcloud', fn ($app) => new QcloudHandler)
        );
    }
  • 在你的model 新增方法routeNotificationForQcloud
public function routeNotificationForQcloud(Notification $notification): string
    {
        return $this->phone_number ?: '18898726543';
    }
  • 使用命令建立Notifications
php artisan make:notification VerifyPhoneNumber
class VerifyPhoneNumber extends Notification implements ShouldQueue
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->onConnection('new-member');
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['qcloud'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toQcloudSms($notifiable)
    {
        return (new QcloudMessage)
            ->setTemplateId('1024635')
            ->setTemplateParamSet([
                (string) rand(1000, 9999),
                '10'
            ]);
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

複製下面程式碼到你的專案

<?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();
    }

    /**
     * Send the given notification.
     *
     * @param mixed $notifiable
     * @param Notification $notification
     *
     * @return mixed
     * @throws AliyunSmsException
     * @throws ClientException
     * @throws ServerException
     */
    public function send($notifiable, Notification $notification)
    {
        if (! $to = $notifiable->routeNotificationFor('qcloud', $notification)) {
            return null;
        }

        /** @var QcloudMessage $message */
        $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;
    }
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結
Bill

相關文章