本文通过示例代码给大家介绍了使用 laravel-sms 构建短信验证码发送校验模块,非常不错,具有参考借鉴价值,需要的朋友可以参考下

laravel 实现短信验证码功能,搜索资料发现比较流行的有两个包:

使用 laravel sms 构建短信验证码发送校验功能  第1张

一个是laravel sms 地址  https://github.com/toplan/laravel-sms

一个是easy sms 地址https://github.com/overtrue/easy-sms,

项目中需要实现一个发送和验证短信验证码的功能。以前的办法稍显繁琐。经高人指点,发现可以用 laravel-sms 这个包替代。且配置和使用简单易学。故有了这篇示例。

本例使用了Laravel 5.5、 Api Starter Kit 以及 Laravel Sms 2.6。

本例使用的短信服务商为云片。

安装

在项目根目录下执行(推荐):

composer require toplan/laravel-sms:~2.6
composer require toplan/laravel-sms:~2.6 

也可以在 composer.json 的 require 字段中添加:

"toplan/laravel-sms": "2.6"
"toplan/laravel-sms": "2.6" 

然后在项目根目录下执行:

composer update
composer update

 在 config/app.php 的 providers 数组中添加:

Toplan\PhpSms\PhpSmsServiceProvider::class,
Toplan\Sms\SmsManagerServiceProvider::class,
Toplan\PhpSms\PhpSmsServiceProvider::class,
Toplan\Sms\SmsManagerServiceProvider::class, 

并在 aliases 数组里添加:

'PhpSms' => Toplan\PhpSms\Facades\Sms::class,
'SmsManager' => Toplan\Sms\Facades\SmsManager::class,
'PhpSms' => Toplan\PhpSms\Facades\Sms::class,
'SmsManager' => Toplan\Sms\Facades\SmsManager::class,

在项目根目录下执行:

php artisan vendor:publish --provider="Toplan\PhpSms\PhpSmsServiceProvider"
php artisan vendor:publish --provider="Toplan\Sms\SmsManagerServiceProvider"
php artisan vendor:publish --provider="Toplan\PhpSms\PhpSmsServiceProvider"
php artisan vendor:publish --provider="Toplan\Sms\SmsManagerServiceProvider" 

会在 config 文件夹中生成两个配置文件:phpsms.php 和 laravel-sms.php。

在 phpsms.php 中可以配置代理器信息及均衡调度方案。

在 laravel-sms.php 中可以配置验证码的发送与验证方案。

同时会向 database\migrations 中复制 2015_12_21_111514_create_sms_table.php 文件。用于生成 laravel_sms 表。

配置

这里仅以云片为例。

配置 phpsms.php

设置 phpsms.php 中 agnets 数组中云片的代理器信息。

'YunPian' => [
 //用户唯一标识,必须
 'apikey' => '在这里填写你的 APIKEY',
],
'YunPian' => [
 //用户唯一标识,必须
 'apikey' => '在这里填写你的 APIKEY',
],

设置 scheme 数组,配置均衡调度方案。

'scheme' => [
 'YunPian',
],
'scheme' => [
 'YunPian',
], 

配置 laravel-sms.php

设置内置路由。 

'route' => [
 'enable'  => true,
 'prefix'  => 'laravel-sms', 
 'middleware' => ['api'],
],
'route' => [
 'enable'  => true,
 'prefix'  => 'laravel-sms', 
 'middleware' => ['api'],
],

设置请求间隔,单位为秒。

'interval' => 60,
'interval' => 60, 

设置号码验证规则。

"toplan/laravel-sms": "2.6"
"toplan/laravel-sms": "2.6" 0

设置验证码规则。

"toplan/laravel-sms": "2.6"
"toplan/laravel-sms": "2.6" 1

设置验证码内容短信。

"toplan/laravel-sms": "2.6"
"toplan/laravel-sms": "2.6" 2

如果有需要,可以开启数据库日志。需要提前运行 php artisan migrate 生成 laravel_sms 表。

"toplan/laravel-sms": "2.6"
"toplan/laravel-sms": "2.6" 3

 API 实现

在 app/Utils 下新建 SmsCodeUtil.php,并在里面实现验证码发送和校验功能。这样其他类可以随时调用,提高代码的复用性。

发送模块

发送前需要对手机号进行校验,包括:

"toplan/laravel-sms": "2.6"
"toplan/laravel-sms": "2.6" 4

通过验证后,再使用 requestVerifySms() 发送验证码。

具体代码如下:

"toplan/laravel-sms": "2.6"
"toplan/laravel-sms": "2.6" 5

校验模块

登入时,可能需要校验手机号和验证码。所以需要在 SmsCodeUtil.php 中添加验证码校验功能。这里官方 Github 上已经给出了代码,稍作修改即可。

"toplan/laravel-sms": "2.6"
"toplan/laravel-sms": "2.6" 6

功能测试

接下来配置路由和控制器,测试下功能是否正常。

可以同时打开 host-domain/laravel-sms/info 查看验证码短信发送和校验状态。

若启用了数据库日志,可以在 laravel_sms 表中查看短信发送结果的详细信息。

先在 api.php 中添加:

"toplan/laravel-sms": "2.6"
"toplan/laravel-sms": "2.6" 7

再在 LoginController.php 中添加:

"toplan/laravel-sms": "2.6"
"toplan/laravel-sms": "2.6" 8

然后使用 Postman 或其他类似工具测试 Api 功能。

发送验证码 

"toplan/laravel-sms": "2.6"
"toplan/laravel-sms": "2.6" 9

若通过验证并发送成功,则会返回:

composer update
composer update0

同时填写的手机号接受到验证码。

若验证失败或发送失败,则会返回对应的错误信息。

校验验证码

composer update
composer update1

若通过验证,则无返回。

若验证失败,则会返回对应的错误信息。

本地化提示信息语言

在 laravel-sms.php 中提供了部分提示信息的自定义。想要将剩余部分的提示信息转换为本地语言,需要另行处理。

首先确保 config/app.php 中的语言设置正确。这里设置为 zh_cn。

composer update
composer update2

然后在 resources\lang\zh_cn 文件夹下新建 validation.php,并填入本地化信息:

composer update
composer update3

重新 POST 相关地址,可以看到对应的提示信息语言已经本地化。

总结

以上所述是小编给大家介绍的使用 laravel-sms 构建短信验证码发送校验模块,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对知优网网站的支持!

转载请说明出处
知优网 » 使用 laravel sms 构建短信验证码发送校验功能

发表评论

您需要后才能发表评论