10000 增加功能 集成短信验证码 · czsvn/laravel_template_with_vue@ba4455b · GitHub
[go: up one dir, main page]

Skip to content

Commit ba4455b

Browse files
committed
增加功能 集成短信验证码
1 parent 32ce2d3 commit ba4455b

File tree

13 files changed

+851
-206
lines changed

13 files changed

+851
-206
lines changed

backend/.env.example

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ APP_URL=http://localhost
88
DB_CONNECTION=mysql
99
DB_HOST=localhost
1010
DB_PORT=3306
11-
DB_DATABASE=root
12-
DB_USERNAME=laravel
11+
DB_DATABASE=laravel
12+
DB_USERNAME=root
1313
DB_PASSWORD=
1414

1515
BROADCAST_DRIVER=log
@@ -36,4 +36,8 @@ PERSONAL_CLIENT_ID=1
3636
PERSONAL_CLIENT_SECRET=
3737

3838
PASSPORT_CLIENT_ID=2
39-
PASSPORT_CLIENT_SECRET=
39+
PASSPORT_CLIENT_SECRET=
40+
41+
// 短信集成
42+
SMS_GATEWAY_TYPE=null
43+
SMS_APP_KEY=null

backend/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,8 @@ Homestead.yaml
1010
npm-debug.log
1111
yarn-error.log
1212
.env
13+
.idea
14+
_ide_helper.php
15+
_ide_helper_models.php
16+
.phpstorm.meta.php
17+

backend/app/Http/Controllers/Import/UserImportHandler.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public function handle($import)
1818
// get the results
1919
// 获取第一个工作表电子表格的数据
2020
$result = $import->first()->toArray();
21-
dump($result);
2221
$lists = [];
2322
foreach ($result as $v){
2423
$data = [

backend/app/Http/Controllers/Result.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ public function successWithData($data)
3030
], 200);
3131
}
3232

33+
public function successWithInfo($info)
34+
{
35+
return response()->json([
36+
'info' => $info,
37+
'status' => 'success',
38+
'status_code' => 200
39+
], 200);
40+
}
41+
3342
public function error()
3443
{
3544
return response()->json([
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Carbon\Carbon;
6+
use Illuminate\Http\Request;
7+
use Illuminate\Support\Facades\Cache;
8+
use Overtrue\EasySms\EasySms;
9+
10+
class SmsController extends Controller
11+
{
12+
//
13+
use Result;
14+
15+
protected function sendVerifyCode($phone, $template = '54224')
16+
{
17+
// 3、 生成验证码并写入到数据表中
18+
$verify = rand(100000,999999);
19+
// 缓存信息,120秒 2分钟
20+
$expiresAt = Carbon::now()->addMinutes(2);
21+
Cache::put($phone, $verify, $expiresAt);
22+
23+
// 4、 发送验证码到指定的手机
24+
$easySms = new EasySms($this->getParams());
25+
$easySms->send($phone, [
26+
'template' => $template,
27+
'data' => [
28+
'code' => $verify
29+
],
30+
]);
31+
return $this->successWithInfo('验证码已经发送');
32+
}
33+
34+
// 手机验证码功能,判断发送的验证码是否成功
35+
protected function checkVerify($phone, $verify)
36+
{
37+
if (Cache::has($phone)) {
38+
$check = Cache::get($phone);
39+
} else {
40+
$info = '手机验证码不存在';
41+
return $this->errorWithInfo($info);
42+
}
43+
if ($check != $verify) {
44+
$info = '输入的验证码不正确,请重新输入';
45+
return $this->errorWithInfo($info);
46+
} else {
47+
$info = '输入的验证码正确';
48+
return $this->successWithInfo($info);
49+
}
50+
}
51+
52+
// 发送验证码
53+
public function send(Request $request)
54+
{
55+
$phone = $request->input('phone');
56+
return $this->sendVerifyCode($phone);
57+
}
58+
59+
// 验证码校验
60+
public function verify(Request $request)
61+
{
62+
$phone = $request->input('phone');
63+
$code = $request->input('code', 111111);
64+
return $this->checkVerify($phone, $code);
65+
}
66+
67+
// 配置参数
68+
protected function getParams()
69+
{
70+
71+
72+
$key = config("app.sms_api_key");
73+
$sms_config = [
74+
// HTTP 请求的超时时间(秒)
75+
'timeout' => 5.0,
76+
77+
// 默认发送配置
78+
'default' => [
79+
// 网关调用策略,默认:顺序调用
80+
'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class,
81+
82+
// 默认可用的发送网关
83+
'gateways' => [],
84+
],
85+
// 可用的网关配置
86+
'gateways' => [
87+
'errorlog' => [
88+
'file' => '/tmp/easy-sms.log',
89+
],
90+
'juhe' => [
91+
'app_key' => $key,
92+
],
93+
],
94+
];
95+
// josn转数组,env文件里面的配置参数必须用双引号引起来
96+
$gateway = json_decode(config("app.sms_gateway_type"),true);
97+
$sms_config['default']['gateways'] = $gateway;
98+
return $sms_config;
99+
}
100+
}

backend/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"laravel/framework": "5.5.*",
1414
"laravel/passport": "^4.0",
1515
"laravel/tinker": "~1.0",
16-
"maatwebsite/excel": "~2.1.0"
16+
"maatwebsite/excel": "~2.1.0",
17+
"overtrue/easy-sms": "^1.0"
1718
},
1819
"require-dev": {
1920
"filp/whoops": "~2.0",

0 commit comments

Comments
 (0)
0