DownAllFileController.class.php
4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?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;
}
}