fix: 修复登录缓存BUG

This commit is contained in:
Ying
2022-08-22 19:07:32 +08:00
parent 6e1b38a34b
commit 257cb95123
9 changed files with 74 additions and 57 deletions

View File

@@ -5,6 +5,11 @@ namespace app\admin\controller;
use app\AdminController;
use app\common\model\system\Admin;
use app\common\model\system\LoginLog;
use Psr\SimpleCache\InvalidArgumentException;
use support\Response;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use Webman\Event\Event;
use Webman\Http\Request;
@@ -12,8 +17,7 @@ class Login extends AdminController
{
/**
* 初始化方法
* @param Request $request
* @return \support\Response|void
* @return void
* @throws \Exception
*/
public function __construct()
@@ -25,15 +29,16 @@ class Login extends AdminController
/**
* 登录函数
* @return \support\Response
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return Response
* @throws InvalidArgumentException
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function index(): \support\Response
{
// 禁止重复访问
if (isset($this->admin['id'])) {
if (isset(request()->adminInfo['id'])) {
return $this->redirect('/admin/index');
}
@@ -42,10 +47,10 @@ class Login extends AdminController
$user = request()->post('name');
$pwd = request()->post('pwd');
$captcha = request()->post('captcha');
if ((isset($this->admin['count'])
&& $this->admin['count'] >= 5)
&& (isset($this->admin['time'])
&& $this->admin['time'] >= strtotime('- 5 minutes'))
if ((isset(request()->adminInfo['count'])
&& request()->adminInfo['count'] >= 5)
&& (isset(request()->adminInfo['time'])
&& request()->adminInfo['time'] >= strtotime('- 5 minutes'))
) {
$error = '错误次数过多,请稍后再试!';
$this->writeLoginLogs($error);
@@ -53,7 +58,7 @@ class Login extends AdminController
}
// 验证码
if (isset($this->admin['isCaptcha'])) {
if (isset(request()->adminInfo['isCaptcha'])) {
if (!$captcha || !$this->captchaCheck($captcha)) {
$error = '验证码错误!';
$this->writeLoginLogs($error);
@@ -70,10 +75,10 @@ class Login extends AdminController
$result = Admin::checkLogin($user, $pwd);
if (empty($result)) {
$this->admin['time'] = time();
$this->admin['isCaptcha'] = true;
$this->admin['count'] = isset($this->admin['count']) ? $this->admin['count'] + 1 : 1;
\request()->session()->set($this->sename, $this->admin);
request()->adminInfo['time'] = time();
request()->adminInfo['isCaptcha'] = true;
request()->adminInfo['count'] = isset(request()->adminInfo['count']) ? request()->adminInfo['count'] + 1 : 1;
request()->session()->set(AdminSession, request()->adminInfo);
$error = '用户名或密码错误!';
$this->writeLoginLogs($error);
Event::emit('adminLoginError', \request()->all());
@@ -93,7 +98,8 @@ class Login extends AdminController
try {
$result->save();
request()->session()->set($this->sename, $result->toArray());
$session = array_merge(request()->adminInfo, $result->toArray());
request()->session()->set(AdminSession, $session);
} catch (\Throwable $th) {
return $this->error($th->getMessage());
}
@@ -106,7 +112,7 @@ class Login extends AdminController
}
return view('login/index', [
'captcha' => $this->admin['isCaptcha'] ?? false,
'captcha' => request()->adminInfo['isCaptcha'] ?? false,
]);
}

View File

@@ -19,6 +19,10 @@ use app\common\model\system\Department;
use app\common\model\system\Admin as AdminModel;
use app\common\model\system\AdminGroup as AdminGroupModel;
use app\common\model\system\AdminAccess as AdminAccessModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use Webman\Http\Request;
/**
* 管理员管理
@@ -353,14 +357,18 @@ class Admin extends AdminController
/**
* 个人中心
* @param Request $request
* @return mixed
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function center(): \support\Response
public function center(Request $request): \support\Response
{
if (request()->isPost()) {
$post = request()->post();
$post['id'] = $this->admin['id'];
$post['id'] = $request->adminId;
if ($this->model->update($post)) {
return $this->success();
}
@@ -369,7 +377,7 @@ class Admin extends AdminController
}
$title = [];
$data = $this->model->find($this->admin['id']);
$data = $this->model->find($request->adminId);
if (!empty($data['group_id'])) {
$group = AdminGroupModel::field('title')
->whereIn('id', $data['group_id'])
@@ -390,11 +398,11 @@ class Admin extends AdminController
/**
* 修改个人资料
*/
public function modify()
public function modify(Request $request)
{
if (request()->isAjax()) {
$post = request()->post();
$id = $this->admin['id'];
$id = $request->adminId;
try {
//code...
switch ($post['field']) {
@@ -445,8 +453,11 @@ class Admin extends AdminController
/**
* 修改密码
* @return mixed
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function pwd(): \support\Response
public function pwd(Request $request): \support\Response
{
if (request()->isPost()) {
@@ -457,7 +468,7 @@ class Admin extends AdminController
}
// 查找数据
$where[] = ['id', '=', $this->admin['id']];
$where[] = ['id', '=', $request->admin_id];
$where[] = ['pwd', '=', encryptPwd($pwd)];
$result = $this->model->where($where)->find();