因之前的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';
}
php artisan make:notification VerifyPhoneNumber
class VerifyPhoneNumber extends Notification implements ShouldQueue
{
use Queueable;
public function __construct()
{
$this->onConnection('new-member');
}
public function via($notifiable)
{
return ['qcloud'];
}
public function toQcloudSms($notifiable)
{
return (new QcloudMessage)
->setTemplateId('1024635')
->setTemplateParamSet([
(string) rand(1000, 9999),
'10'
]);
}
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();
}
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;
}
}