Laravel-sms 2.4
插件请戳这里:https://github.com/toplan/laravel-sms,该插件支持laravel 5.2+, 5.1版本的需要修改routes。
安装
composer require 'toplan/laravel-sms:2.4.*'
php artisan vendor:publish
php artisan migrate
和该插件的开发者toplan沟通后,laravel 5.1下要使用该插件的方法是,
vi vendor/toplan/laravel-sms/src/Toplan/LaravelSms/routes.php
// '注释掉这个中间件'
//'middleware' => config('laravel-sms.middleware', 'web'),
配置
短信服务商
我使用的是luosimao。
vi config/phpsms.php
'scheme' => [
'Luosimao',
],
*/
'Luosimao' => [
// 短信 API key
// 在管理中心->短信->触发发送下查看
'apikey' => '你的apikey',
// 语言验证 API key
// 在管理中心->语音->语音验证下查看
'voiceApikey' => '你的voiceapi key',
],
验证码短信及有效时间
vi config/laravel-sms.php
'storage' => 'Toplan\Sms\SessionStorage',
'verifySmsContent' => '您的验证码是%s。有效期%s分钟,请尽快验证。【APP】',
'codeLength' => 5,
'codeValidMinutes' => 60,
使用
引入laravel-sms.js
该js绑定表单页面里的发送验证码按钮,并取得手机号输入框的内容。配置如下。我的表单中手机号input name为telephone,所以在下面如此指定。请求间隔我设置成了一个小时。
$('#sendVerifySmsButton').sms({
//laravel csrf token
token : "{{csrf_token()}}",
//定义如何获取mobile的值
mobile_selector : 'input[name=telephone]',
//手机号的检测规则
mobile_rule : 'mobile_required',
//语音验证
voice : false,
//请求间隔时间
interval : 3600
});
表单验证
我的前端表单是这样的
<div class="form-group control-group">
{!! Form::label('telephone', '手机号码', ['class' => 'col-sm-2 control-label']) !!}
<div class="controls col-sm-10">
{!! Form::text('telephone', $telephone, ['class' => 'form-control', 'required' => true, 'data-validation-required-message' => '请输入11位手机号
码', 'placeholder' => '请输入11位手机号码', 'pattern' => '^((\+)?86|((\+)?86)?)0?1[3458]\d{9}$', 'data-validation-pattern-message' => '请输入正确的手机号码', 'maxlength' => '17']) !!}
<br/>
<button class="btn btn-highlight" id="sendVerifySmsButton" type="button">
发送验证码
</button>
</div>
</div>
<div class="form-group control-group">
{!! Form::label('vcode', '验证码', ['class' => 'col-sm-2 control-label']) !!}
<div class="controls col-sm-10">
{!! Form::text('vcode', '', ['class' => 'form-control', 'required' => true, 'data-validation-required-message' => '请输入收到的验证码', 'placeholder' => '请输入验证码', 'pattern' => '^\d{5}$', 'data-validation-pattern-message' => '请>输入正确的验证码', 'maxlength' => '5']) !!}
</div>
</div>
如果你的表单验证控制写在这里,
vi app/Http/Requests/Frontend/PostFormRequest.php
添加验证方法如下,这几条验证规则由插件定义。
confirm_mobile_not_change,verify_code|confirm_rule:mobile,mobile_required
public function rules()
{
return [
...
'telephone' => 'required|max:55|confirm_mobile_not_change',
'vcode' => 'required|verify_code|confirm_rule:mobile,mobile_required',
];
}
插件通过表单验证的逻辑实现验证码是否和手机号匹配,所以是很方便的。除此之外该插件还有队列及其他控制,很优秀。
增加黑名单控制
因为项目要求,需要增加手机号黑名单控制,我就把该插件稍微改了一下,给大家分享一下做法。
给laravel-sms表增加一个标志位
创建数据库迁移文件
php artisan make:migration add_blocked_to_laravel_sms_table
修改迁移动作
vi database/migrations/***_add_status_to_conferences_table.php
up里增加标志位项,down里移除。
public function up()
{
//
Schema::table('laravel_sms', function ($table){
$table->integer('blocked')->default('0')->after('id')->comment('1 is blocked, 0 is not blocked');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
Schema::table('laravel_sms', function ($table){
$table->dropColumn('blocked');
});
}
阻止给已被禁止的手机号发送短信
先验证是否该手机号已被封禁。
vi vendor/toplan/laravel-sms/src/Toplan/LaravelSms/SmsManager.php
public function validateFields(array $data){
....
// check if blocked
if(DB::table('laravel_sms')->where('to', $data['mobile'])->where('blocked', 1)->get()){
return self::generateResult(false, '已封禁');
}
创建拉黑和解除黑名单方法
创建routes并导入到这两个方法里面。
public function blacklist($telephone){
//blacklist telephone
DB::table('laravel_sms')->where('to', $telephone)->update(['blocked'=>1]);
...
}
public function undo_blacklist($telephone){
//undo blacklist telephone
DB::table('laravel_sms')->where('to', $telephone)->update(['blocked'=>0]);
...
}
防止跨域请求CSRF
这是个题外的,貌似原作的post表单里写了csrf_token(),但routes里面没看到使用,所以我把这个文件也修改了,作为备注。
vi vendor/toplan/laravel-sms/src/Toplan/LaravelSms/routes.php
Route::post('verify-code', array('before'=>'csrf', 'uses' => 'Toplan\Sms\SmsController@postSendCode'));
本文介绍如何在 Laravel 项目中集成 Laravel-sms 插件,实现短信验证码功能,并添加黑名单控制。涵盖安装步骤、配置说明、表单验证及自定义功能。

477

被折叠的 条评论
为什么被折叠?



