'阅读', '2' => '点赞', '3' => '分享', '4' => '推荐', ]; protected function _initialize(){ parent::_initialize(); $this->assign('breadcrumb1','资讯管理'); $this->assign('breadcrumb2','事件记录'); } /** * 数据列表 * * */ public function index(Model $Model){ // 接受参数 $articleId = request('article_id',''); $typeId = request('type_id'); $title = request('title'); $startTime = request('start_time',''); $endTime = request('end_time',''); // 查询条件 $map = []; // 编码ID if( $articleId ) $map[] = ['article_event.article_id','=',$articleId]; if( $title ) $map[] = ['article.title','LIKE','%'.$title.'%']; if( $startTime ) $map[] = ['article_event.insert_time','>=',strtotime($startTime)]; if( $endTime ) $map[] = ['article_event.insert_time','<=',strtotime($endTime)]; if( $typeId ) $map[] = ['article_event.type_id','=',$typeId]; // 查询数据 $list = $Model->query() ->join('custom','article_event.custom_uid','=','custom.uid') ->join('article','article_event.article_id','=','article.id')->where($map) ->orderByDesc('article_event.id') ->select([ 'article_event.id', 'article_event.status', 'article.title as title', 'article_event.type_id', 'article_event.article_id', 'custom.username as custom_name', 'custom.uid as custom_uid', 'custom.weiban_extid as weiban_extid', 'article_event.update_time', ]) ->paginate(request('limit',config('page_num',10)))->appends(request()->all()); // 循环处理数据 foreach ($list as $key => $value) { // 事件类型:1阅读;2点赞;3分享;4推荐 $value['event'] = isset(self::EVENT_TYPE[$value['type_id']]) ? self::EVENT_TYPE[$value['type_id']] : ''; $value['event'] = $value['event'] ? ($value['status']?'取消':'').$value['event'] : ''; // 重组 $list[$key] = $value; } // 分配数据 $this->assign('empty', '~~暂无数据'); $this->assign('list', $list); // 加载模板 return $this->fetch(); } /** * 导出表格导入 * * */ public function down_excel(Model $Model,Custom $Custom){ // 接受参数 $title = request('title'); $typeId = request('type_id'); $startTime = request('start_time',''); $endTime = request('end_time',''); // 查询条件 $map = []; // 编码ID if( $title ) $map[] = ['article.title','LIKE','%'.$title.'%']; if( $startTime ) $map[] = ['article_event.insert_time','>=',strtotime($startTime)]; if( $endTime ) $map[] = ['article_event.insert_time','<=',strtotime($endTime)]; if( $typeId ) $map[] = ['article_event.type_id','=',$typeId]; // 查询数据 $list = $Model->query() ->join('custom','article_event.custom_uid','=','custom.uid') ->join('article','article_event.article_id','=','article.id')->where($map) ->orderByDesc('article_event.id') ->select([ 'custom.uid as custom_uid', 'custom.username as custom_name', 'article_event.article_id', 'article.title as title', 'article_event.status', 'article_event.type_id', 'custom.weiban_extid as weiban_extid', 'article_event.update_time', ])->get()->toArray(); // 返回结果 $data = []; // 循环处理数据 foreach ($list as $value) { // id转编号 $value['custom_uid'] = $Custom->idToCode($value['custom_uid']); // 事件类型:1阅读;2点赞;3分享;4推荐 $value['type_id'] = isset(self::EVENT_TYPE[$value['type_id']]) ? self::EVENT_TYPE[$value['type_id']] : ''; $value['type_id'] = $value['type_id'] ? ($value['status']?'取消':'').$value['type_id'] : ''; $value['update_time'] = $value['update_time']? date('Y-m-d H:i:s',$value['update_time']) : ''; // 删除数据 unset($value['status']); // 重组 $data[] = $value; } try { // 去下载 $this->toDown($data); } catch (\Throwable $th) { echo $th->getMessage(); } } /** * 去下载 */ private function toDown($data){ try { $config = [ 'path' => public_path().'/uploads/' // xlsx文件保存路径 ]; $excel = new \Vtiful\Kernel\Excel($config); $header = [ '客户编码', '客户昵称', '资讯ID', '资讯标题', '操作类型', '微伴ID', '操作时间', ]; $filePath = $excel->fileName( uniqid().'.xlsx', 'sheet1') ->header($header) ->data($data) ->output(); $filename = '资讯用户轨迹下载.xlsx'; header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); header('Content-Disposition: attachment;filename="' . $filename . '"'); header('Content-Length: ' . filesize($filePath)); header('Content-Transfer-Encoding: binary'); header('Cache-Control: must-revalidate'); header('Cache-Control: max-age=0'); header('Pragma: public'); ob_clean(); flush(); if (copy($filePath, 'php://output') === false) { dd('下载出错'); } @unlink($filePath); exit(); }catch (\Throwable $th) { return $th->getMessage(); } } }