SysLog.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * +----------------------------------------------------------------------
  4. * | 管理员日志模型
  5. * +----------------------------------------------------------------------
  6. */
  7. namespace app\model;
  8. // 引入框架内置类
  9. use think\Model;
  10. use think\facade\Request;
  11. use think\facade\Session;
  12. class SysLog extends Model
  13. {
  14. // 获取列表
  15. public static function queryPage(int $pageSize = 0)
  16. {
  17. $list = self::order('id desc')->paginate($pageSize);
  18. return $list;
  19. }
  20. // 管理员日志记录
  21. public static function record($route = "")
  22. {
  23. // 入库信息
  24. $userid = Session::get('adminuser.userid', 0);
  25. $username = Session::get('adminuser.username', '');
  26. $url = Request::url();
  27. $title = '';
  28. $content = Request::except(['s', '_pjax']); //s 变量为系统内置的变量,_pjax为js的变量,无记录的必要
  29. $ip = Request::ip();
  30. $userAgent = Request::server('HTTP_USER_AGENT');
  31. // 标题处理
  32. $active = \app\model\SysMenu::where('url', $route)->find();
  33. $title = $active != null ? $active['name'] : "";
  34. // 内容处理(过长的内容和涉及密码的内容不进行记录)
  35. if ($content) {
  36. foreach ($content as $k => $v) {
  37. if (is_string($v) && strlen($v) > 200 || stripos($k, 'password') !== false || stripos($k, 'newpassword') !== false || stripos($k, 'repassword') !== false) {
  38. unset($content[$k]);
  39. }
  40. }
  41. }
  42. // 插入数据
  43. if (!empty($title)) {
  44. // 查询管理员上一条数据
  45. $result = self::where('userid', '=', $userid)
  46. ->order('id', 'desc')
  47. ->find();
  48. if ($result) {
  49. if ($result->url != $url) {
  50. self::create([
  51. 'title' => $title ? $title : '',
  52. 'username' => $username,
  53. 'content' => ! is_scalar($content) ? json_encode($content, JSON_UNESCAPED_UNICODE) : $content,
  54. 'url' => $url,
  55. 'userid' => $userid,
  56. 'user_agent' => $userAgent,
  57. 'ip' => $ip
  58. ]);
  59. }
  60. } else {
  61. self::create([
  62. 'title' => $title ? $title : '',
  63. 'username' => $username,
  64. 'content' => ! is_scalar($content) ? json_encode($content, JSON_UNESCAPED_UNICODE) : $content,
  65. 'url' => $url,
  66. 'userid' => $userid,
  67. 'user_agent' => $userAgent,
  68. 'ip' => $ip
  69. ]);
  70. }
  71. }
  72. }
  73. }