AdminLog.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace app\admin\model;
  3. use think\Model;
  4. class AdminLog extends Model
  5. {
  6. // 开启自动写入时间戳字段
  7. protected $autoWriteTimestamp = 'int';
  8. // 定义时间戳字段名
  9. protected $createTime = 'createtime';
  10. protected $updateTime = '';
  11. //自定义日志标题
  12. protected static $title = '';
  13. //自定义日志内容
  14. protected static $content = '';
  15. public static function setTitle($title)
  16. {
  17. self::$title = $title;
  18. }
  19. public static function setContent($content)
  20. {
  21. self::$content = $content;
  22. }
  23. public static function record($title = '')
  24. {
  25. $admin = \think\Session::get('admin');
  26. $admin_id = $admin ? $admin->id : 0;
  27. $username = $admin ? $admin->username : __('Unknown');
  28. $content = self::$content;
  29. if (!$content)
  30. {
  31. $content = request()->param();
  32. foreach ($content as $k => $v)
  33. {
  34. if (is_string($v) && strlen($v) > 200 || stripos($k, 'password') !== false)
  35. {
  36. unset($content[$k]);
  37. }
  38. }
  39. }
  40. $title = self::$title;
  41. if (!$title)
  42. {
  43. $title = [];
  44. $breadcrumb = \app\admin\library\Auth::instance()->getBreadcrumb();
  45. foreach ($breadcrumb as $k => $v)
  46. {
  47. $title[] = $v['title'];
  48. }
  49. $title = implode(' ', $title);
  50. }
  51. self::create([
  52. 'title' => $title,
  53. 'content' => !is_scalar($content) ? json_encode($content) : $content,
  54. 'url' => request()->url(),
  55. 'admin_id' => $admin_id,
  56. 'username' => $username,
  57. 'useragent' => request()->server('HTTP_USER_AGENT'),
  58. 'ip' => request()->ip()
  59. ]);
  60. }
  61. public function admin()
  62. {
  63. return $this->belongsTo('Admin', 'admin_id')->setEagerlyType(0);
  64. }
  65. }