AdminLogUtils.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018-2020 rights reserved.
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // | Date: 2021/2/9 下午3:23
  10. // +----------------------------------------------------------------------
  11. namespace library\utils;
  12. use app\model\system\Admin;
  13. use app\model\system\AdminLog;
  14. use app\Request;
  15. class AdminLogUtils
  16. {
  17. private $request;
  18. /**
  19. * 路由配置文件 | 读取@路径访问标题内容
  20. * @var
  21. */
  22. private $_path;
  23. /**
  24. * 日志model
  25. * @var AdminLog
  26. */
  27. private $adminLogModel;
  28. private $logConfig = null;
  29. public function __construct(Request $r)
  30. {
  31. $this->logConfig = \config('log');
  32. $this->request = $r;
  33. $this->adminLogModel = new AdminLog;
  34. }
  35. /**
  36. * 路由配置文件
  37. * // @基本设置
  38. * Route::group('sys', function () {
  39. *
  40. *
  41. * }
  42. * 自动识别@参数
  43. * @param $path
  44. * @return AdminLogUtils
  45. */
  46. public function path($path): AdminLogUtils
  47. {
  48. $this->_path = $path;
  49. return $this;
  50. }
  51. /**
  52. * 记录日志
  53. */
  54. public function log(): void
  55. {
  56. $pathinfo = $this->request->pathinfo();
  57. //禁用访问
  58. if ($this->logConfig['LOG_TYPE'] == 1 && in_array($pathinfo, $this->logConfig['LOG_DISABLE'])) {
  59. return;
  60. }
  61. //部分记录
  62. if ($this->logConfig['LOG_TYPE'] == 2 && !in_array($pathinfo, $this->logConfig['LOG_ALLOW'])) {
  63. return;
  64. }
  65. $ip = get_client_ip();
  66. if(empty($ip)){
  67. $ip = $this->request->ip();
  68. }
  69. $title = $this->getRoule($pathinfo);
  70. $save = [];
  71. $save['admin_username'] = $this->request->adminInfo['username'];
  72. $save['time'] = time();
  73. $save['ip'] = $ip;
  74. $save['action'] = $pathinfo;
  75. if ($this->logConfig['LOG_PARAM']) {
  76. $post = array_filter($this->request->post(), function ($item) {
  77. if (empty($item)) return false;
  78. return true;
  79. });
  80. $get = array_filter($this->request->get(), function ($item) {
  81. if (empty($item)) return false;
  82. return true;
  83. });
  84. $save['post'] = json_encode($post, \JSON_UNESCAPED_UNICODE);
  85. $save['get'] = json_encode($get, \JSON_UNESCAPED_UNICODE);
  86. }
  87. $save['title'] = empty($title) ? '-无-' : $title;
  88. $this->adminLogModel->insert($save);
  89. }
  90. /**
  91. * 获取路由参数
  92. * @param $pathinfo
  93. */
  94. private function getRoule($pathinfo)
  95. {
  96. $expAr = explode('/', $pathinfo);
  97. if (empty($expAr)) return '';
  98. $strAr = [];
  99. //路由一级
  100. $content = @file_get_contents($this->_path . "/{$expAr[0]}.php");
  101. preg_match("/\/\/\@(.*?)Route::group/is", $content, $match);
  102. if (!empty($match)) {
  103. $strAr[] = trim($match[1]);
  104. }
  105. //路由二级
  106. if (count($expAr) > 1) {
  107. $ruleName = $expAr[1];
  108. preg_match("/\/\/\@(.[^Route::group]*?)Route::(post|get|rule)(\(\'{$ruleName}\'|\(\"{$ruleName}\")/s", $content, $match);
  109. if (!empty($match)) {
  110. $strAr[] = trim($match[1]);
  111. }
  112. }
  113. return empty($strAr) ? '' : join('-', $strAr);
  114. }
  115. }