SysLog.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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()
  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. $route = strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', Request::controller())) . '/' . strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', Request::action()));
  33. $active = \app\model\SysMenu::where('url', $route)->find();
  34. $title = $active != null ? $active['name'] : "";
  35. // 内容处理(过长的内容和涉及密码的内容不进行记录)
  36. if ($content) {
  37. foreach ($content as $k => $v) {
  38. if (is_string($v) && strlen($v) > 200 || stripos($k, 'password') !== false || stripos($k, 'newpassword') !== false || stripos($k, 'repassword') !== false) {
  39. unset($content[$k]);
  40. }
  41. }
  42. }
  43. // 插入数据
  44. if (!empty($title)) {
  45. // 查询管理员上一条数据
  46. $result = self::where('userid', '=', $userid)
  47. ->order('id', 'desc')
  48. ->find();
  49. if ($result) {
  50. if ($result->url != $url) {
  51. self::create([
  52. 'title' => $title ? $title : '',
  53. 'username' => $username,
  54. 'content' => ! is_scalar($content) ? json_encode($content, JSON_UNESCAPED_UNICODE) : $content,
  55. 'url' => $url,
  56. 'userid' => $userid,
  57. 'user_agent' => $userAgent,
  58. 'ip' => $ip
  59. ]);
  60. }
  61. } else {
  62. self::create([
  63. 'title' => $title ? $title : '',
  64. 'username' => $username,
  65. 'content' => ! is_scalar($content) ? json_encode($content, JSON_UNESCAPED_UNICODE) : $content,
  66. 'url' => $url,
  67. 'userid' => $userid,
  68. 'user_agent' => $userAgent,
  69. 'ip' => $ip
  70. ]);
  71. }
  72. }
  73. }
  74. }