腾讯云sdk 支持 腾讯云短信 Laravel Notification [最新版]

Laravel框架
435
0
0
2022-05-11
因之前的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;
    }
}