AdminLog.php 2.0 KB

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