Skip to content

Commit cab05c2

Browse files
committed
增加日志中间件,增加数据库更新工具
1 parent 5d724e9 commit cab05c2

9 files changed

Lines changed: 292 additions & 3043 deletions

File tree

src/Controller/Logs.php

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* @contact 4213509@qq.com
1010
* @license https://github.com/hyperf-plus/admin/blob/master/LICENSE
1111
*/
12+
1213
namespace HPlus\Admin\Controller;
1314

1415
use HPlus\Admin\Model\Admin\OperationLog;
@@ -19,8 +20,12 @@
1920
use HPlus\UI\Components\Grid\Avatar;
2021
use HPlus\UI\Components\Grid\Route;
2122
use HPlus\UI\Components\Grid\Tag;
23+
use HPlus\UI\Components\Widgets\Button;
24+
use HPlus\UI\Components\Widgets\Dialog;
25+
use HPlus\UI\Components\Widgets\Markdown;
2226
use HPlus\UI\Form;
2327
use HPlus\UI\Grid;
28+
use HPlus\UI\Layout\Content;
2429
use Hyperf\Database\Model\Model;
2530

2631
/**
@@ -38,24 +43,64 @@ protected function grid()
3843
->emptyText('暂无日志')
3944
->height('auto')
4045
->appendFields(['user.id']);
46+
$grid->pageSizes([10, 20]);
4147
$grid->column('id', 'ID')->width('100');
4248
$grid->column('user.avatar', '头像', 'user_id')->component(Avatar::make()->size('small'))->width(80);
43-
$grid->column('user.name', '用户', 'user_id')->help('操作用户')->sortable()->component(Route::make('/admin/logs/list?user_id={user.id}')->type('primary'));
44-
$grid->column('method', '请求方式')->width(100)->align('center')->component(Tag::make()->type(['GET' => 'info', 'POST' => 'success']));
49+
$grid->column('user.name', '用户', 'user_id')->width(80)->help('操作用户')->sortable()->component(Route::make('/admin/logs/list?user_id={user.id}')->type('primary'));
50+
$grid->column('method', '请求方式')->width(80)->align('center')->component(Tag::make()->type(['GET' => 'info', 'POST' => 'success']));
4551
$grid->column('path', '路径')->help('操作URL')->sortable();
46-
$grid->column('runtime', '执行时间')->help('毫秒');
47-
$grid->column('ip', 'IP')->component(Route::make('/admin/logs/list?ip={ip}')->type('primary'));
52+
$grid->column('runtime', '执行时间')->help('毫秒')->width(80);
53+
$grid->column('ip', 'IP')->component(Route::make('/admin/logs/list?ip={ip}')->type('primary'))->width(100);
4854
$grid->column('created_at', '创建时间')->sortable();
49-
5055
$grid->actions(function (Grid\Actions $actions) {
56+
$row = $actions->getRow();
57+
$action = new Grid\Actions\ActionButton('请求头');
58+
$action->order(5);
59+
$action->dialog(function (Dialog $dialog) use ($row) {
60+
$dialog->title('查看请求头信息');
61+
$dialog->slot(function (Content $content) use ($row) {
62+
$code = "```json\n";
63+
$code .= json_encode($row->header, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
64+
$code .= "\n```";
65+
$content->body(Markdown::make($code)->style("height:60vh;"));
66+
});
67+
});
68+
$actions->add($action);
69+
unset($action);
70+
$action = new Grid\Actions\ActionButton('请求值');
71+
$action->order(5);
72+
$action->dialog(function (Dialog $dialog) use ($row) {
73+
$dialog->title('查看提交参数信息');
74+
$dialog->slot(function (Content $content) use ($row) {
75+
$code = "```json\n";
76+
$code .= json_encode($row->request, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
77+
$code .= "\n```";
78+
$content->body(Markdown::make($code)->style("height:60vh;"));
79+
});
80+
});
81+
$actions->add($action);
82+
unset($action);
83+
84+
$action = new Grid\Actions\ActionButton('响应结果');
85+
$action->order(4);
86+
$action->dialog(function (Dialog $dialog) use ($row) {
87+
$dialog->title('查看响应结果');
88+
$dialog->slot(function (Content $content) use ($row) {
89+
$code = "```json\n";
90+
$code .= json_encode($row->result ,JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
91+
$code .= "\n```";
92+
$content->body(Markdown::make($code)->style("height:60vh;"));
93+
});
94+
});
95+
$actions->add($action);
5196
$actions->hideEditAction();
5297
$actions->hideViewAction();
5398
})->toolbars(function (Grid\Toolbars $toolbars) {
5499
$toolbars->hideCreateButton();
55100
});
56101

57102
$grid->filter(function (Grid\Filter $filter) {
58-
$user_id = (int) request('user_id');
103+
$user_id = (int)request('user_id');
59104
$filter->equal('user_id')->component(Select::make($user_id)->placeholder('请选择用户')->options(function () {
60105
$user_ids = OperationLog::query()->groupBy('user_id')->get(['user_id'])->pluck('user_id')->toArray();
61106
/*@var Model $userModel */

src/Install/install.sql

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,21 @@ INSERT INTO `admin_menu` (`id`, `parent_id`, `is_menu`, `order`, `title`, `icon`
4444
--
4545

4646
CREATE TABLE `admin_operation_log` (
47-
`id` int(10) UNSIGNED NOT NULL,
47+
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
4848
`user_id` int(11) NOT NULL,
4949
`path` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
5050
`method` varchar(10) COLLATE utf8mb4_unicode_ci NOT NULL,
5151
`ip` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
52-
`input` text COLLATE utf8mb4_unicode_ci NOT NULL,
52+
`request` json NOT NULL,
53+
`result` json NOT NULL,
54+
`header` json NOT NULL,
5355
`runtime` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
5456
`created_at` timestamp NULL DEFAULT NULL,
55-
`updated_at` timestamp NULL DEFAULT NULL
56-
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
57-
57+
`updated_at` timestamp NULL DEFAULT NULL,
58+
PRIMARY KEY (`id`) USING BTREE,
59+
KEY `admin_operation_log_user_id_index` (`user_id`)
60+
) ENGINE=InnoDB AUTO_INCREMENT=160 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
5861
-- --------------------------------------------------------
59-
6062
--
6163
-- Table structure for table `admin_permissions`
6264
--

src/Install/update.sql

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/Install/updateCommand.php

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/Middleware/LogsMiddleware.php

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.plus
6+
*
7+
* @link https://www.hyperf.plus
8+
* @document https://doc.hyperf.plus
9+
* @contact 4213509@qq.com
10+
* @license https://github.com/hyperf-plus/admin/blob/master/LICENSE
11+
*/
12+
13+
namespace HPlus\Admin\Middleware;
14+
15+
use HPlus\Admin\Facades\Admin;
16+
use HPlus\Admin\Model\Admin\OperationLog;
17+
use HPlus\Helper\RunTime;
18+
use Hyperf\Contract\ConfigInterface;
19+
use Hyperf\Utils\Str;
20+
use Psr\Http\Message\ResponseInterface;
21+
use Psr\Http\Message\ServerRequestInterface;
22+
use Psr\Http\Server\MiddlewareInterface;
23+
use Psr\Http\Server\RequestHandlerInterface;
24+
use Qbhy\HyperfAuth\Authenticatable;
25+
use Qbhy\HyperfAuth\AuthGuard;
26+
use Qbhy\HyperfAuth\AuthManager;
27+
use Qbhy\HyperfAuth\Exception\UnauthorizedException;
28+
29+
/**
30+
* Class AuthMiddleware.
31+
*/
32+
class LogsMiddleware implements MiddlewareInterface
33+
{
34+
protected $config;
35+
36+
public function __construct(AuthManager $auth, ConfigInterface $config)
37+
{
38+
$this->config = $config->get('admin.operation_log');
39+
}
40+
41+
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
42+
{
43+
if (!$this->config['enable']
44+
|| !$this->inAllowedMethods($request->getMethod())
45+
|| $this->inExceptArray($request)
46+
) {
47+
return $handler->handle($request);
48+
}
49+
$RunTime = new RunTime;
50+
$response = $handler->handle($request);
51+
$time = $RunTime->spent();
52+
try {
53+
OperationLog::create([
54+
'user_id' => Admin::user()->getId(),
55+
'path' => substr($request->getUri()->getPath(), 0, 255),
56+
'method' => $request->getMethod(),
57+
'ip' => get_client_ip(),
58+
'runtime' => $time,
59+
'header' => $request->getHeaders(),
60+
'request' => array_merge($request->getQueryParams(), (array)$request->getParsedBody()),
61+
'result' => json_decode($response->getBody()->getContents(), true),
62+
]);
63+
} catch (\Throwable $exception) {
64+
// pass
65+
Logger()->info("可能您的log日志表不是最新的, 请执行 php bin/hyperf.php admin:db -l 查看升级命令");
66+
}
67+
return $response;
68+
}
69+
70+
/**
71+
* Whether requests using this method are allowed to be logged.
72+
*
73+
* @param string $method
74+
*
75+
* @return bool
76+
*/
77+
protected function inAllowedMethods($method)
78+
{
79+
$allowedMethods = collect($this->config['allowed_methods'])->filter();
80+
if ($allowedMethods->isEmpty()) {
81+
return true;
82+
}
83+
return $allowedMethods->map(function ($method) {
84+
return strtoupper($method);
85+
})->contains($method);
86+
}
87+
88+
/**
89+
* Determine if the request has a URI that should pass through CSRF verification.
90+
*
91+
* @param ServerRequestInterface $request
92+
*
93+
* @return bool
94+
*/
95+
protected function inExceptArray($request)
96+
{
97+
foreach ($this->config['except'] as $except) {
98+
if ($except !== '/') {
99+
$except = trim($except, '/');
100+
}
101+
$methods = [];
102+
if (Str::contains($except, ':')) {
103+
list($methods, $except) = explode(':', $except);
104+
$methods = explode(',', $methods);
105+
}
106+
$methods = array_map('strtoupper', $methods);
107+
if ($this->is($except) &&
108+
(empty($methods) || in_array($request->getMethod(), $methods))) {
109+
return true;
110+
}
111+
}
112+
return false;
113+
}
114+
115+
public function is(...$except)
116+
{
117+
$path = request()->path();
118+
foreach ($except as $pattern) {
119+
if (Str::is($pattern, rawurldecode($path))) {
120+
return true;
121+
}
122+
}
123+
return false;
124+
}
125+
}

src/Model/Admin/OperationLog.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* @contact 4213509@qq.com
1010
* @license https://github.com/hyperf-plus/admin/blob/master/LICENSE
1111
*/
12+
1213
namespace HPlus\Admin\Model\Admin;
1314

1415
use HPlus\Admin\Model\Model;
@@ -28,11 +29,14 @@ class OperationLog extends Model
2829
'LINK', 'UNLINK', 'COPY', 'HEAD', 'PURGE',
2930
];
3031

31-
protected $fillable = ['user_id', 'path', 'runtime', 'method', 'ip', 'input'];
32+
protected $fillable = ['user_id', 'path', 'runtime', 'method', 'header', 'result', 'ip', 'request'];
3233

3334
protected $casts = [
3435
'created_at' => 'Y-m-d H:i:s',
3536
'updated_at' => 'Y-m-d H:i:s',
37+
'result' => 'json',
38+
'header' => 'json',
39+
'request' => 'json',
3640
];
3741

3842
/**

src/Update/db/20210107.sql

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#升级日志记录表 会删除admin_operation_log表如果已经对admin_operation_log进行二开 请不要执行此命令#
2+
DROP TABLE IF EXISTS `admin_operation_log`;
3+
CREATE TABLE `admin_operation_log` (
4+
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
5+
`user_id` int(11) NOT NULL,
6+
`path` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
7+
`method` varchar(10) COLLATE utf8mb4_unicode_ci NOT NULL,
8+
`ip` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
9+
`request` json NOT NULL,
10+
`result` json NOT NULL,
11+
`header` json NOT NULL,
12+
`runtime` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
13+
`created_at` timestamp NULL DEFAULT NULL,
14+
`updated_at` timestamp NULL DEFAULT NULL,
15+
PRIMARY KEY (`id`) USING BTREE,
16+
KEY `admin_operation_log_user_id_index` (`user_id`)
17+
) ENGINE=InnoDB AUTO_INCREMENT=160 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

0 commit comments

Comments
 (0)