GithubHelp home page GithubHelp logo

nbczw8750 / think-notification-easysms-channel Goto Github PK

View Code? Open in Web Editor NEW

This project forked from liuwave/think-notification-easysms-channel

0.0 1.0 0.0 5 KB

使用 overtrue/easy-sms 发送 ThinkPHP6 消息通知。

PHP 100.00%

think-notification-easysms-channel's Introduction

EasySms Notification channel For ThinkPHP6

使用 overtrue/easy-sms 发送 ThinkPHP6 消息通知。

安装

$ composer require liuwave/think-notification-easysms-channel

配置

1、安装插件会自动生成 config/easysms.php 文件,若未生成,可以执行以下命令:

$ php think vendor:publish

2、在config/easysms.php 中修改配置。

使用

1、创建通知

通知类,需要实现channelstoEasySms方法

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,
          ]);
    }
    
}

发送短信通知

继承创建手机号码类,需要插入trait yunwuxin\notification\Notifiable ,这里直接继承\Overtrue\EasySms\PhoneNumber,并实现prepareEasySms方法。

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'));
        

think-notification-easysms-channel's People

Contributors

liuwave avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.