From 15be66ec2612968b6ef1744120073e5b15351944 Mon Sep 17 00:00:00 2001 From: wuyanwen Date: Thu, 12 Dec 2019 22:33:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=B3=BB=E7=BB=9F=E7=AE=A1?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- catchAdmin/login/LoginLogListener.php | 79 +++++++++++++++++++ catchAdmin/permissions/OperateLogListener.php | 7 ++ catchAdmin/system/controller/LoginLog.php | 19 +++++ catchAdmin/system/controller/OperateLog.php | 25 ++++++ .../migrations/20191212110921_login_log.php | 40 ++++++++++ .../migrations/20191212110930_operate_log.php | 40 ++++++++++ catchAdmin/system/event/LoginLogEvent.php | 12 +++ catchAdmin/system/event/OperateLogEvent.php | 12 +++ catchAdmin/system/module.json | 11 +++ catchAdmin/system/route.php | 8 ++ catchAdmin/system/view/loginLog/index.html | 57 +++++++++++++ catchAdmin/system/view/operateLog/index.html | 53 +++++++++++++ 12 files changed, 363 insertions(+) create mode 100644 catchAdmin/login/LoginLogListener.php create mode 100644 catchAdmin/permissions/OperateLogListener.php create mode 100644 catchAdmin/system/controller/LoginLog.php create mode 100644 catchAdmin/system/controller/OperateLog.php create mode 100644 catchAdmin/system/database/migrations/20191212110921_login_log.php create mode 100644 catchAdmin/system/database/migrations/20191212110930_operate_log.php create mode 100644 catchAdmin/system/event/LoginLogEvent.php create mode 100644 catchAdmin/system/event/OperateLogEvent.php create mode 100644 catchAdmin/system/module.json create mode 100644 catchAdmin/system/route.php create mode 100644 catchAdmin/system/view/loginLog/index.html create mode 100644 catchAdmin/system/view/operateLog/index.html diff --git a/catchAdmin/login/LoginLogListener.php b/catchAdmin/login/LoginLogListener.php new file mode 100644 index 0000000..bf2b5be --- /dev/null +++ b/catchAdmin/login/LoginLogListener.php @@ -0,0 +1,79 @@ +params = $params; + } + + public function handle() + { + dd('ad'); + $agent = request()->header('user-agent'); + + $username = Users::where('email', $this->params['email'])->value('username'); + + Db::name('login_log')->insert([ + 'login_name' => $username ? : $this->params['email'], + 'login_ip' => ip2long(request()->ip()), + 'browser' => $this->getBrowser($agent), + 'os' => $this->getOs($agent), + 'login_at' => time(), + 'status' => $this->params['success'] ? 1 : 2, + ]); + } + + + private function getOs($agent) + { + if (false !== stripos($agent, 'win') && preg_match('/nt 6.1/i', $agent)) { + return 'Windows 7'; + } + if (false !== stripos($agent, 'win') && preg_match('/nt 6.2/i', $agent)) { + return 'Windows 8'; + } + if(false !== stripos($agent, 'win') && preg_match('/nt 10.0/i', $agent)) { + return 'Windows 10';#添加win10判断 + } + if (false !== stripos($agent, 'win') && preg_match('/nt 5.1/i', $agent)) { + return 'Windows XP'; + } + if (false !== stripos($agent, 'linux')) { + return 'Linux'; + } + if (false !== stripos($agent, 'mac')) { + return 'mac'; + } + + return '未知'; + } + + private function getBrowser($agent) + { + if (false !== stripos($agent, "MSIE")) { + return 'MSIE'; + } + if (false !== stripos($agent, "Firefox")) { + return 'Firefox'; + } + if (false !== stripos($agent, "Chrome")) { + return 'Chrome'; + } + if (false !== stripos($agent, "Safari")) { + return 'Safari'; + } + if (false !== stripos($agent, "Opera")) { + return 'Opera'; + } + + return '未知'; + } +} diff --git a/catchAdmin/permissions/OperateLogListener.php b/catchAdmin/permissions/OperateLogListener.php new file mode 100644 index 0000000..053457d --- /dev/null +++ b/catchAdmin/permissions/OperateLogListener.php @@ -0,0 +1,7 @@ +fetch(); + } + + public function list() + { + return CatchResponse::paginate(Db::name('login_log')->paginate(10)); + } +} diff --git a/catchAdmin/system/controller/OperateLog.php b/catchAdmin/system/controller/OperateLog.php new file mode 100644 index 0000000..b24f465 --- /dev/null +++ b/catchAdmin/system/controller/OperateLog.php @@ -0,0 +1,25 @@ +fetch(); + } + + public function list() + { + return CatchResponse::paginate( + Db::name('operate_log') + ->field(['operate_log.*', 'users.username as creator']) + ->join('users','users.id = operate_log.creator_id') + ->order('id', 'desc') + ->paginate(10) + ); + } +} diff --git a/catchAdmin/system/database/migrations/20191212110921_login_log.php b/catchAdmin/system/database/migrations/20191212110921_login_log.php new file mode 100644 index 0000000..45965d0 --- /dev/null +++ b/catchAdmin/system/database/migrations/20191212110921_login_log.php @@ -0,0 +1,40 @@ +table('login_log',['engine'=>'Myisam', 'comment' => '登录日志', 'signed' => false]); + $table->addColumn('login_name', 'string',['limit' => 50,'default'=>'','comment'=>'用户名']) + ->addColumn('login_ip', 'string',['default'=>0, 'limit' => 20, 'comment'=>'登录地点ip', 'signed' => false]) + ->addColumn('browser', 'string',['default'=> '','comment'=>'浏览器']) + ->addColumn('os', 'string',['default'=> '','comment'=>'操作系统']) + ->addColumn('login_at', 'integer', array('default'=>0,'comment'=>'登录时间', 'signed' => false )) + ->addColumn('status', 'integer',['limit' => \Phinx\Db\Adapter\MysqlAdapter::INT_TINY,'default'=> 1,'comment'=>'1 成功 2 失败']) + ->create(); + } +} diff --git a/catchAdmin/system/database/migrations/20191212110930_operate_log.php b/catchAdmin/system/database/migrations/20191212110930_operate_log.php new file mode 100644 index 0000000..4e75d7e --- /dev/null +++ b/catchAdmin/system/database/migrations/20191212110930_operate_log.php @@ -0,0 +1,40 @@ +table('operate_log',['engine'=>'Myisam', 'comment' => '操作日志', 'signed' => false]); + $table->addColumn('module', 'string',['limit' => 50,'default'=>'','comment'=>'模块名称']) + ->addColumn('ip', 'string',['default'=>'', 'limit' => 20,'comment'=>'ip', 'signed' => false]) + ->addColumn('operate', 'string',['default'=> '','comment'=>'操作模块']) + ->addColumn('creator_id', 'integer',['default'=> 0,'comment'=>'创建人ID', 'signed' => false]) + ->addColumn('method', 'string',['default'=> '','comment'=>'请求方法']) + ->addColumn('created_at', 'integer', array('default'=>0,'comment'=>'登录时间', 'signed' => false )) + ->create(); + } +} diff --git a/catchAdmin/system/event/LoginLogEvent.php b/catchAdmin/system/event/LoginLogEvent.php new file mode 100644 index 0000000..9d33a1c --- /dev/null +++ b/catchAdmin/system/event/LoginLogEvent.php @@ -0,0 +1,12 @@ +params = $params; + } +} \ No newline at end of file diff --git a/catchAdmin/system/event/OperateLogEvent.php b/catchAdmin/system/event/OperateLogEvent.php new file mode 100644 index 0000000..cece388 --- /dev/null +++ b/catchAdmin/system/event/OperateLogEvent.php @@ -0,0 +1,12 @@ +params = $params; + } +} \ No newline at end of file diff --git a/catchAdmin/system/module.json b/catchAdmin/system/module.json new file mode 100644 index 0000000..e5d44e9 --- /dev/null +++ b/catchAdmin/system/module.json @@ -0,0 +1,11 @@ +{ + "name": "系统管理", + "alias": "system", + "description": "", + "keywords": [], + "order": 2, + "services": [], + "aliases": {}, + "files": [], + "requires": [] +} \ No newline at end of file diff --git a/catchAdmin/system/route.php b/catchAdmin/system/route.php new file mode 100644 index 0000000..d81abc8 --- /dev/null +++ b/catchAdmin/system/route.php @@ -0,0 +1,8 @@ +get('log/login', '\catchAdmin\system\controller\LoginLog@list'); +$router->get('loginLog/index', '\catchAdmin\system\controller\LoginLog@index'); +// 操作日志 +$router->get('log/operate', '\catchAdmin\system\controller\OperateLog@list'); +$router->get('operateLog/index', '\catchAdmin\system\controller\OperateLog@index'); + diff --git a/catchAdmin/system/view/loginLog/index.html b/catchAdmin/system/view/loginLog/index.html new file mode 100644 index 0000000..17d9a45 --- /dev/null +++ b/catchAdmin/system/view/loginLog/index.html @@ -0,0 +1,57 @@ +{extend name="../../../view/layout"} +{block name="title"}登录日志{/block} +{block name="search"} +
+
+ + + +
+
+{/block} +{block name="table"} +
+{/block} +{block name="script"} + +{/block} \ No newline at end of file diff --git a/catchAdmin/system/view/operateLog/index.html b/catchAdmin/system/view/operateLog/index.html new file mode 100644 index 0000000..57b5cad --- /dev/null +++ b/catchAdmin/system/view/operateLog/index.html @@ -0,0 +1,53 @@ +{extend name="../../../view/layout"} +{block name="title"}操作日志{/block} +{block name="search"} +
+
+ + + +
+
+{/block} +{block name="table"} +
+{/block} +{block name="script"} + +{/block} \ No newline at end of file