ThinkPHP 3导出数据防止内存泄露

一、php代码

/**
    * 分页导出数据(总是顺序导出).
    * 返回的是 json, 由前端 js 生成 csv/xls 文件.
    * @param int $page 当前页数
    * @param int $limit 当前导出的记录数目 (这个值不能太大, 否则会造成内存超出限制)
    */
   public function export_json($page = 1, $limit = 2000)
   {
      $this->checkIndexPermission();
      if (!$this->admin->canExportGoods()) {
         exit;
      }
      
      $exportType = null;
      $goods = D('Goods');
      $where = $this->getSearchOptions();
      $count = $goods->alias('a')->where($where)->count();
      $totalPage = ceil($count / $limit);
      $list = $goods->alias('a')
         ->join('LEFT JOIN goods_trace b ON a.id = b.gid')
         ->field('a.*,b.record_time')
         ->where($where)
         ->page($page, $limit)->order('a.id desc')->select();
      $titles = $this->getExportFieldNames($exportType);
      $rows = $this->exportToArray($list);
      $data = array(
         'titles' => array_values($titles), // 标题
         'rows' => $rows, // 数据列表
         'percentage' => (int)(($page / $totalPage) * 100), // 当前总进度
      );

      // 如果不是最后一页, 就加一个 next_url 的字段, 前端根据这个字段继续请求下一页数据
      if ($page < $totalPage) {
         $search = I('get.');
         $search['page'] = $page + 1;
         $data['next_url'] = U('export_json', $search);
      }
      $this->ajaxReturn($data);
   }
复制代码
 /***
    *  导出字段名称
    */
   public function getExportFieldNames()
   {
      $allFields = array(
         // 字段名 => 标题名称
         'id' => '订单id',
         'dian' => '店铺',
         'utime' => '进店时间',
         'wang' => '网销',
         'name' => '客户姓名',
         'phone' => '客户电话',
         'name1' => '客户姓名1',
         'phone1' => '客户电话1',
         'wx' => '微信',
         'is_contacted' => '是否联系',
         'men' => '门市',
         'record_time' => '走单时间',
         'jiage' => '标准套系',
         'sfje' => '实付金额',
         'kefu' => '客服',
         'beizhu' => '备注',
         'ctime' => '录入时间',
      );

      return $allFields;
   }
复制代码
/**
    * 获取 字段映射 数组.
    * [数据库中的字段名 => 导出的字段名]
    * @return array
    */
   public function exportToArray($list)
   {
      $res = array();
      foreach ($list as $key=>$data) {
         $res[$key] = array_values(array(
            'id' => $data['id'],
            'dian' => $data['dian'],
            'utime' => $data['utime'],
            'wang' => $data['wang'],
            'name' => $data['name'],
            'phone' => $data['phone'],
            'name1' => $data['name1'],
            'phone1' => $data['phone1'],
            'wx' => $data['wx'],
            'is_contacted' => $data['is_contacted'] == 1 ? '是' : '否',
            'men' => $data['men'],
            'record_time' => $data['record_time'],
            'jiage' => $data['jiage'],
            'sfje' => $data['sfje'],
            'kefu' => $data['kefu'],
            'beizhu' => $data['beizhu'],
            'ctime' => $data['ctime'],
         ));
      }
      return $res;
   }

复制代码

二、html代码

"export-data-btn" class="btn btn-warning" data-href="{:U('keyun/list/export_json', array_merge($search_options))}">数据导出

"modal fade" id="export-modal" tabindex="-1" role="dialog" data-backdrop="static">
"modal-dialog" role="document">
"modal-content">
"modal-header">

"modal-title" id="myModalLabel">数据导出

"modal-body">

数据导出中, 请勿刷新或关闭浏览器

"progress">
"export-progress" class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%;">
复制代码

你可能感兴趣的:(ThinkPHP 3导出数据防止内存泄露)