欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Laravel實現(xiàn)短信注冊的示例代碼

 更新時間:2018年05月29日 11:40:00   作者:劍歌丶君  
這篇文章主要介紹了Laravel實現(xiàn)短信注冊的示例代碼,使用云片短信平臺,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

正在公司做一個商城項目,由于只有一個后臺,所以注冊用短信注冊也就輪到我來做的。剛剛開始,我內(nèi)心還是有點虛的,還好有 Laravel-china 社區(qū)的 summer 大神,寫的書。在里面參考了它的寫法和思路,并且用了 easy-sms 包,這才用了半個下午的時間,順利的做出來了,晚上趕緊和大家分享一波了。

1、確定短信運營商

我看到大佬都是用的云片,我也就毫不猶豫的大力推薦公司用這個短信平臺了,不過其他的也可以咯。

首先自己注冊一個帳號,然后找到這個

 

點擊開始接入,完成新手引導(dǎo)過程。

 

第二部的簽名和模板必須填寫,類似我下面填寫的這樣

 

值得注意的是這個模板必須和你到時候用 easy-sms 包的時候,設(shè)定的短信內(nèi)容必須和這個一模一樣,不然會報錯的。

還有就是記得一定得拿到APIKEY。到時候,在env里進(jìn)行配置。

# 云片
YUNPIAN_API_KEY=9c60bdd**********

2、安裝 easy-sms

利用這個包,可以快速的實現(xiàn)短信發(fā)送功能。

composer require "overtrue/easy-sms"

由于該組件還沒有 Laravel 的 ServiceProvider ,為了方便使用,我們可以自己封裝一下。

首先在 config 目錄中增加 easysms.php 文件

config/easysms.php 填寫如下內(nèi)容。

<?php
return [
 // HTTP 請求的超時時間(秒)
 'timeout' => 5.0,

 // 默認(rèn)發(fā)送配置
 'default' => [
  // 網(wǎng)關(guān)調(diào)用策略,默認(rèn):順序調(diào)用
  'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class,

  // 默認(rèn)可用的發(fā)送網(wǎng)關(guān)
  'gateways' => [
   'yunpian',
  ],
 ],
 // 可用的網(wǎng)關(guān)配置
 'gateways' => [
  'errorlog' => [
   'file' => '/tmp/easy-sms.log',
  ],
  'yunpian' => [
   'api_key' => env('YUNPIAN_API_KEY'),
  ],
 ],
];

然后創(chuàng)建一個 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 里增加剛剛創(chuàng)建的服務(wù)寫進(jìn)去,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、創(chuàng)建路由和對應(yīng)的控制器

首先創(chuàng)建路由,我們需要一個ajax請求短信驗證碼的方法,和一個進(jìn)行確認(rèn)注冊的邏輯方法,如下:

Route::group(['prefix' => 'verificationCodes', 'as' => 'verificationCodes.'], function() {
  Route::post('register', 'VerificationCodesController@register')->name('register');
  Route::get('ajaxregister', 'VerificationCodesController@ajaxregister')->name('ajaxregister');
 });

路由創(chuàng)建好了,我們用命令生成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位隨機數(shù),左側(cè)補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);
  }
  
  //生成一個不重復(fù)的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);
 }

這樣,用戶就能收到短信,并且前端應(yīng)該保存這個 key ,提交注冊表單的時候傳遞給后臺,判斷是否已經(jīng)過期。下面就是判斷是否過期,驗證碼是否錯誤。

public function register(VerificationCodeRequest $request)
{
 //獲取剛剛緩存的驗證碼和key
 $verifyData = \Cache::get($request->verification_key);
 
 //如果數(shù)據(jù)不存在,說明驗證碼已經(jīng)失效。
 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 是可防止時序攻擊的字符串比較的~

以上就是我整個的過程。

相關(guān)文章

最新評論