SystemLog.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. /**
  10. * 管理员操作记录
  11. * Class SystemLog
  12. * @package app\models\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. * @param $mer_id
  39. * @return bool
  40. */
  41. public static function adminVisit($adminId, $adminName, $type, $mer_id)
  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. 'merchant_id' => $mer_id,
  59. 'type' => $type
  60. ];
  61. $res = self::create($data);
  62. if ($res) {
  63. self::commit();
  64. return true;
  65. } else {
  66. self::rollback();
  67. return false;
  68. }
  69. } catch (\Exception $e) {
  70. self::rollback();
  71. return self::setErrorInfo($e->getMessage());
  72. }
  73. }
  74. /**
  75. * 手动添加管理员当前页面访问记录
  76. * @param array $adminInfo
  77. * @param string $page 页面名称
  78. * @return object
  79. */
  80. public static function setCurrentVisit($adminInfo, $page)
  81. {
  82. $request = app('request');
  83. $module = app('http')->getName();
  84. $controller = $request->controller();
  85. $action = $request->action();
  86. $route = $request->route();
  87. $data = [
  88. 'method' => $request->method(),
  89. 'admin_id' => $adminInfo['id'],
  90. 'path' => SystemMenus::getAuthName($action, $controller, $module, $route),
  91. 'page' => $page,
  92. 'ip' => $request->ip()
  93. ];
  94. return self::create($data);
  95. }
  96. /**
  97. * 获取管理员访问记录
  98. * @param array $where
  99. * @return array
  100. */
  101. public static function systemPage($where = array())
  102. {
  103. $model = new self;
  104. $model = $model->alias('l');
  105. if ($where['pages'] !== '') $model = $model->where('l.page', 'LIKE', "%$where[pages]%");
  106. if ($where['path'] !== '') $model = $model->where('l.path', 'LIKE', "%$where[path]%");
  107. if ($where['ip'] !== '') $model = $model->where('l.ip', 'LIKE', "%$where[ip]%");
  108. if ($where['admin_id'] != '')
  109. $adminIds = $where['admin_id'];
  110. else
  111. $adminIds = SystemAdmin::where('level', '>=', $where['level'])->column('id', 'id');
  112. $model = $model->where('l.admin_id', 'IN', $adminIds);
  113. if ($where['data'] !== '') {
  114. $model = self::getModelTime($where, $model, "add_time");
  115. }
  116. $model->where('l.type', 'system');
  117. $count = $model->count();
  118. $model = $model->order('l.id desc');
  119. $list = $model->page((int)$where['page'], (int)$where['limit'])->select();
  120. return compact('count', 'list');
  121. }
  122. /**
  123. * 删除超过90天的日志
  124. * @throws \Exception
  125. */
  126. public static function deleteLog()
  127. {
  128. $model = new self;
  129. $model->where('add_time', '<', time() - 7776000);
  130. $model->delete();
  131. }
  132. }