Git Product home page Git Product logo

laravel-sms's Introduction

Laravel Sms

Laravel 贴合实际需求同时满足多种通道的短信发送组件

Build Status Scrutinizer Code Quality Code Coverage Build Status Latest Stable Version Latest Unstable Version License

Featrue

基于业务需求在 overtrue/easy-sms 基础进行扩展开发,主要实现如下目标:

  1. 支持短信验证码直接在 config 中配置模板ID
  2. 支持短信验证码自定义长度
  3. 支持短信验证码有效分钟,默认5分钟
  4. 支持短信验证码重试次数,防止用户意外输错验证码导致需要再次发送验证码的问题。
  5. 支持短信验证码未验证时,用户再次请求验证码,在有效分钟内验证码保持一致。
  6. 集成短信发送路由,支持 web 和 api 发送方式。
  7. 支持验证码调试,debug 模式下可直接查询手机号目前有效的验证码
  8. 支持短信验证码发送记录保存到数据库

TODO:

  1. 支持语音验证码

安装

composer require ibrand/laravel-sms:~1.0 -vvv

低于 Laravel5.5 版本

config/app.php 文件中 'providers' 添加

iBrand\Sms\ServiceProvider::class

config/app.php 文件中 'aliases' 添加

'Sms'=> iBrand\Sms\Facade::class

使用

发送验证码

实现了发送短信验证码路由,支持 web 和 api ,可以自定义路由的 prefix。

'route' => [
        'prefix' => 'sms',
        'middleware' => ['web'],
    ],
    
or

'route' => [
        'prefix' => 'sms',
        'middleware' => ['api'],
    ],

POST请求 http://your.domain/sms/verify-code

参数:mobile

备注:为了开发调试方便,在 debug 模式下不会验证手机的有效性。

返回参数:

{
    "status": true,
    "message": "短信发送成功"
}

验证验证码

    use iBrand\Sms\Facade as Sms;
    

    if (!Sms::checkCode(\request('mobile'), \request('code'))) {
            //Add you code.
        }

配置模板 ID

config/ibrand/sms.phpgateways 参数可以直接添加 code_template_id 来配置模板 id

    // 可用的网关配置
        'gateways' => [

            'errorlog' => [
                'file' => '/tmp/easy-sms.log',
            ],

            'yunpian' => [
                'api_key' => '824f0ff2f71cab52936axxxxxxxxxx',
            ],

            'aliyun' => [
                'access_key_id' => 'dalvTXXX',
                'access_key_secret' => 'XXXX',
                'sign_name' => '阿里云短信测试专用',
                'code_template_id' => 'SMS_80215252'
            ],

            'alidayu' => 
                //...
            ],
        ],

配置 Content

非模板类通道,可以通过 config/ibrand/sms.php 自定义短信内容

'content' => '【your signature】亲爱的用户,您的验证码是%s。有效期为%s分钟,请尽快验证。'

debug

在实际开发中会存在并不用真实发出验证码的情况,因此在 debug 模式下,可以通过

http://your.domain/api/sms/info?mobile=1898888XXXX 来直接只看某个手机号当前有效验证码信息。

database log

目前已经支持把发送记录保存到数据库,执行 php artisan migrate 生成  laravel_sms_log 表。

同时在 config/ibrand/sms.phpdblog 设置为 true

'dblog' => true,

Resources

  1. overtrue/easy-sms
  2. toplan/laravel-sms

laravel-sms's People

Contributors

chenbidepro avatar liuwei0514 avatar

Watchers

 avatar  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.