SystemLog.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace app\admin\middleware;
  3. use app\admin\service\annotation\ControllerAnnotation;
  4. use app\admin\service\annotation\MiddlewareAnnotation;
  5. use app\admin\service\annotation\NodeAnnotation;
  6. use app\admin\service\SystemLogService;
  7. use app\common\traits\JumpTrait;
  8. use app\Request;
  9. use Closure;
  10. use ReflectionException;
  11. class SystemLog
  12. {
  13. use JumpTrait;
  14. /**
  15. * 敏感信息字段,日志记录时需要加密
  16. * @var array
  17. */
  18. protected array $sensitiveParams = [
  19. 'password',
  20. 'password_again',
  21. 'phone',
  22. 'mobile',
  23. ];
  24. /**
  25. * @throws ReflectionException
  26. */
  27. public function handle(Request $request, Closure $next)
  28. {
  29. $response = $next($request);
  30. if (!env('APP_ADMIN_SYSTEM_LOG', true)) return $response;
  31. $params = $request->param();
  32. if (isset($params['s'])) unset($params['s']);
  33. foreach ($params as $key => $val) {
  34. in_array($key, $this->sensitiveParams) && $params[$key] = "***********";
  35. }
  36. $method = strtolower($request->method());
  37. $url = $request->url();
  38. if (env('APP_DEBUG')) {
  39. trace(['url' => $url, 'method' => $method, 'params' => $params,], 'requestDebugInfo');
  40. }
  41. if ($request->isAjax()) {
  42. if (in_array($method, ['post', 'put', 'delete'])) {
  43. $title = '';
  44. try {
  45. $pathInfo = $request->pathinfo();
  46. $pathInfoExp = explode('/', $pathInfo);
  47. $_action = end($pathInfoExp) ?? '';
  48. $pathInfoExp = explode('.', $pathInfoExp[0] ?? '');
  49. $_name = $pathInfoExp[0] ?? '';
  50. $_controller = ucfirst($pathInfoExp[1] ?? '');
  51. $className = $_controller ? "app\admin\controller\\{$_name}\\{$_controller}" : "app\admin\controller\\{$_name}";
  52. if ($_name && $_action) {
  53. $reflectionMethod = new \ReflectionMethod($className, $_action);
  54. $attributes = $reflectionMethod->getAttributes(MiddlewareAnnotation::class);
  55. foreach ($attributes as $attribute) {
  56. $annotation = $attribute->newInstance();
  57. $_ignore = (array)$annotation->ignore;
  58. if (in_array('log', array_map('strtolower', $_ignore))) return $response;
  59. }
  60. $controllerTitle = $nodeTitle = '';
  61. $controllerAttributes = (new \ReflectionClass($className))->getAttributes(ControllerAnnotation::class);
  62. $actionAttributes = $reflectionMethod->getAttributes(NodeAnnotation::class);
  63. foreach ($controllerAttributes as $controllerAttribute) {
  64. $controllerAnnotation = $controllerAttribute->newInstance();
  65. $controllerTitle = $controllerAnnotation->title ?? '';
  66. }
  67. foreach ($actionAttributes as $actionAttribute) {
  68. $actionAnnotation = $actionAttribute->newInstance();
  69. $nodeTitle = $actionAnnotation->title ?? '';
  70. }
  71. $title = $controllerTitle . ' - ' . $nodeTitle;
  72. }
  73. }catch (\Throwable $exception) {
  74. }
  75. $ip = $request->ip();
  76. // 限制记录的响应内容,避免过大
  77. $_response = json_encode($response->getData(), JSON_UNESCAPED_UNICODE);
  78. $_response = mb_substr($_response, 0, 3000, 'utf-8');
  79. $data = [
  80. 'admin_id' => session('admin.id'),
  81. 'title' => $title,
  82. 'url' => $url,
  83. 'method' => $method,
  84. 'ip' => $ip,
  85. 'content' => json_encode($params, JSON_UNESCAPED_UNICODE),
  86. 'response' => $_response,
  87. 'useragent' => $request->server('HTTP_USER_AGENT'),
  88. 'create_time' => time(),
  89. ];
  90. SystemLogService::instance()->save($data);
  91. }
  92. }
  93. return $response;
  94. }
  95. }