PHP code example of liuwave / think-notification-easysms-channel
1. Go to this page and download the library: Download liuwave/think-notification-easysms-channel library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
liuwave / think-notification-easysms-channel example snippets
namespace app\common\notification;
use liuwave\notification\channel\EasySms as EasySmsChannel;
use liuwave\notification\message\EasySms as EasySmsMessage;
use yunwuxin\Notification;
/**
* Class ValidateCode
* @package app\common\notification
*/
class ValidateCode extends Notification
{
/**
* @var string
*/
public $code = '';
/**
* @var string
*/
public $template = "";
/**
* ValidateCode constructor.
*
* @param $template
* @param string $code
*/
public function __construct(string $template, string $code = '')
{
$this->template = $template;
$this->code = $code;
}
/**
* @inheritDoc
*/
public function channels($notifiable)
{
return [ EasySmsChannel::class ];
}
/**
* @param \yunwuxin\notification\Notifiable $notifiable
*
* @return \liuwave\notification\message\EasySms
*/
public function toEasySms($notifiable)
{
return ( new EasySmsMessage )->setContent('验证码是$code')
->setTemplate($this->template)
->setData([
'code' => empty($this->code) ? rand(1000, 9999) : $this->code,
]);
}
}
namespace app\common\notification\user;
use yunwuxin\notification\Notifiable;
/**
* Class PhoneNumber
* @package app\common\notification\notifiable
* @mixin Notifiable
*/
class PhoneNumber extends \Overtrue\EasySms\PhoneNumber
{
use Notifiable;
/**
* @return $this
*/
public function prepareEasySms()
{
return $this;
}
}
$to = '181*****932';
// 使用 Notifiable Trait 发送
(new \app\common\notification\user\PhoneNumber($to))
->notify(new \app\common\notification\ValidateCode('SMS_15****670'));
// 使用 Notification Facade 发送
\yunwuxin\facade\Notification::send( new \app\common\notification\user\PhoneNumber($to),
new \app\common\notification\ValidateCode('SMS_15****670'));
shell
$ php think vendor:publish