DownAllFileController.class.php 4.9 KB
<?php
/**
 * 【调研中心-后台】24_下载问题的所有附件
 * DownAllFileController.class.php
 * CreateBy:dj
 * Date:2017-06-19
 */

namespace Frontend\Controller\Callback;

use Common\Common\ExportDownload;
use Common\Service\BaseinfoService;
use Common\Service\QuestionService;
use Common\Service\RecordService;
use Org\Net\PHPZip;
use Think\Log;

class DownAllFileController extends AbstractController
{

    // 免登录
    protected $_require_login = false;

    public function Index()
    {

        // 问题id
        $back = $this->callBackData;

        $this->user_id = $back['user_id'];
        $this->id = $back['id'];
        $this->time = $back['time'];
        $this->title = $back['title'];

        Log::write('=========生成图片进来了=====文件ID==' . $this->id, 'ERR', '', C('LOG_PATH') . date('y_m_d') . '.txt');

        // 参数验证
        if (empty($back['q_id'])) {

            return true;
        }

        // 调研回答信息表
        $question_s = new QuestionService();
        // 获取问题信息
        $data = $question_s->get($back['q_id']);
        if (empty($data)) {

            return true;
        }

        // 调研基本信息表
        $base_s = new BaseinfoService();
        // 获取调研基本信息
        $base_info = $base_s->get($data['qu_id']);

        // 调研填写信息表
        $record_s = new RecordService();
        // 调研填写记录详情列表
        $record_list = $record_s->list_by_where($back['q_id']);

        // 获取该试题的回答记录
        $result_data = $question_s->format_other_list($record_list, $page = 1, count($record_list),
            $base_info['anonymous']);

        if (empty($result_data)) {

            return true;
        }

        $pic_list = [];
        // 循环数据,格式化图片地址
        foreach ($result_data as $v) {

            $pic_data = [];
            $answer_option_data = [];

            if (!empty($v['answer_content'])) {
                // 反序列化答案数据
                $answer_option_data = unserialize($v['answer_content']);
            }

            // 此处需要处理答案中包含的图片的显示路径
            foreach ($answer_option_data as $_v) {

                if (!empty($_v['option_img'])) {

                    $pic_arr = [];
                    $pic_arr['imgUrl'] = imgUrl($_v['option_img']);
                    $pic_data[] = $pic_arr;
                }
            }

            // 处理部门信息
            if (empty($v['user_dp'])) {
                $key = $v['user_name'];
            } else {
                $key = $v['user_name'] . '(' . $v['user_dp'] . ')';
            }

            $pic_list[$key] = $pic_data;
        }

        if (empty($pic_list)) {

            return true;
        }

        // 图片临时存放目录
        $cur_file = ExportDownload::get_down_dir($this->user_id . microtime(true) . 'IMG');
        // 压缩包存放目录
        $save_path = ExportDownload::get_down_dir($this->user_id . microtime(true) . 'ZIP');

        // 组织文件上传地址
        $dir = $cur_file . $data['q_title'];
        if (!is_dir($dir)) {

            mkdir($dir, 0777);
        }

        foreach ($pic_list as $k => $v) {
            // 创建以用户姓名和部门为名称的目录
            $img_dir = $dir . D_S . $k;

            if (!is_dir($img_dir)) {

                mkdir($img_dir, 0777);
            }

            foreach ($v as $_v) {

                $img_data = $this->curl_file_get_contents($_v['imgUrl']);

                // 组织文件上传地址
                $file = $img_dir . D_S . uniqid() . '.jpg';

                // 将uc的图片保存到本地
                $success = file_put_contents($file, $img_data);
            }
        }

       // sleep(10);
        // 生成文件名


        $phpzip = new PHPZip();

        // 组装压缩文件名称
        $filename = $save_path . microtime(true) . '.zip';
        Log::write('=========生成ZIP=====目录==' . $filename, 'ERR', '', C('LOG_PATH') . date('y_m_d') . '.txt');

        // 打包文件
        $phpzip->zipDir($dir, $filename);

        if ($filename) {
            Log::write('=========生成图片=====文件ID==' . $this->id, 'ERR', '', C('LOG_PATH') . date('y_m_d') . '.txt');

            // 更新状态
            $data = [
                'url' => $dir,
                'id' => $this->id,
                'size' => filesize($filename)
            ];

            ExportDownload::update_down_load($data);

        }

       unlink($filename);

        return true;
    }

    // 获取远程图片
    private function curl_file_get_contents($durl)
    {

        $ch = curl_init();
        $timeout = 30;
        curl_setopt($ch, CURLOPT_URL, $durl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $img = curl_exec($ch);
        curl_close($ch);

        return $img;
    }
}