清理技术债务;增加后台登录成功事件;

This commit is contained in:
2023-09-09 13:43:29 +08:00
parent 1b0ae546c3
commit a2c14391b1
5 changed files with 55 additions and 23 deletions

View File

@@ -1,23 +1,20 @@
<?php <?php
namespace app\admin\controller; namespace app\admin\controller;
use app\admin\model\SystemAdmin; use app\admin\model\SystemAdmin;
use app\common\controller\AdminController; use app\common\controller\AdminController;
use think\captcha\facade\Captcha; use think\captcha\facade\Captcha;
use think\facade\Env; use think\facade\Env;
use think\facade\Event;
/** /**
* Class Login * Class Login.
* @package app\admin\controller
*/ */
class Login extends AdminController class Login extends AdminController
{ {
/** /**
* 初始化方法 * 初始化方法.
*/ */
public function initialize() public function initialize()
{ {
@@ -30,7 +27,7 @@ class Login extends AdminController
} }
/** /**
* 用户登录 * 用户登录.
* @return string * @return string
* @throws \Exception * @throws \Exception
*/ */
@@ -40,8 +37,8 @@ class Login extends AdminController
if ($this->request->isPost()) { if ($this->request->isPost()) {
$post = $this->request->post(); $post = $this->request->post();
$rule = [ $rule = [
'username|用户名' => 'require', 'username|用户名' => 'require',
'password|密码' => 'require', 'password|密码' => 'require',
'keep_login|是否保持登录' => 'require', 'keep_login|是否保持登录' => 'require',
]; ];
$captcha == 1 && $rule['captcha|验证码'] = 'require|captcha'; $captcha == 1 && $rule['captcha|验证码'] = 'require|captcha';
@@ -58,19 +55,24 @@ class Login extends AdminController
} }
$admin->login_num += 1; $admin->login_num += 1;
$admin->save(); $admin->save();
Event::trigger('AdminLoginSuccess', $admin);
$admin = $admin->toArray(); $admin = $admin->toArray();
unset($admin['password']); unset($admin['password']);
$admin['expire_time'] = $post['keep_login'] == 1 ? true : time() + 7200; $admin['expire_time'] = $post['keep_login'] == 1 ? true : time() + 7200;
session('admin', $admin); session('admin', $admin);
$this->success('登录成功'); $this->success('登录成功');
} }
$this->assign('captcha', $captcha); $this->assign('captcha', $captcha);
$this->assign('demo', $this->isDemo); $this->assign('demo', $this->isDemo);
return $this->fetch(); return $this->fetch();
} }
/** /**
* 用户退出 * 用户退出.
* @return mixed * @return mixed
*/ */
public function out() public function out()

View File

@@ -1,17 +1,14 @@
<?php <?php
// 事件定义文件 // 事件定义文件
return [ return [
'bind' => [ 'bind' => [
], ],
'listen' => [ 'listen' => [
'AppInit' => [ 'AppInit' => [],
\app\admin\listener\ViewInitListener::class, 'HttpRun' => [],
], 'HttpEnd' => [],
'HttpRun' => [
\app\admin\listener\ViewInitListener::class,
],
'HttpEnd' => [],
'LogLevel' => [], 'LogLevel' => [],
'LogWrite' => [], 'LogWrite' => [],
], ],

View File

@@ -0,0 +1,15 @@
<?php
namespace app\common\event\AdminLoginSuccess;
use app\admin\model\SystemAdmin;
use think\facade\Log;
class LogEvent
{
public function handle(SystemAdmin $system_admin)
{
// 事件监听处理
Log::report("admin login success,{$system_admin->username}");
}
}

View File

@@ -1,17 +1,30 @@
<?php <?php
// 事件定义文件 // 事件定义文件
return [
'bind' => [ use app\common\event\AdminLoginSuccess\LogEvent;
$event = [
'bind' => [
], ],
'listen' => [ 'listen' => [
'AppInit' => [], 'AppInit' => [],
'HttpRun' => [], 'HttpRun' => [],
'HttpEnd' => [], 'HttpEnd' => [],
'LogLevel' => [], 'LogLevel' => [],
'LogWrite' => [], 'LogWrite' => [],
'AdminLoginSuccess' => [
LogEvent::class,
],
], ],
'subscribe' => [ 'subscribe' => [
], ],
]; ];
$listen = include __DIR__ . '/listen.php';
$event['listen'] = array_merge($event['listen'], $listen);
return $event;

5
app/listen.php Normal file
View File

@@ -0,0 +1,5 @@
<?php
return [
];