Laravel——短信注册

正在公司做一个商城项目,由于只有一个后台,所以注册用短信注册也就轮到我来做的。刚刚开始,我内心还是有点虚的,还好有Laravel-china社区的summer大神,写的书。在里面参考了它的写法和思路,并且用了easy-sms包,这才用了半个下午的时间,顺利的做出来了,晚上赶紧和大家分享一波了。

  1. 确定短信运营商

我看到大佬都是用的云片,我也就毫不犹豫的大力推荐公司用这个短信平台了,不过其他的也可以咯。

  • 首先自己注册一个帐号,然后找到这个

  • 点击开始接入,完成新手引导过程。

  • 第二部的签名和模板必须填写,类似我下面填写的这样

值得注意的是这个模板必须和你到时候用 easy-sms包的时候,设定的短信内容必须和这个一模一样,不然会报错的。
还有就是记得一定得拿到APIKEY。到时候,在env里进行配置。

# 云片
YUNPIAN_API_KEY=9c60bdd**********
  1. 安装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.phpproviders里增加刚刚创建的服务写进去,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
  1. 创建路由和对应的控制器

  • 首先创建路由,我们需要一个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

再直接在里面写registerajaxregister方法了

  • 代码逻辑
    修改文件
    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 是可防止时序攻击的字符串比较的~

以上就是我整个的过程。

猜你喜欢

转载自juejin.im/post/5b0bf9c8f265da08e23376be