AdminLog.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. $controllername = Loader::parseName(request()->controller());
  45. $actionname = strtolower(request()->action());
  46. $path = str_replace('.', '/', $controllername) . '/' . $actionname;
  47. if (self::$ignoreRegex) {
  48. foreach (self::$ignoreRegex as $index => $item) {
  49. if (preg_match($item, $path)) {
  50. return;
  51. }
  52. }
  53. }
  54. $content = $content ? $content : self::$content;
  55. if (!$content) {
  56. $content = request()->param('', null, 'trim,strip_tags,htmlspecialchars');
  57. $content = self::getPureContent($content);
  58. }
  59. $title = $title ? $title : self::$title;
  60. if (!$title) {
  61. $title = [];
  62. $breadcrumb = Auth::instance()->getBreadcrumb($path);
  63. foreach ($breadcrumb as $k => $v) {
  64. $title[] = $v['title'];
  65. }
  66. $title = implode(' / ', $title);
  67. }
  68. self::create([
  69. 'title' => $title,
  70. 'content' => !is_scalar($content) ? json_encode($content, JSON_UNESCAPED_UNICODE) : $content,
  71. 'url' => substr(request()->url(), 0, 1500),
  72. 'admin_id' => $admin_id,
  73. 'username' => $username,
  74. 'useragent' => substr(request()->server('HTTP_USER_AGENT'), 0, 255),
  75. 'ip' => request()->ip()
  76. ]);
  77. }
  78. /**
  79. * 获取已屏蔽关键信息的数据
  80. * @param $content
  81. * @return false|string
  82. */
  83. protected static function getPureContent($content)
  84. {
  85. if (!is_array($content)) {
  86. return $content;
  87. }
  88. foreach ($content as $index => &$item) {
  89. if (preg_match("/(password|salt|token)/i", $index)) {
  90. $item = "***";
  91. } else {
  92. if (is_array($item)) {
  93. $item = self::getPureContent($item);
  94. }
  95. }
  96. }
  97. return $content;
  98. }
  99. public function admin()
  100. {
  101. return $this->belongsTo('Admin', 'admin_id')->setEagerlyType(0);
  102. }
  103. }