SystemLog.php 3.6 KB

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