Systemlog.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace app\admin\controller\base;
  3. /**
  4. * @title: 系统日志
  5. * @Description:
  6. * @Author: goldenrock 112049337@qq.com
  7. * @Date: 2024-08-24 09:40:45
  8. * @LastEditTime: 2024-11-29 17:48:58
  9. * @LastEditors: goldenrock
  10. * @FilePath: \OA_hbdrwhe:\HBDRWHCODE\JindDongFang_platform\app\admin\controller\base\Systemlog.php
  11. */
  12. use app\admin\controller\Base;
  13. use app\common\model\base\org\Org;
  14. use app\common\model\base\Systemlog as Syslog;
  15. use app\common\model\base\user\User;
  16. use app\common\model\base\user\UserRole;
  17. class Systemlog extends Base
  18. {
  19. protected $model = null;
  20. public function initialize()
  21. {
  22. parent::initialize();
  23. $this->model = new Syslog;
  24. }
  25. //创建列表查询语句
  26. public function createWhere()
  27. {
  28. $data = $this->request->param();
  29. if(empty($data['org_id'])){
  30. $org_ids = Org::getChildrenIds($this->userinfo['org_id']);
  31. }else{
  32. $org_ids = Org::getChildrenIds($data['org_id']);
  33. }
  34. $user_ids = UserRole::where('org_id','in',$org_ids)->column('user_id');
  35. $where = [['create_uid','in',$user_ids]];
  36. if(!empty($data['username'])){
  37. $where[] = ['username','LIKE',"%{$data['username']}%"];
  38. }
  39. if(!empty($data['username'])){
  40. $where[] = ['username','LIKE',"%{$data['username']}%"];
  41. }
  42. if(!empty($data['daterange']) && 2==count($data['daterange'])){
  43. $data['daterange'][1].=' 23:59:59';
  44. $where[] = ['createtime','between',$data['daterange']];
  45. }
  46. return $where;
  47. }
  48. /**
  49. * @title: 系统日志列表
  50. * @param {int} {pageNo} {非必填,默认值为1} {页码}
  51. * @param {int} {pageSize} {非必填,默认值为10} {每页数量}
  52. * @param {string} {username} {非必填} {账户搜索}
  53. * @param {array} {daterange} {非必填} {时间区间}
  54. * @param {int} {org_id} {非必填} {组织id}
  55. * @return mixed
  56. * @Author: wangkewei
  57. * @Date: 2021/5/18 10:08
  58. */
  59. public function getList($pageNo=1,$pageSize=20)
  60. {
  61. $list = $this->model
  62. ->where($this->createWhere())
  63. ->with(['request','menu','createUser'=>function($query){
  64. $query->with(['org'=>function($query_){
  65. $query_->field('org_id,name')->append(['status_txt']);
  66. }])->field('user_id,name,org_id,status')->append(['status_txt']);
  67. }])
  68. ->order("createtime DESC")
  69. ->paginate(['page'=>$pageNo,'list_rows'=>$pageSize]);
  70. return pageRes(1,"获取成功",$list->total(),$list->items());
  71. }
  72. /**
  73. * @title: 系统日志导出
  74. * @param {string} {username} {非必填} {账户搜索}
  75. * @param {array} {daterange} {非必填} {时间区间}
  76. * @param {int} {org_id} {非必填} {组织id}
  77. * @return mixed
  78. * @Author: wangkewei
  79. * @Date: 2022/2/28 17:18
  80. * @throws \Exception
  81. */
  82. public function doExport()
  83. {
  84. $list = $this->model->where($this->createWhere())
  85. ->with(['request','menu','createUser'=>function($query){
  86. $query->with(['org'=>function($query_){
  87. $query_->field('org_id,name')->append(['status_txt']);
  88. }])->field('user_id,name,org_id,status')->append(['status_txt']);
  89. }])
  90. ->order("createtime DESC")
  91. ->select();
  92. if($list->isEmpty()){
  93. return res(2,'没有数据导出');
  94. }
  95. $head = ['序号','操作用户','用户组织','访问路径','操作代码','操作内容','操作结果','IP地址','操作时间'];
  96. $body = [];
  97. foreach($list as $key=>$item){
  98. $index = $key + 1;
  99. $body[$index][] = $item->id;
  100. $body[$index][] = $item->username;
  101. $body[$index][] = $item->createUser->org->name;
  102. $body[$index][] = $item->path;
  103. $body[$index][] = $item->code;
  104. $body[$index][] = $item->remark;
  105. $body[$index][] = $item->result_txt;
  106. $body[$index][] = $item->ip;
  107. $body[$index][] = $item->createtime;
  108. }
  109. //创建文件夹
  110. $basepath = "uploads".DS."download".DS.date('Ymd');
  111. $savepath = public_path().$basepath;
  112. if(!is_dir($savepath)){
  113. mkdir($savepath,0777,true);
  114. }
  115. //保存文件
  116. $filename = date('Ymd')."系统日志导出.xls";
  117. $path = $savepath."/".$filename;
  118. $export_help = new \excel\MultiHeadExcel();
  119. $export_help->export_help($body,$head,$path);
  120. //返回路径
  121. $returnpath = WEBURL.DS.$basepath.DS.$filename;
  122. slog(1,"导出了系统日志");
  123. return res(1,"获取成功",['url'=>$returnpath,'name'=>$filename]);
  124. }
  125. }