首页  >  文章  >  后端开发  >  laravel框架下php手机短信验证码实现流程

laravel框架下php手机短信验证码实现流程

不言
不言原创
2018-08-10 11:04:552243浏览

本篇文章给大家带来的内容是关于 laravel框架下php手机短信验证码实现流程,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

具体短信服务提供商大家可以自由选择。

1,实现流程

输入手机号,点击获取验证码
提交正确的短信验证码后,注册完成

2,实现思路图

这里写图片描述

3,注册 云片,以及开发信息认证,模板设置,这里就不详细展开了

4, 安装 easy-sms,easy-sms 是安正超写的一个短信发送组件,利用这个组件,我们可以快速的实现短信发送功能。

 composer require "overtrue/easy-sms"
    //新建配置文件
    touch config/easysms.php

然后在 easysms.php 文件内 添加以下内容:

 <?php

   return [

       &#39;timeout&#39;=>5.0,
       &#39;default&#39;=>[
           // 网关调用策略,默认:顺序调用
           &#39;strategy&#39; => \Overtrue\EasySms\Strategies\OrderStrategy::class,

           // 默认可用的发送网关
           &#39;gateways&#39; => [
               &#39;yunpian&#39;,
           ],
       ],
       // 可用的网关配置
       &#39;gateways&#39; => [
           &#39;errorlog&#39; => [
               &#39;file&#39; => &#39;/tmp/easy-sms.log&#39;,
           ],
           &#39;yunpian&#39; => [
               &#39;api_key&#39; => env(&#39;YUNPIAN_API_KEY&#39;),
           ],
       ],


   ];

然后创建一个 ServiceProvider

  php artisan make:provider EasySmsServiceProvider

修改文件

app/providers/EasySmsServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Overtrue\EasySms\EasySms;

class EasySmsServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton(EasySms::class,function ($app){

            return new EasySms(config(&#39;easysms&#39;));

        });

        $this->app->alias(EasySms::class,&#39;easysms&#39;);
    }
}

最后 打开config/app.php 在 providers 中增加

 App\Providers\EasySmsServiceProvider::class,

5,获取云片的API_KEY

在 .env中配置 YUNPIAN_API_KEY,注意下面需要替换为你自己的 key

6,控制器代码 获取验证码(将code 以及key存入缓存)

public function getVerificationCode($request)
    {
        if(FALSE === $this->validateApiRequest($request->all(),
                [&#39;mobile&#39; => &#39;required|regex:/^1[34578]\d{9}$/|unique:users&#39;],[
                    &#39;mobile.required&#39;=>&#39;请输入手机号&#39;,
                    &#39;mobile.regex&#39;=>&#39;手机号格式不正确&#39;,
                    &#39;mobile.unique&#39;=>&#39;手机号已存在&#39;
                ])){
            return false;
        }

        $mobile = trim($request->get(&#39;mobile&#39;));
       $code = str_pad(random_int(1,9999),4,0,STR_PAD_LEFT);


        try{
             $easySms->send($mobile,
                [&#39;content&#39;=>"【UKNOW】您的验证码是{$code}。如非本人操作,请忽略本短信"]             );

        }catch(\GuzzleHttp\Exception\ClientException $exception){

            $response = $exception->getResponse();
            $result =json_decode($response->getBody()->getContents(),true);
            $this->setMsg($result[&#39;msg&#39;]?? &#39;短信发送异常&#39;);
            return false;
        }

        $key = &#39;verificationCode&#39;.str_random(15);
        $expiredAt = now()->addMinutes(1);
        Cache::put($key,[&#39;mobile&#39;=>$mobile,&#39;code&#39;=>$code],$expiredAt);

        return [
            &#39;verification_key&#39;=>$key,
            &#39;expiredAt&#39;=>$expiredAt->toDateTimeString(),
            &#39;verification_code&#39;=>$code
            ];
    }

7,对比验证码

public function userStore($mobile, $verification_key,$code,$password,$password_confirmation)
    {

        $params = [
            &#39;mobile&#39;=>$mobile,
            &#39;verification_key&#39;=>$verification_key,
            &#39;code&#39;=>$code,
            &#39;password&#39;=>$password,
            &#39;password_confirmation&#39;=>$password_confirmation
        ];
        //参数判断
        if (
            FALSE === $this->validateApiRequest($params, [
                &#39;mobile&#39;  => &#39;required|regex:/^1[34578]\d{9}$/|unique:users&#39;,
                &#39;code&#39;    => &#39;required&#39;,
                &#39;verification_key&#39;=>&#39;required&#39;,
                &#39;password&#39;     => &#39;required|min:6|confirmed&#39;,
                &#39;password_confirmation&#39; => &#39;required&#39;,
            ], [
                &#39;mobile.required&#39; => &#39;请输入手机号&#39;,
                &#39;mobile.regex&#39;    => &#39;手机号格式不正确&#39;,
                &#39;mobile.unique&#39;   => &#39;手机号已存在&#39;,
                &#39;code.required&#39;   => &#39;请输入短信验证码&#39;,
                &#39;password.required&#39;    => &#39;请输入密码&#39;,
                &#39;password.min&#39;         => &#39;密码不得小于6位&#39;,
                &#39;password.confirmed&#39;   => &#39;密码前后不一致&#39;,
                &#39;password_confirmation.required&#39;=>&#39;请再次输入密码&#39;,
                &#39;verification_key.required&#39;=>&#39;请输入短信验证码&#39;
            ])
        ) {
            return false;
        }

        $verifyData = Cache::get($verification_key);
        if( !$verifyData){
            $this->setMsg(&#39;验证码已失效&#39;);
            return false;
        }
        if(!hash_equals($code,(string)$verifyData[&#39;code&#39;])){
            $this->setMsg(&#39;验证码错误&#39;);
            return false;
        }

        Cache::forget($verification_key);
        $user = User::create([
            &#39;mobile&#39;=>$mobile,
            &#39;password&#39;=>bcrypt($password)
        ]);
        if(!$user){
            $this->setMsg(&#39;注册失败&#39;);
            return false;
        }
        return true;
    }

 相关推荐:

thinkphp模板如何判断是手机微信支付还是微信扫码支付

PHP想要实现页面跳转功能具体怎么操作?(函数标签示例)

以上是laravel框架下php手机短信验证码实现流程的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn