Browse Source

登陆新增验证码

master
wanghongjun 2 years ago
parent
commit
60fd740435
  1. 27
      app/controller/Passport.php
  2. 3
      composer.json
  3. 55
      composer.lock
  4. 39
      config/captcha.php
  5. 1
      route/app.php

27
app/controller/Passport.php

@ -6,6 +6,7 @@ use app\BaseController;
use app\model\User as UserModel;
use app\validate\User as UserValidate;
use think\exception\ValidateException;
use think\facade\Cookie;
use think\facade\Request;
class Passport extends BaseController
@ -63,10 +64,21 @@ class Passport extends BaseController
{
$data = Request::param();
$count = 0;
$defaultCount = 3;
try {
// 验证用户输入
validate(UserValidate::class)->scene('login')->check($data);
# 验证码验证
if ($count = Cookie::get('login_count'.$_SERVER['HTTP_HOST'])) {
Cookie::set('login_count'.$_SERVER['HTTP_HOST'],$count+1);
} else {
Cookie::set('login_count'.$_SERVER['HTTP_HOST'],1);
}
if ($count > $defaultCount) {
$this->validate($data,['captcha|验证码'=>'require|captcha']);
}
// 用户登录
$userModel = new UserModel();
$user = $userModel->login($data);
@ -78,13 +90,24 @@ class Passport extends BaseController
return $this->renderSuccess('登陆成功',$token);
} else {
return $this->renderError($user['msg']);
throw new ValidateException($user['msg']);
}
} catch (ValidateException $exception) {
return $this->renderError($exception->getMessage());
$data = [];
if ($count >= $defaultCount) $data['captcha_img'] = captcha_src();
return $this->renderError($exception->getMessage(),$data);
}
}
/**
* 变换验证码图片
* @return array
*/
public function changeCaptcha()
{
return $this->renderSuccess('数据返回成功',['captcha_img' => captcha_src()]);
}
/**
* 找回密码
* @return array

3
composer.json

@ -23,7 +23,8 @@
"php": ">=7.1.0",
"topthink/framework": "^6.0.0",
"topthink/think-orm": "^2.0",
"firebase/php-jwt": "^6.4"
"firebase/php-jwt": "^6.4",
"topthink/think-captcha": "^3.0"
},
"require-dev": {
"symfony/var-dumper": "^4.2",

55
composer.lock

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "4c92acd1c1151fbd83009333bdc40a38",
"content-hash": "b901b3966cb78e1368ca740fbe63c9c9",
"packages": [
{
"name": "firebase/php-jwt",
@ -513,6 +513,59 @@
},
"time": "2021-04-27T00:41:08+00:00"
},
{
"name": "topthink/think-captcha",
"version": "v3.0.9",
"source": {
"type": "git",
"url": "https://github.com/top-think/think-captcha.git",
"reference": "b1ef360670578214edeebcf824aaf6ab7ee0528b"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/top-think/think-captcha/zipball/b1ef360670578214edeebcf824aaf6ab7ee0528b",
"reference": "b1ef360670578214edeebcf824aaf6ab7ee0528b",
"shasum": ""
},
"require": {
"topthink/framework": "^6.0|^8.0"
},
"type": "library",
"extra": {
"think": {
"services": [
"think\\captcha\\CaptchaService"
],
"config": {
"captcha": "src/config.php"
}
}
},
"autoload": {
"files": [
"src/helper.php"
],
"psr-4": {
"think\\captcha\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"Apache-2.0"
],
"authors": [
{
"name": "yunwuxin",
"email": "448901948@qq.com"
}
],
"description": "captcha package for thinkphp",
"support": {
"issues": "https://github.com/top-think/think-captcha/issues",
"source": "https://github.com/top-think/think-captcha/tree/v3.0.9"
},
"time": "2023-04-27T07:18:40+00:00"
},
{
"name": "topthink/think-helper",
"version": "v3.1.6",

39
config/captcha.php

@ -0,0 +1,39 @@
<?php
// +----------------------------------------------------------------------
// | Captcha配置文件
// +----------------------------------------------------------------------
return [
//验证码位数
'length' => 5,
// 验证码字符集合
'codeSet' => '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY',
// 验证码过期时间
'expire' => 1800,
// 是否使用中文验证码
'useZh' => false,
// 是否使用算术验证码
'math' => false,
// 是否使用背景图
'useImgBg' => false,
//验证码字符大小
'fontSize' => 25,
// 是否使用混淆曲线
'useCurve' => true,
//是否添加杂点
'useNoise' => true,
// 验证码字体 不设置则随机
'fontttf' => '',
//背景颜色
'bg' => [243, 251, 254],
// 验证码图片高度
'imageH' => 0,
// 验证码图片宽度
'imageW' => 0,
// 添加额外的验证码设置
// verify => [
// 'length'=>4,
// ...
//],
];

1
route/app.php

@ -20,6 +20,7 @@ Route::group('passport',function (){
Route::post('register','passport/register');
Route::post('login','passport/login');
Route::post('retrieve','passport/retrieve');
Route::post('changeCaptcha','passport/changeCaptcha');
});
Route::group('user',function (){

Loading…
Cancel
Save