SystemLogBadmin.php 3.9 KB

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