From fbead8c3a11b42fd0d03dd2da602bfe1560244bf Mon Sep 17 00:00:00 2001 From: zhengyongxing Date: Wed, 4 Dec 2019 17:51:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A8=E5=B9=BF=E8=A1=A5=E9=93=BE=E5=AF=BC?= =?UTF-8?q?=E5=87=BA=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controller/ExportController.class.php | 139 +++++++++++++----- 1 file changed, 102 insertions(+), 37 deletions(-) diff --git a/Application/Admin/Controller/ExportController.class.php b/Application/Admin/Controller/ExportController.class.php index 8809d0684..32f8c5da1 100644 --- a/Application/Admin/Controller/ExportController.class.php +++ b/Application/Admin/Controller/ExportController.class.php @@ -5414,57 +5414,122 @@ if ($key == 'model'){ $model = M('user','tab_'); - $xlsCell = array( - array('account','玩家账号'), - array('register_time','注册时间'), - array('login_time','最后登录时间'), - array('register_type','注册来源'), - array('register_ip','注册IP'), - array('login_time','上次登录'), - array('top_promote','所属推广员'), - ); +// $xlsCell = array( +// array('account','玩家账号'), +// array('register_time','注册时间'), +// array('login_time','最后登录时间'), +// array('register_type','注册来源'), +// array('register_ip','注册IP'), +// array('login_time','上次登录'), +// array('top_promote','所属推广员'), +// ); + $xlsCell = array( + '玩家账号','注册时间','最后登录时间','注册来源','注册IP','上次登录','所属推广员', + ); if(isset($_REQUEST['account'])){ if ($_REQUEST['account']=='全部') { unset($_REQUEST['account']); } - $map['account']=array('like','%'.$_REQUEST['account'].'%'); + $map['tab_user.account']=array('like','%'.$_REQUEST['account'].'%'); unset($_REQUEST['account']); } - $page = intval($_REQUEST['p']); - - $page = $page?$page:1; - - $row = $_REQUEST['row']?$_REQUEST['row']:10; - - - $data=$model - ->where($map) - ->order('id desc') - ->select(); - - $xlsData = []; - - if(is_array($data)){ - - foreach($data as $k => $v) { - - $v['register_time'] = date('Y-m-d H:i:s',$v['register_time']); - $v['login_time'] = $v['login_time']>0?date('Y-m-d H:i:s',$v['login_time']):'暂无登录'; - - $v['register_type'] = get_registertype($v['register_type']); + $csvFileName = $xlsName.'.csv'; + //设置好告诉浏览器要下载excel文件的headers + header('Content-Description: File Transfer'); + header('Content-Type: application/vnd.ms-excel'); + header('Content-Disposition: attachment; filename="'. $csvFileName .'"'); + header('Expires: 0'); + header('Cache-Control: must-revalidate'); + header('Pragma: public'); + $fp = fopen('php://output', 'a');//打开output流 + mb_convert_variables('GBK', 'UTF-8', $xlsCell); + fputcsv($fp, $xlsCell);//将数据格式化为CSV格式并写入到output流中 + + $accessNum=$model + ->join("left join tab_promote on tab_promote.id=tab_user.promote_id") + ->field("tab_user.id,tab_user.account,tab_user.register_time,tab_user.login_time,tab_user.register_type + ,tab_user.register_ip, + tab_promote.parent_name as parent_name, + tab_promote.account as promote_account") + ->where($map) + ->order('tab_user.id desc') + ->count(); + + $perSize = 5000;//每次查询的条数 + $pages = ceil($accessNum / $perSize); + $lastId = 0; + + for($i = 1; $i <= $pages; $i++) { + // REPLACE(substring_index(tab_promote.chain, '/', 2),'/','') + $xlsData=$model + ->join("left join tab_promote on tab_promote.id=tab_user.promote_id") + ->field("tab_user.id,tab_user.account,tab_user.register_time,tab_user.login_time,tab_user.register_type + ,tab_user.register_ip,tab_user.login_time as last_login_time, + tab_promote.parent_name as parent_name, + tab_promote.account as promote_account,tab_user.id as top_promote") + ->where($map) + ->limit(($i-1)*$perSize ,$perSize) + ->order('tab_user.id desc') + ->select(); - $promote = get_parent_promoteto($v['promote_id']); + foreach($xlsData as $key => $value) { + unset($value['id']); + $value['register_time'] = date('Y-m-d H:i:s',$value['register_time']); + $value['login_time'] = $value['login_time']>0?date('Y-m-d H:i:s',$value['login_time']):'暂无登录'; + $value['last_login_time'] = $value['last_login_time']>0?date('Y-m-d H:i:s',$value['last_login_time']):'暂无登录'; + $value['register_type'] = get_registertype($value['register_type']); + $value['top_promote'] = $value['parent_name']?$value['promote_account'].'['.$value['parent_name'].']':$value['promote_account']; + if (!$value['promote_account']) { + $value['top_promote'] = "官方渠道"; + } + if($value['parent_name'] == "官方渠道") { + $value['top_promote'] = $value['promote_account']; + } + unset($value['promote_account']); + unset($value['parent_name']); + mb_convert_variables('GBK', 'UTF-8', $value); + fputcsv($fp, $value); - $v['top_promote'] = $promote?($v['promote_account'] . get_parent_promoteto($v['promote_id'])):get_promote_name($v['promote_id']); + } + unset($xlsData);//释放变量的内存 + //刷新输出缓冲到浏览器 + ob_flush(); + flush();//必须同时使用 ob_flush() 和flush() 函数来刷新输出缓冲。 + } + fclose($fp); + exit(); - $xlsData[] = $v; +// $data = M()->table("({$data}) as a") +// ->field("a.account,a.register_time,a.login_time,a.register_type,a.register_ip,a.top_promote,a.promote_account") +// ->join("left join tab_promote on a.top_promote=tab_promote.id") +// ->order("a.id desc") +// ->select(false); - } +// var_dump($data);die(); +// $xlsData = []; - } +// if(is_array($data)){ +// +// foreach($data as $k => $v) { +// +// $v['register_time'] = date('Y-m-d H:i:s',$v['register_time']); +// $v['login_time'] = $v['login_time']>0?date('Y-m-d H:i:s',$v['login_time']):'暂无登录'; +// +// $v['register_type'] = get_registertype($v['register_type']); +// +// $promote = get_parent_promoteto($v['promote_id']); +// +// $v['top_promote'] = $promote?($v['promote_account'] . get_parent_promoteto($v['promote_id'])):get_promote_name($v['promote_id']); +// +// $xlsData[] = $v; +// +// } +// +// +// } $this->exportExcel($xlsName, $xlsCell, $xlsData);