SystemLog.php 4.0 KB

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