UploadController.class.php
7.51 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/**
* 【考试中心-手机端】媒体文件上传或取消
* UploadController.class.php
* @author: 蔡建华
* @date: 2017-05-23
*/
namespace Api\Controller\Answer;
use Common\Common\AttachOperation;
use VcySDK\Service;
use VcySDK\Attach;
use Common\Service\AnswerDetailService;
use Common\Service\AnswerAttachService;
class UploadController extends AbstractController
{
/** @var string 文件信息 */
private $__file_info = '';
/** @var Attach 附件信息 */
private $__attach = null;
/** @var AnswerAttachService */
protected $answer_attach_serv;
public function before_action($action = '')
{
if (!parent::before_action($action)) {
return false;
}
// 初始化附件表service
$this->answer_attach_serv = new AnswerAttachService();
return true;
}
public function Index_post()
{
// 接收post参数
$params = I('post.');
// 答题id
$ead_id = intval($params['ead_id']);
// 答题id为空,返回提示"题目ID不能为空"
if (!$ead_id) {
E('_ERR_ECT_ID_EMPTY_FOR_VOICE');
}
// 语音顺序编号
$order_id = $params['order_id'];
// 语音顺序编号为空,返回提示"语音顺序编号"
if (!$order_id) {
E('_ERR_ID_ORDER_FOR_VOICE');
}
// 实例化答卷详情service
$answer_detail_s = new AnswerDetailService();
// 根据答题id获取答题详情数据
$data = $answer_detail_s->get($ead_id);
// 答题详情数据为空,返回提示"题目信息不存在"
if (empty($data)) {
E('_ERR_COUNT_DETAIL_EMPTY_FOR_VOICE');
}
// 媒体文件id为空
if (empty($params['media_id'])) {
// 移除媒体文件
$result = $this->remove($params, $data['ea_id']);
// 移除失败
if (!$result) {
return false;
}
} else {
// 上传媒体文件
$result = $this->add_media($params, $data['ea_id'], $data['ep_id']);
// 上次失败
if ($result) {
return false;
}
}
return true;
}
/**
* 上传媒体文件
*
* @param array $params
* @param int $ea_id 答卷id
* @param int $ep_id 试卷id
*
* @return mixed
*/
private function add_media($params = [], $ea_id = 0, $ep_id = 0)
{
// 媒体文件id为空或者不是一个标量,返回提示"语音文件不存在"
if (empty($params['media_id']) || !is_scalar($params['media_id'])) {
E('_ERR_VOICE_UPLOAD_MEDIA_IS_EMPTY');
}
// 初始化
$serv = &Service::instance();
$this->__attach = new Attach($serv);
$is_complete = AnswerAttachService::IS_COMPLETE_NO;
$at_id = '';
$file_type = '';
// 文件类型:音频
if (AnswerAttachService::TYPE_VOICE == $params['type']) {
// 新增音频文件
$at_id = $this->add_voice($params);
if (!$at_id) {
return false;
}
// 文件类型:音频
$file_type = AnswerAttachService::TYPE_VOICE;
// 文件没有转换完毕
$is_complete = AnswerAttachService::IS_COMPLETE_NO;
} elseif (AnswerAttachService::TYPE_IMAGE == $params['type']) {
// 文件类型:图片,新增图片文件
$at_id = $this->add_image($params);
if (!$at_id) {
return false;
}
// 文件类型:图片
$file_type = AnswerAttachService::TYPE_IMAGE;
// 文件转换完毕
$is_complete = AnswerAttachService::IS_COMPLETE_YES;
} else {
// 返回提示"未知的媒体文件类型"
E('_ERR_NOT_KNOW_MEDIA_TYPE');
}
// 待存数据
$data = [
'ead_id' => $params['ead_id'],
'ea_id' => $ea_id,
'order_id' => $params['order_id'],
'media_id' => $params['media_id'],
'is_complete' => $is_complete,
'at_id' => $at_id,
'type' => $file_type,
'file_info' => serialize($this->__file_info)
];
// 附件处理
$attach_serv = new AttachOperation();
$attach_serv->insert_attach(
APP_DIR,
'paper',
$ep_id,
['attach_ids' => [$at_id]]
);
// 写入附件表
return $this->answer_attach_serv->insert($data);
}
/**
* 移除媒体文件
*
* @param array $params
* @param int $ea_id 答卷id
*
* @return bool
*/
private function remove($params = [], $ea_id = 0)
{
// 删除条件:答题id,答卷id,顺序编号
$del_conds = [
'ead_id' => $params['ead_id'],
'ea_id' => $ea_id,
'order_id' => $params['order_id']
];
// 获取附件ID
$attach = $this->answer_attach_serv->get_by_conds($del_conds);
// 删除UC附件服务器的附件文件
$attach_serv = new AttachOperation();
$attach_serv->deleteFile([$attach['at_id']]);
// 移除附件
return $this->answer_attach_serv->delete_by_conds($del_conds);
}
/**
* 新增语音文件
*
* @param array $params
*
* @return bool|string
*/
private function add_voice($params = [])
{
// 文件信息
$this->__file_info = $params['info'];
// 语音时长未设置,返回提示"语音文件需指定时长"
if (!isset($this->__file_info['length'])) {
E('_ERR_UPLOAD_VOICE_LENGTH_ERROR');
}
// 重新整理文件信息
$this->__file_info = [
'length' => $this->__file_info['length']
];
// 回调URL参数
$oaurl_params = [
'ead_id' => $params['ead_id'],
'order_id' => $params['order_id']
];
// 转换完毕后的回调 URL
$url = oaUrl('/Frontend/Callback/Upload/Index', $oaurl_params);
// 获取微信服务号多媒体信息条件
$attach_conds = [
'memUid' => $this->uid,
'mediaId' => $params['media_id'],
'fileType' => Attach::TYPE_AUDIO,
'callbackUrl' => $url
];
// 获取微信服务号多媒体信息
$result = $this->__attach->getMedia($attach_conds);
// 获取微信服务号多媒体信息为空,返回提示"语音答题文件上传失败"
if (!$result || empty($result['atId'])) {
E('_ERR_UPLOAD_VOICE_FAILED');
}
return $result['atId'];
}
/**
* 新增图片文件
*
* @param $params
*
* @return bool|string
*/
private function add_image($params)
{
// 文件信息初始化
$this->__file_info = [];
// 获取微信服务号多媒体信息条件
$attach_conds = [
'memUid' => $this->uid,
'mediaId' => $params['media_id'],
'fileType' => Attach::TYPE_IMG
];
// 获取微信服务号多媒体信息
$result = $this->__attach->getMedia($attach_conds);
// 获取微信服务号多媒体信息为空,返回提示"图片答题文件上传失败"
if (!$result || empty($result['atId'])) {
E('_ERR_UPLOAD_IMAGE_FAILED');
}
return $result['atId'];
}
}