Laravel实现短信注册的示例代码
正在公司做一个商城项目,由于只有一个后台,所以注册用短信注册也就轮到我来做的。刚刚开始,我内心还是有点虚的,还好有 laravel-china
社区的 summer
大神,写的书。在里面参考了它的写法和思路,并且用了 easy-sms
包,这才用了半个下午的时间,顺利的做出来了,晚上赶紧和大家分享一波了。
1、确定短信运营商
我看到大佬都是用的云片,我也就毫不犹豫的大力推荐公司用这个短信平台了,不过其他的也可以咯。
首先自己注册一个帐号,然后找到这个
点击开始接入,完成新手引导过程。
第二部的签名和模板必须填写,类似我下面填写的这样
值得注意的是这个模板必须和你到时候用 easy-sms
包的时候,设定的短信内容必须和这个一模一样,不然会报错的。
还有就是记得一定得拿到apikey。到时候,在env里进行配置。
# 云片 yunpian_api_key=9c60bdd**********
2、安装 easy-sms
包
利用这个包,可以快速的实现短信发送功能。
composer require "overtrue/easy-sms"
由于该组件还没有 laravel 的 serviceprovider
,为了方便使用,我们可以自己封装一下。
首先在 config 目录中增加 easysms.php
文件
在 config/easysms.php 填写如下内容。
<?php return [ // http 请求的超时时间(秒) 'timeout' => 5.0, // 默认发送配置 'default' => [ // 网关调用策略,默认:顺序调用 'strategy' => \overtrue\easysms\strategies\orderstrategy::class, // 默认可用的发送网关 'gateways' => [ 'yunpian', ], ], // 可用的网关配置 'gateways' => [ 'errorlog' => [ 'file' => '/tmp/easy-sms.log', ], 'yunpian' => [ 'api_key' => env('yunpian_api_key'), ], ], ];
然后创建一个 serviceprovider
php artisan make:provider easysmsserviceprovider
修改文件 app/providers/easysmsserviceprovider.php
<?php namespace app\providers; use overtrue\easysms\easysms; use illuminate\support\serviceprovider; class easysmsserviceprovider extends serviceprovider { /** * bootstrap the application services. * * @return void */ public function boot() { // } /** * register the application services. * * @return void */ public function register() { $this->app->singleton(easysms::class, function ($app) { return new easysms(config('easysms')); }); $this->app->alias(easysms::class, 'easysms'); } }
最后在 config/app.php
在 providers
里增加刚刚创建的服务写进去,app\providers\easysmsserviceprovider::class,
app\providers\appserviceprovider::class, app\providers\authserviceprovider::class, // app\providers\broadcastserviceprovider::class, app\providers\eventserviceprovider::class, app\providers\routeserviceprovider::class, app\providers\easysmsserviceprovider::class, //easy-sms
3、创建路由和对应的控制器
首先创建路由,我们需要一个ajax请求短信验证码的方法,和一个进行确认注册的逻辑方法,如下:
route::group(['prefix' => 'verificationcodes', 'as' => 'verificationcodes.'], function() { route::post('register', 'verificationcodescontroller@register')->name('register'); route::get('ajaxregister', 'verificationcodescontroller@ajaxregister')->name('ajaxregister'); });
路由创建好了,我们用命令生成controller了
php artisan make:controller home\verificationcodescontroller
再直接在里面写 register
和 ajaxregister
方法了
代码逻辑
修改文件
app/home/verificationcodescontroller.php
<?php . . . use overtrue\easysms\easysms; use app\models\system\user; class verificationcodescontroller extends controller { // 这里验证就不写了。 public function ajaxregister(verificationcoderequest $request, easysms $easysms) { //获取前端ajax传过来的手机号 $phone = $request->phone; // 生成4位随机数,左侧补0 $code = str_pad(random_int(1, 9999), 4, 0, str_pad_left); try { $result = $easysms->send($mobile, [ 'content' => "【安拾商城】您的验证码是{$code}。如非本人操作,请忽略本短信" ]); } catch (overtrue\easysms\exceptions\nogatewayavailableexception $exception) { $response = $exception->getexceptions(); return response()->json($response); } //生成一个不重复的key 用来搭配缓存cache判断是否过期 $key = 'verificationcode_' . str_random(15); $expiredat = now()->addminutes(10); // 缓存验证码 10 分钟过期。 \cache::put($key, ['mobile' => $mobile, 'code'=> $code], $expiredat); return response()->json([ 'key' => $key, 'expired_at' => $expiredat->todatetimestring(), ], 201); }
这样,用户就能收到短信,并且前端应该保存这个 key
,提交注册表单的时候传递给后台,判断是否已经过期。下面就是判断是否过期,验证码是否错误。
public function register(verificationcoderequest $request) { //获取刚刚缓存的验证码和key $verifydata = \cache::get($request->verification_key); //如果数据不存在,说明验证码已经失效。 if(!$verifydata) { return response()->json(['status' =>0, 'message'=> '短信验证码已失效'], 422); } // 检验前端传过来的验证码是否和缓存中的一致 if (!hash_equals($verifydata['code'], $request->verification_code) { return redirect()->back()->with('warning', '短信验证码错误'); } $user = user::create([ 'mobile' => $verifydata['mobile'], 'password' => bcrypt($request->password), ]); // 清除验证码缓存 \cache::forget($request->verification_key); return redirect()->route('login')->with('success', '注册成功!'); }
上面的 hash_equals
是可防止时序攻击的字符串比较的~
以上就是我整个的过程。