AdminLog.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace app\admin\model;
  3. use app\admin\library\Auth;
  4. use think\Model;
  5. use think\Loader;
  6. class AdminLog extends Model
  7. {
  8. // 开启自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'int';
  10. // 定义时间戳字段名
  11. protected $createTime = 'createtime';
  12. protected $updateTime = '';
  13. //自定义日志标题
  14. protected static $title = '';
  15. //自定义日志内容
  16. protected static $content = '';
  17. //忽略的链接正则列表
  18. protected static $ignoreRegex = [
  19. '/^(.*)\/(selectpage|index)$/i',
  20. ];
  21. public static function setTitle($title)
  22. {
  23. self::$title = $title;
  24. }
  25. public static function setContent($content)
  26. {
  27. self::$content = $content;
  28. }
  29. public static function setIgnoreRegex($regex = [])
  30. {
  31. $regex = is_array($regex) ? $regex : [$regex];
  32. self::$ignoreRegex = array_merge(self::$ignoreRegex, $regex);
  33. }
  34. /**
  35. * 记录日志
  36. * @param string $title 日志标题
  37. * @param string $content 日志内容
  38. */
  39. public static function record($title = '', $content = '')
  40. {
  41. $auth = Auth::instance();
  42. $admin_id = $auth->isLogin() ? $auth->id : 0;
  43. $username = $auth->isLogin() ? $auth->username : __('Unknown');
  44. // 设置过滤函数
  45. request()->filter('trim,strip_tags,htmlspecialchars');
  46. $controllername = Loader::parseName(request()->controller());
  47. $actionname = strtolower(request()->action());
  48. $path = str_replace('.', '/', $controllername) . '/' . $actionname;
  49. if (self::$ignoreRegex) {
  50. foreach (self::$ignoreRegex as $index => $item) {
  51. if (preg_match($item, $path)) {
  52. return;
  53. }
  54. }
  55. }
  56. $content = $content ?: self::$content;
  57. if (!$content) {
  58. $content = request()->param('') ?: file_get_contents("php://input");
  59. $content = self::getPureContent($content);
  60. }
  61. $title = $title ?: self::$title;
  62. if (!$title) {
  63. $title = [];
  64. $breadcrumb = Auth::instance()->getBreadcrumb($path);
  65. foreach ($breadcrumb as $k => $v) {
  66. $title[] = $v['title'];
  67. }
  68. $title = implode(' / ', $title);
  69. }
  70. self::create([
  71. 'title' => $title,
  72. 'content' => !is_scalar($content) ? json_encode($content, JSON_UNESCAPED_UNICODE) : $content,
  73. 'url' => substr(xss_clean(strip_tags(request()->url())), 0, 1500),
  74. 'admin_id' => $admin_id,
  75. 'username' => $username,
  76. 'useragent' => substr(request()->server('HTTP_USER_AGENT'), 0, 255),
  77. 'ip' => xss_clean(strip_tags(request()->ip()))
  78. ]);
  79. }
  80. /**
  81. * 获取已屏蔽关键信息的数据
  82. * @param $content
  83. * @return array
  84. */
  85. protected static function getPureContent($content)
  86. {
  87. if (!is_array($content)) {
  88. return $content;
  89. }
  90. foreach ($content as $index => &$item) {
  91. if (preg_match("/(password|salt|token)/i", $index)) {
  92. $item = "***";
  93. } else {
  94. if (is_array($item)) {
  95. $item = self::getPureContent($item);
  96. }
  97. }
  98. }
  99. return $content;
  100. }
  101. public function admin()
  102. {
  103. return $this->belongsTo('Admin', 'admin_id')->setEagerlyType(0);
  104. }
  105. }