AdminLog.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. public static function record($title = '')
  12. {
  13. $admin = \think\Session::get('admin');
  14. $admin_id = $admin ? $admin->id : 0;
  15. $username = $admin ? $admin->username : __('Unknown');
  16. $content = request()->param();
  17. foreach ($content as $k => $v)
  18. {
  19. if (is_string($v) && strlen($v) > 200)
  20. {
  21. unset($content[$k]);
  22. }
  23. }
  24. $title = [];
  25. $breadcrumb = \app\admin\library\Auth::instance()->getBreadcrumb();
  26. foreach ($breadcrumb as $k => $v)
  27. {
  28. $title[] = $v['title'];
  29. }
  30. self::create([
  31. 'title' => implode(' ', $title),
  32. 'content' => json_encode($content),
  33. 'url' => request()->url(),
  34. 'admin_id' => $admin_id,
  35. 'username' => $username,
  36. 'useragent' => request()->server('HTTP_USER_AGENT'),
  37. 'ip' => request()->ip()
  38. ]);
  39. }
  40. public function admin()
  41. {
  42. return $this->belongsTo('Admin', 'admin_id')->setEagerlyType(0);
  43. }
  44. }