AdminLog.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace app\admin\model;
  3. use think\Model;
  4. use app\admin\library\Auth;
  5. /**
  6. * AdminLog模型
  7. * @controllerUrl 'authAdminLog'
  8. */
  9. class AdminLog extends Model
  10. {
  11. protected $autoWriteTimestamp = 'int';
  12. protected $createTime = 'createtime';
  13. protected $updateTime = false;
  14. // 自定义日志标题
  15. protected static $title = '';
  16. // 自定义日志内容
  17. protected static $data = '';
  18. // 忽略的链接正则列表
  19. protected static $urlIgnoreRegex = [
  20. '/^(.*)\/(select|index|logout)$/i',
  21. ];
  22. public static function setTitle($title)
  23. {
  24. self::$title = $title;
  25. }
  26. public static function setData($data)
  27. {
  28. self::$data = $data;
  29. }
  30. public static function setUrlIgnoreRegex($regex = [])
  31. {
  32. $regex = is_array($regex) ? $regex : [$regex];
  33. self::$urlIgnoreRegex = array_merge(self::$urlIgnoreRegex, $regex);
  34. }
  35. /**
  36. * 数据脱敏
  37. * @param $data
  38. * @return array
  39. */
  40. protected static function pureData($data)
  41. {
  42. if (!is_array($data)) {
  43. return $data;
  44. }
  45. foreach ($data as $index => &$item) {
  46. if (preg_match("/(password|salt|token)/i", $index)) {
  47. $item = "***";
  48. } else {
  49. if (is_array($item)) {
  50. $item = self::pureData($item);
  51. }
  52. }
  53. }
  54. return $data;
  55. }
  56. public static function record($title = '', $data = '')
  57. {
  58. $auth = Auth::instance();
  59. $admin_id = $auth->isLogin() ? $auth->id : 0;
  60. $username = $auth->isLogin() ? $auth->username : __('Unknown');
  61. $controller = str_replace('.', '/', request()->controller(true));
  62. $action = request()->action(true);
  63. $path = $controller . '/' . $action;
  64. if (self::$urlIgnoreRegex) {
  65. foreach (self::$urlIgnoreRegex as $item) {
  66. if (preg_match($item, $path)) {
  67. return;
  68. }
  69. }
  70. }
  71. $data = $data ?: self::$data;
  72. if (!$data) {
  73. $data = request()->param('', null, 'trim,strip_tags,htmlspecialchars');
  74. $data = self::pureData($data);
  75. }
  76. $title = $title ?: self::$title;
  77. if (!$title) {
  78. $controllerTitle = MenuRule::where('name', $controller)->value('title');
  79. $title = MenuRule::where('name', $path)->value('title');
  80. $title = $title ?: __('Unknown') . '(' . $action . ')';
  81. $title = $controllerTitle ? ($controllerTitle . '-' . $title) : $title;
  82. }
  83. self::create([
  84. 'admin_id' => $admin_id,
  85. 'username' => $username,
  86. 'url' => substr(request()->url(), 0, 1500),
  87. 'title' => $title,
  88. 'data' => !is_scalar($data) ? json_encode($data, JSON_UNESCAPED_UNICODE) : $data,
  89. 'ip' => request()->ip(),
  90. 'useragent' => substr(request()->server('HTTP_USER_AGENT'), 0, 255),
  91. ]);
  92. }
  93. public function admin()
  94. {
  95. return $this->belongsTo(Admin::class);
  96. }
  97. }