SystemLog.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace app\admin\model\system;
  3. use crmeb\traits\ModelTrait;
  4. use crmeb\basic\BaseModel;
  5. /**
  6. * 管理员操作记录
  7. * Class SystemLog
  8. * @package app\admin\model\system
  9. */
  10. class SystemLog extends BaseModel
  11. {
  12. /**
  13. * 数据表主键
  14. * @var string
  15. */
  16. protected $pk = 'id';
  17. /**
  18. * 模型名称
  19. * @var string
  20. */
  21. protected $name = 'system_log';
  22. use ModelTrait;
  23. protected $insert = ['add_time'];
  24. protected function setAddTimeAttr()
  25. {
  26. return time();
  27. }
  28. /**
  29. * 管理员访问记录
  30. *
  31. * @param $adminId
  32. * @param $adminName
  33. * @param $type
  34. * @return SystemLog|\think\Model
  35. */
  36. public static function adminVisit($adminId, $adminName, $type)
  37. {
  38. $request = app('request');
  39. $module = app('http')->getName();
  40. $controller = $request->controller();
  41. $action = $request->action();
  42. $route = $request->route();
  43. self::startTrans();
  44. try {
  45. $data = [
  46. 'method' => app('http')->getName(),
  47. 'admin_id' => $adminId,
  48. 'add_time' => time(),
  49. 'admin_name' => $adminName,
  50. 'path' => SystemMenus::getAuthName($action, $controller, $module, $route),
  51. 'page' => SystemMenus::getVisitName($action, $controller, $module, $route) ?: '未知',
  52. 'ip' => $request->ip(),
  53. 'type' => $type
  54. ];
  55. $res = self::create($data);
  56. if ($res) {
  57. self::commit();
  58. return true;
  59. } else {
  60. self::rollback();
  61. return false;
  62. }
  63. } catch (\Exception $e) {
  64. self::rollback();
  65. return self::setErrorInfo($e->getMessage());
  66. }
  67. }
  68. /**
  69. * 手动添加管理员当前页面访问记录
  70. * @param array $adminInfo
  71. * @param string $page 页面名称
  72. * @return object
  73. */
  74. public static function setCurrentVisit($adminInfo, $page)
  75. {
  76. $request = app('request');
  77. $module = app('http')->getName();
  78. $controller = $request->controller();
  79. $action = $request->action();
  80. $route = $request->route();
  81. $data = [
  82. 'method' => $request->method(),
  83. 'admin_id' => $adminInfo['id'],
  84. 'path' => SystemMenus::getAuthName($action, $controller, $module, $route),
  85. 'page' => $page,
  86. 'ip' => $request->ip()
  87. ];
  88. return self::create($data);
  89. }
  90. /**
  91. * 获取管理员访问记录
  92. * @param array $where
  93. * @return array
  94. */
  95. public static function systemPage($where = array())
  96. {
  97. $model = new self;
  98. $model = $model->alias('l');
  99. if ($where['pages'] !== '') $model = $model->where('l.page', 'LIKE', "%$where[pages]%");
  100. if ($where['path'] !== '') $model = $model->where('l.path', 'LIKE', "%$where[path]%");
  101. if ($where['ip'] !== '') $model = $model->where('l.ip', 'LIKE', "%$where[ip]%");
  102. if ($where['admin_id'] != '')
  103. $adminIds = $where['admin_id'];
  104. else
  105. $adminIds = SystemAdmin::where('level', '>=', $where['level'])->column('id', 'id');
  106. $model = $model->where('l.admin_id', 'IN', $adminIds);
  107. if ($where['data'] !== '') {
  108. list($startTime, $endTime) = explode(' - ', $where['data']);
  109. $model = $model->where('l.add_time', '>', strtotime($startTime));
  110. $model = $model->where('l.add_time', '<', strtotime($endTime));
  111. }
  112. $model->where('l.type', 'system');
  113. $model = $model->order('l.id desc');
  114. return self::page($model, $where);
  115. }
  116. /**
  117. * 删除超过90天的日志
  118. * @throws \Exception
  119. */
  120. public static function deleteLog()
  121. {
  122. $model = new self;
  123. $model->where('add_time', '<', time() - 7776000);
  124. $model->delete();
  125. }
  126. }