Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

L03 7.x passport #48

Open
wants to merge 30 commits into
base: L03_7.x_passport
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
14257d9
2.7 API基础配置
invalid-email-address Oct 26, 2020
af5cd46
L3 3.2 安装使用easysms
invalid-email-address Oct 27, 2020
c0a2c61
L3 3.3 发送短信验证码
invalid-email-address Oct 27, 2020
6c6e864
L3 3.4 用户注册
invalid-email-address Oct 27, 2020
2972fe2
L3 3.6 速率限制
invalid-email-address Oct 27, 2020
a400317
L3 3.6 图片验证码
invalid-email-address Oct 28, 2020
210019d
L3 4.5 使用JWT
invalid-email-address Oct 28, 2020
153c909
L3 4.6 命令行生成token
invalid-email-address Oct 28, 2020
8169376
L3 5.1 获取用户信息
invalid-email-address Oct 29, 2020
b92258b
L3 5.2 编辑用户信息
invalid-email-address Oct 29, 2020
2bf537a
L3 6.1 分类列表
invalid-email-address Oct 29, 2020
f82b90d
L3 6.2 发布话题
invalid-email-address Nov 2, 2020
83808c9
L3 6.3 话题修改
invalid-email-address Nov 2, 2020
3b2cdb9
L3 6.4 删除话题
invalid-email-address Nov 2, 2020
550ef3d
L3 6.5 话题列表
invalid-email-address Nov 2, 2020
71074bc
L3 6.6 话题详情
invalid-email-address Nov 2, 2020
75d44d4
L3 7.2 删除回复
invalid-email-address Nov 3, 2020
8a994b3
L3 7.3 回复列表
invalid-email-address Nov 3, 2020
348e8cf
L3 7.4 消息通知列表
invalid-email-address Nov 3, 2020
960f60f
L3 7.5 未读消息统计
invalid-email-address Nov 3, 2020
07b8672
L3 7.6 标记通知为已读
invalid-email-address Nov 3, 2020
a29e1de
L3 8.2 用户权限列表
invalid-email-address Nov 4, 2020
2729cc4
L3 8.3 显示用户角色
invalid-email-address Nov 4, 2020
a66a529
L3 9.1 资源推荐列表
invalid-email-address Nov 4, 2020
6d1a3d5
L3 9.2 活跃用户
invalid-email-address Nov 4, 2020
8228857
L3 9.3 本地化
invalid-email-address Nov 4, 2020
036fa48
L3 10.2 话题单元测试
invalid-email-address Nov 5, 2020
af62c22
L3 11.2 安装passport
invalid-email-address Nov 6, 2020
de63028
L3 11.3 使用passport
invalid-email-address Nov 6, 2020
50bd3a8
L3 11.4 Passport第三方登录
invalid-email-address Nov 6, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
L3 3.6 图片验证码
  • Loading branch information
vagrant committed Oct 28, 2020
commit a400317c801aab5af3e50a69b1c8c52baa022af3
43 changes: 43 additions & 0 deletions app/Http/Controllers/Api/CaptchasController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\Api\CaptchaRequest;
use Gregwar\Captcha\CaptchaBuilder;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
use Illuminate\Http\Request;

class CaptchasController extends Controller
{
public function store(CaptchaRequest $request,CaptchaBuilder $captchaBuilder){
$key = 'captcha-'.Str::random(15);
$phone =$request->phone;

$captcha = $captchaBuilder->build();
$expiredAt = now()->addMinutes(5);
Cache::put($key,['phone' => $phone, 'code' => $captcha->getPhrase()],$expiredAt);

$result = [
'captcha_key'=>$key,
'expired_at'=>$expiredAt->toDateTimeString(),
'url'=>url('api/v1/captchas/'.$key),
'cptach_img_content'=>$captcha->inline(),
];
return response()->json($result)->setStatusCode(201);
}

public function show(Request $request){
$key = $request->captcha_key;
$captchaData = Cache::get($key);
if(!$captchaData){
abort(403,'验证码已失效');
}

$captchaBuilder = new CaptchaBuilder($captchaData['code']);
$captchaBuilder->build();
header('Content-type: image/jpeg');

$captchaBuilder->output();
}
}
12 changes: 11 additions & 1 deletion app/Http/Controllers/Api/VerificationCodesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,22 @@
use Illuminate\Support\Str;
use Overtrue\EasySms\EasySms;
use App\Http\Requests\Api\VerificationCodeRequest;
use Illuminate\Auth\AuthenticationException;

class VerificationCodesController extends Controller
{
public function store(VerificationCodeRequest $request,EasySms $easySms){
$phone = $request->phone;
$key = $request->captcha_key;
$captchaData = Cache::get($key);
if(!$captchaData){
abort(403,'图片验证码已失效');
}
if(!hash_equals($captchaData['code'],$request->captcha_code)){
// 返回401
throw new AuthenticationException('图片验证码错误');
}

$phone = $captchaData['phone'];
if(!app()->environment('production')){
$code = '1234';
}else{
Expand Down
19 changes: 19 additions & 0 deletions app/Http/Requests/Api/CaptchaRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Http\Requests\Api;

use Illuminate\Foundation\Http\FormRequest;

class CaptchaRequest extends FormRequest
{
public function rules()
{
return [
'phone' => [
'required',
'regex:/^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199)\d{8}$/',
'unique:users'
]
];
}
}
15 changes: 10 additions & 5 deletions app/Http/Requests/Api/VerificationCodeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ class VerificationCodeRequest extends FormRequest
public function rules()
{
return [
'phone' => [
'required',
'regex:/^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199)\d{8}$/',
'unique:users'
]
'captcha_key'=>'required|string',
'captcha_code'=>'required|string'
];
}

public function attributes()
{
return [
'captcha_key'=>'key必须',
'captcha_code'=>'验证码'
];
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"doctrine/dbal": "^2.12",
"fideloper/proxy": "^4.2",
"fruitcake/laravel-cors": "^1.0",
"gregwar/captcha": "^1.1",
"guzzlehttp/guzzle": "~6.3",
"intervention/image": "^2.5",
"laravel/framework": "^7.0",
Expand Down
61 changes: 60 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@

Route::prefix('v1')->namespace('Api')->name('api.v1.')->group(function () {
Route::middleware('throttle:10,1')->group(function (){
// 图片验证码
Route::post('captchas', 'CaptchasController@store')->name('captchas.store');




// 发送短信验证码
Route::post('verificationCodes', 'VerificationCodesController@store')->name('verificationCodes.store');
//用户注册
Expand All @@ -26,6 +32,6 @@
Route::middleware('throttle:' . config('api.rate_limits.access'))->group(function (){

});

Route::get('captchas/{captcha_key}', 'CaptchasController@show')->name('captchas.show');

});