model = new Syslog; } //创建列表查询语句 public function createWhere() { $data = $this->request->param(); if(empty($data['org_id'])){ $org_ids = Org::getChildrenIds($this->userinfo['org_id']); }else{ $org_ids = Org::getChildrenIds($data['org_id']); } $user_ids = UserRole::where('org_id','in',$org_ids)->column('user_id'); $where = [['create_uid','in',$user_ids]]; if(!empty($data['username'])){ $where[] = ['username','LIKE',"%{$data['username']}%"]; } if(!empty($data['username'])){ $where[] = ['username','LIKE',"%{$data['username']}%"]; } if(!empty($data['daterange']) && 2==count($data['daterange'])){ $data['daterange'][1].=' 23:59:59'; $where[] = ['createtime','between',$data['daterange']]; } return $where; } /** * @title: 系统日志列表 * @param {int} {pageNo} {非必填,默认值为1} {页码} * @param {int} {pageSize} {非必填,默认值为10} {每页数量} * @param {string} {username} {非必填} {账户搜索} * @param {array} {daterange} {非必填} {时间区间} * @param {int} {org_id} {非必填} {组织id} * @return mixed * @Author: wangkewei * @Date: 2021/5/18 10:08 */ public function getList($pageNo=1,$pageSize=20) { $list = $this->model ->where($this->createWhere()) ->with(['request','menu','createUser'=>function($query){ $query->with(['org'=>function($query_){ $query_->field('org_id,name')->append(['status_txt']); }])->field('user_id,name,org_id,status')->append(['status_txt']); }]) ->order("createtime DESC") ->paginate(['page'=>$pageNo,'list_rows'=>$pageSize]); return pageRes(1,"获取成功",$list->total(),$list->items()); } /** * @title: 系统日志导出 * @param {string} {username} {非必填} {账户搜索} * @param {array} {daterange} {非必填} {时间区间} * @param {int} {org_id} {非必填} {组织id} * @return mixed * @Author: wangkewei * @Date: 2022/2/28 17:18 * @throws \Exception */ public function doExport() { $list = $this->model->where($this->createWhere()) ->with(['request','menu','createUser'=>function($query){ $query->with(['org'=>function($query_){ $query_->field('org_id,name')->append(['status_txt']); }])->field('user_id,name,org_id,status')->append(['status_txt']); }]) ->order("createtime DESC") ->select(); if($list->isEmpty()){ return res(2,'没有数据导出'); } $head = ['序号','操作用户','用户组织','访问路径','操作代码','操作内容','操作结果','IP地址','操作时间']; $body = []; foreach($list as $key=>$item){ $index = $key + 1; $body[$index][] = $item->id; $body[$index][] = $item->username; $body[$index][] = $item->createUser->org->name; $body[$index][] = $item->path; $body[$index][] = $item->code; $body[$index][] = $item->remark; $body[$index][] = $item->result_txt; $body[$index][] = $item->ip; $body[$index][] = $item->createtime; } //创建文件夹 $basepath = "uploads".DS."download".DS.date('Ymd'); $savepath = public_path().$basepath; if(!is_dir($savepath)){ mkdir($savepath,0777,true); } //保存文件 $filename = date('Ymd')."系统日志导出.xls"; $path = $savepath."/".$filename; $export_help = new \excel\MultiHeadExcel(); $export_help->export_help($body,$head,$path); //返回路径 $returnpath = WEBURL.DS.$basepath.DS.$filename; slog(1,"导出了系统日志"); return res(1,"获取成功",['url'=>$returnpath,'name'=>$filename]); } }