SystemLog.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/28
  6. */
  7. namespace app\admin\model\system;
  8. use traits\ModelTrait;
  9. use basic\ModelBasic;
  10. use think\Request;
  11. use app\admin\model\system\SystemMenus;
  12. use app\admin\model\system\SystemAdmin;
  13. /**
  14. * 管理员操作记录
  15. * Class SystemLog
  16. * @package app\admin\model\system
  17. */
  18. class SystemLog extends ModelBasic
  19. {
  20. use ModelTrait;
  21. protected $insert = ['add_time'];
  22. protected function setAddTimeAttr()
  23. {
  24. return time();
  25. }
  26. /**
  27. * 管理员访问记录
  28. * @param Request $request
  29. */
  30. public static function adminVisit($adminId,$adminName,$type)
  31. {
  32. $request = Request::instance();
  33. $module = $request->module();
  34. $controller = $request->controller();
  35. $action = $request->action();
  36. $route = $request->route();
  37. $data = [
  38. 'method'=>$request->method(),
  39. 'admin_id'=>$adminId,
  40. 'admin_name'=>$adminName,
  41. 'path'=>SystemMenus::getAuthName($action,$controller,$module,$route),
  42. 'page'=>SystemMenus::getVisitName($action,$controller,$module,$route)?:'未知',
  43. 'ip'=>$request->ip(),
  44. 'type'=>$type
  45. ];
  46. return self::set($data);
  47. }
  48. /**
  49. * 手动添加管理员当前页面访问记录
  50. * @param array $adminInfo
  51. * @param string $page 页面名称
  52. * @return object
  53. */
  54. public static function setCurrentVisit($adminInfo, $page)
  55. {
  56. $request = Request::instance();
  57. $module = $request->module();
  58. $controller = $request->controller();
  59. $action = $request->action();
  60. $route = $request->route();
  61. $data = [
  62. 'method'=>$request->method(),
  63. 'admin_id'=>$adminInfo['id'],
  64. 'path'=>SystemMenus::getAuthName($action,$controller,$module,$route),
  65. 'page'=>$page,
  66. 'ip'=>$request->ip()
  67. ];
  68. return self::set($data);
  69. }
  70. /**
  71. * 获取管理员访问记录
  72. * */
  73. public static function systemPage($where = array()){
  74. $model = new self;
  75. $model = $model->alias('l');
  76. if($where['pages'] !== '') $model = $model->where('l.page','LIKE',"%$where[pages]%");
  77. if($where['path'] !== '') $model = $model->where('l.path','LIKE',"%$where[path]%");
  78. if($where['ip'] !== '') $model = $model->where('l.ip','LIKE',"%$where[ip]%");
  79. if($where['admin_id'] != '')
  80. $adminIds = $where['admin_id'];
  81. else
  82. $adminIds = SystemAdmin::where('level','>=',$where['level'])->column('id');
  83. $model = $model->where('l.admin_id','IN',$adminIds);
  84. if($where['data'] !== ''){
  85. list($startTime,$endTime) = explode(' - ',$where['data']);
  86. $model = $model->where('l.add_time','>',strtotime($startTime));
  87. $model = $model->where('l.add_time','<',strtotime($endTime));
  88. }
  89. $model->where('l.type','system');
  90. $model = $model->order('l.id desc');
  91. return self::page($model,$where);
  92. }
  93. /**
  94. * 删除超过90天的日志
  95. */
  96. public static function deleteLog(){
  97. $model = new self;
  98. $model->where('add_time','<',time()-7776000);
  99. $model->delete();
  100. }
  101. }