AdminLog.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace app\common\model;
  3. use app\admin\library\Auth;
  4. use think\Log;
  5. use think\Model;
  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. public static function setTitle($title)
  18. {
  19. self::$title = $title;
  20. }
  21. public static function setContent($content)
  22. {
  23. self::$content = $content;
  24. }
  25. public static function record($title = '')
  26. {
  27. $url = strtolower(request()->baseUrl());
  28. if (in_array($url,
  29. ['/admin/collect/ajaxtoday', '/admin/user/collect/ajaxtoday', '/admin/book/collect/ajaxcharge'])) {
  30. return false;
  31. }
  32. $auth = Auth::instance();
  33. $admin_id = $auth->isLogin() ? $auth->id : 0;
  34. $username = $auth->isLogin() ? $auth->username : __('Unknown');
  35. $content = self::$content;
  36. if (!$content)
  37. {
  38. $content = request()->param();
  39. foreach ($content as $k => $v)
  40. {
  41. if (is_string($v) && strlen($v) > 200 || stripos($k, 'password') !== false)
  42. {
  43. unset($content[$k]);
  44. }
  45. }
  46. }
  47. $title = self::$title;
  48. if (!$title)
  49. {
  50. $title = [];
  51. $breadcrumb = Auth::instance()->getBreadcrumb();
  52. foreach ($breadcrumb as $k => $v)
  53. {
  54. $title[] = $v['title'];
  55. }
  56. $title = implode(' ', $title);
  57. }
  58. self::create([
  59. 'title' => $title,
  60. 'content' => !is_scalar($content) ? json_encode($content) : $content,
  61. 'url' => substr(request()->url(), 0, 100),
  62. 'admin_id' => $admin_id,
  63. 'username' => $username,
  64. 'useragent' => request()->server('HTTP_USER_AGENT', ''),
  65. 'ip' => request()->ip()
  66. ]);
  67. }
  68. public function admin()
  69. {
  70. return $this->belongsTo('Admin', 'admin_id')->setEagerlyType(0);
  71. }
  72. }