作者 黎建湛

云订单的导出表格数据异步输出到浏览器中

@@ -91,49 +91,41 @@ class DemoController extends Controller @@ -91,49 +91,41 @@ class DemoController extends Controller
91 // 用于测试接口实例测试 91 // 用于测试接口实例测试
92 public function actionTestdemoapi() 92 public function actionTestdemoapi()
93 { 93 {
94 - // 测试程序开始时间  
95 - /*  
96 - $date = date('Y-m-d h:i:s', time());  
97 - var_dump('程序开始时间测试: '.$date);  
98 - */ 94 + ini_set("memory_limit","1900M");
99 $input = Yii::$app->request->post(); 95 $input = Yii::$app->request->post();
100 -  
101 - // 测试程序结束时间  
102 - /*  
103 - $endDate = date('Y-m-d h:i:s', time());  
104 - var_dump($endDate);exit;  
105 - */  
106 - $data = $this->exportSurplusData($input['warehouse_id']);  
107 - $res['data'] = $this->exportSuccessData($data);  
108 -  
109 - //测试程序快慢  
110 - $i = 2;  
111 - foreach ($res['data'] as $k => $value) {  
112 - // 模拟循环语句测试  
113 - /*if ($k >0 ){  
114 - var_dump($k);  
115 - }*/  
116 -  
117 - foreach (['phone'] as $kk => $val) {  
118 - if ($kk > 0) {  
119 - var_dump($kk);exit;  
120 - }  
121 - var_dump($kk+10);exit;  
122 - // 输出每格数据  
123 - // $PHPSheet->setCellValue($english[$k] . $i, '' . $value[$val]);  
124 - }  
125 - $i++; 96 + $title = ['手机号码','粉丝性别'];
  97 + $name = '导出筛选数据信息';
  98 + $data = $this->DemoLogic->getTestScreenList($input['task_id']);
  99 +
  100 + set_time_limit(0);
  101 + $headerList = $title;
  102 + $fileName = date("Y-m-d") . "-".$name."数据.csv";
  103 + // 文件名称转码
  104 + $fileName = iconv('UTF-8', 'gbk', $fileName);
  105 + header("Content-type:text/csv");
  106 + header("Content-Disposition:attachment;filename=".$fileName);
  107 + header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
  108 + header('Expires:0');
  109 + header('Pragma:public');
  110 + //打开PHP文件句柄,php://output,表示直接输出到浏览器
  111 + $fp = fopen("php://output","a");
  112 +
  113 + //输出Excel列名信息
  114 + foreach ($headerList as $key => $value) {
  115 + //CSV的Excel支持GBK编码,一定要转换,否则乱码
  116 + $headerList[$key] = iconv('utf-8', 'gbk', $value);
126 } 117 }
127 - $endDate = date('Y-m-d h:i:s', time());  
128 - var_dump('程序结束时间测试: '.$endDate);exit; 118 + // 将数据格式化为CSV格式并写入到output流中
  119 + fputcsv($fp, $headerList);
129 120
130 - if (isset($res['data'])) {  
131 - $this->getExcel('导出剩余数据',['账号'],['phone'],$res['data']);  
132 - }else{  
133 - return "<script>alert('".$res."');window.opener = null;window.open('', '_self');window.close();</script>"; 121 + foreach ($data as $arr) {
  122 + $line[] = $arr['phone'];
  123 + $line[] = $arr['sex']."\t";
134 } 124 }
135 - $endDate = date('Y-m-d h:i:s', time());  
136 - var_dump('程序结束时间测试: '.$endDate);exit; 125 + fputcsv($fp, $line);
  126 +
  127 + fclose($fp);
  128 + exit();
137 } 129 }
138 130
139 public function getExcel($name,$title,$arr,$data,$width = false) { 131 public function getExcel($name,$title,$arr,$data,$width = false) {
@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
2 2
3 namespace app\dao; 3 namespace app\dao;
4 4
  5 +use app\models\TaskModel;
5 use Yii; 6 use Yii;
6 use app\models\DemoModel; 7 use app\models\DemoModel;
7 8
@@ -40,4 +41,8 @@ class DemoDao @@ -40,4 +41,8 @@ class DemoDao
40 public function delDemoData($where){ 41 public function delDemoData($where){
41 return $this->model::deleteAll($where); 42 return $this->model::deleteAll($where);
42 } 43 }
  44 +
  45 + public function getTestSreenInfo($and_where){
  46 + return TaskModel::find()->andWhere($and_where)->asArray()->all();
  47 + }
43 } 48 }
@@ -35,4 +35,9 @@ class DemoLogic @@ -35,4 +35,9 @@ class DemoLogic
35 public function delDemoData($input){ 35 public function delDemoData($input){
36 return $this->DemoDao->delDemoData($input); 36 return $this->DemoDao->delDemoData($input);
37 } 37 }
  38 +
  39 + public function getTestScreenList($id) {
  40 + $where = ['and', "task_id = {$id}"];
  41 + return $this->DemoDao->getTestSreenInfo($where);
  42 + }
38 } 43 }
  1 +<?php
  2 +
  3 +namespace app\models;
  4 +
  5 +use yii\base\Model;
  6 +use yii\db\ActiveRecord;
  7 +
  8 +class TaskModel extends ActiveRecord
  9 +{
  10 +
  11 + public static function tableName()
  12 + {
  13 + return '{{%cloud_task_filter_data}}';
  14 + }
  15 +}