refactor: 修改登录接口
This commit is contained in:
@@ -3,6 +3,9 @@ declare (strict_types = 1);
|
|||||||
|
|
||||||
namespace app\admin\controller\v1;
|
namespace app\admin\controller\v1;
|
||||||
|
|
||||||
|
use apiret\Ret;
|
||||||
|
use app\admin\exception\InvalidLoginException;
|
||||||
|
use app\admin\model\v1\UserLoginLogModel;
|
||||||
use app\admin\model\v1\UserModel;
|
use app\admin\model\v1\UserModel;
|
||||||
use app\admin\validate\v1\LoginValidate;
|
use app\admin\validate\v1\LoginValidate;
|
||||||
use thans\jwt\facade\JWTAuth;
|
use thans\jwt\facade\JWTAuth;
|
||||||
@@ -23,38 +26,57 @@ class Login
|
|||||||
'captcha'
|
'captcha'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// 验证参数
|
$user = new UserModel();
|
||||||
$validate = new LoginValidate();
|
$msg = '';
|
||||||
if (!$validate->check($post)) {
|
try {
|
||||||
return error($validate->getError());
|
// 验证参数
|
||||||
}
|
$validate = new LoginValidate();
|
||||||
|
if (!$validate->check($post)) {
|
||||||
|
throw new InvalidLoginException($validate->getError());
|
||||||
|
}
|
||||||
|
|
||||||
// 校验验证码
|
// 校验验证码
|
||||||
$code = Cache::get('captcha:token.' . $post['token']);
|
$code = Cache::get('captcha:token.' . $post['token']);
|
||||||
if (!$code) {
|
if (!$code) {
|
||||||
return error('验证码不存在或已过期!');
|
throw new InvalidLoginException('验证码不存在或已过期!');
|
||||||
}
|
}
|
||||||
Cache::delete('captcha:token.' . $post['token']);
|
Cache::delete('captcha:token.' . $post['token']);
|
||||||
|
|
||||||
// 校验
|
// 校验
|
||||||
if (!password_verify($post['captcha'], $code)) {
|
if (!password_verify($post['captcha'], $code)) {
|
||||||
return error('验证码错误!');
|
throw new InvalidLoginException('验证码错误!');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 验证用户
|
// 验证用户
|
||||||
$user = UserModel::usernameOrMobile($post['username'])->find();
|
$user = UserModel::usernameOrMobile($post['username'])->find();
|
||||||
if (!$user) {
|
if (!$user) {
|
||||||
return error('用户不存在!');
|
throw new InvalidLoginException('用户不存在!');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 验证密码
|
// 验证密码
|
||||||
if ($user['password'] != password_with_salt($post['password'], $user['salt'])) {
|
if ($user['password'] != password_with_salt($post['password'], $user['salt'])) {
|
||||||
return error('密码错误!');
|
throw new InvalidLoginException('密码错误!');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 验证用户状态
|
// 验证用户状态
|
||||||
if ($user['status'] == -1) {
|
if ($user['status'] == -1) {
|
||||||
return error('用户已禁用,请联系管理员!');
|
throw new InvalidLoginException('用户已禁用,请联系管理员!');
|
||||||
|
}
|
||||||
|
} catch (InvalidLoginException $e) {
|
||||||
|
$msg = $e->getMessage();
|
||||||
|
return error($msg);
|
||||||
|
} catch (\Throwable $th) {
|
||||||
|
$msg = $th->getMessage();
|
||||||
|
return error('登录失败!');
|
||||||
|
} finally {
|
||||||
|
// 记录登录日志
|
||||||
|
UserLoginLogModel::create([
|
||||||
|
'user_id' => $user['id'],
|
||||||
|
'ip' => ip2long(request()->ip()),
|
||||||
|
'user_agent' => request()->header('user-agent'),
|
||||||
|
'message' => $msg,
|
||||||
|
'status' => !$msg ? 1 : -1,
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 生成 jwt token
|
// 生成 jwt token
|
||||||
|
|||||||
10
app/admin/exception/InvalidLoginException.php
Normal file
10
app/admin/exception/InvalidLoginException.php
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
namespace app\admin\exception;
|
||||||
|
|
||||||
|
class InvalidLoginException extends \Exception
|
||||||
|
{
|
||||||
|
// public function __construct($message = "", $code = 0, \Throwable $previous = null)
|
||||||
|
// {
|
||||||
|
// parent::__construct($message, $code, $previous);
|
||||||
|
// }
|
||||||
|
}
|
||||||
29
app/admin/model/v1/UserLoginLogModel.php
Normal file
29
app/admin/model/v1/UserLoginLogModel.php
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
|
namespace app\admin\model\v1;
|
||||||
|
|
||||||
|
use think\Model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @mixin \think\Model
|
||||||
|
*/
|
||||||
|
class UserLoginLogModel extends Model
|
||||||
|
{
|
||||||
|
// 设置表名
|
||||||
|
protected $name = 'sys_user_login_log';
|
||||||
|
|
||||||
|
// 设置主键
|
||||||
|
protected $pk = 'id';
|
||||||
|
|
||||||
|
// 设置字段信息
|
||||||
|
protected $schema = [
|
||||||
|
'id' => 'int',
|
||||||
|
'user_id' => 'int',
|
||||||
|
'ip' => 'string',
|
||||||
|
'user_agent' => 'string',
|
||||||
|
'message' => 'string',
|
||||||
|
'status' => 'int',
|
||||||
|
'created_at' => 'datetime',
|
||||||
|
];
|
||||||
|
}
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
// 接口错误返回
|
// 接口错误返回
|
||||||
if (!function_exists('error')) {
|
if (!function_exists('error')) {
|
||||||
function error($msg = '', $data = [])
|
function error($msg = '', $data = []): \think\Response
|
||||||
{
|
{
|
||||||
return \apiret\Api::error($msg, $data);
|
return \apiret\Api::error($msg, $data);
|
||||||
}
|
}
|
||||||
@@ -12,7 +12,7 @@ if (!function_exists('error')) {
|
|||||||
|
|
||||||
// 接口成功返回
|
// 接口成功返回
|
||||||
if (!function_exists('success')) {
|
if (!function_exists('success')) {
|
||||||
function success($msg = '', $data = [])
|
function success($msg = '', $data = []): \think\Response
|
||||||
{
|
{
|
||||||
return \apiret\Api::success($msg, $data);
|
return \apiret\Api::success($msg, $data);
|
||||||
}
|
}
|
||||||
@@ -20,7 +20,7 @@ if (!function_exists('success')) {
|
|||||||
|
|
||||||
// 接口调结果返回
|
// 接口调结果返回
|
||||||
if (!function_exists('result')) {
|
if (!function_exists('result')) {
|
||||||
function result($errno, $msg = '', $data = [])
|
function result($errno, $msg = '', $data = []): \think\Response
|
||||||
{
|
{
|
||||||
return \apiret\Api::result($errno)->message($msg)->response($data);
|
return \apiret\Api::result($errno)->message($msg)->response($data);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user