UploadImgController.class.php
4.34 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
<?php
/**
* 图片上传
*
* 由于UC获取临时素材是异步的,所以前端至少会请求2次。分别是上传和请求图片地址。
* 参数atId不为空并且wxid不为空时,是轮询请求图片地址。
* 参数atId为空并且wxid不为空时,是上传临时图片素材
*
* Created by PhpStorm.
* User: mr.song
* Date: 2016/7/22
* Time: 16:49
*/
namespace Api\Controller\Attachment;
use VcySDK\Service;
use VcySDK\Attach;
use Common\Common\Attach as CommonAttach;
class UploadImgController extends AbstractController
{
protected $_require_login = false;
/**
* VcySDK 附件操作类
*
* @type Attach
*/
protected $_attach;
/**
* 微信素材ID
*
* @var string
*/
protected $_wxid;
/**
* 附件ID
*
* @var string
*/
protected $_atId;
/**
* 企业标识
*
* @var string
*/
protected $_identifier;
public function before_action($action = '')
{
if (! parent::before_action($action)) {
return false;
}
$serv = &Service::instance();
$this->_attach = new Attach($serv);
return true;
}
public function Index()
{
// 微信文件ID
$this->_wxid = I('post.wxid');
// 附件ID
$this->_atId = I('post.atId');
// 企业标识
$this->_identifier = I('post._identifier');
// 参数错误
if (empty($this->_wxid)) {
$this->_set_error('_ERR_PARAMS_UNALLOWED');
return false;
}
/**
* 根据参数判断是什么业务,进行相应的处理
* -- 附件ID参数不为空时,是获取附件信息操作
* -- 附件ID为空时, 是上传附件
*/
if (empty($this->_atId)) {
$this->_upload();
} else {
$this->_getDetail();
}
return true;
}
/**
* 上传临时图片素材到UC
*
* @return array 返回UC返回的参数
*/
protected function _upload()
{
$params = array(
'memUid' => empty($this->_login->user) ? null : $this->_login->user['memUid'],
'mediaId' => $this->_wxid,
'fileType' => Attach::TYPE_IMG,
'plIdentifier' => $this->_identifier,
'callbackUrl' => oaUrl('Api/Attachment/UploadCallback', array(), '', 'Public') . '?_identifier=' . $this->_identifier
);
$attach = $this->_attach->getMedia($params);
// 上传错误
if (empty($attach)) {
$this->_set_error('_ERR_UPLOAD_ERROR');
}
// 缓存标记
CommonAttach::instance()->setWaitingFlag($attach['atId']);
$this->_result = array(
'wxid' => $this->_wxid,
'atId' => $attach['atId'],
'atMqStatus' => 0
);
return true;
}
/**
* 获取附件详情
*
* @return array
*/
protected function _getDetail()
{
$flag = CommonAttach::instance()->getFlag($this->_atId);
// 如果已经通知了, 并且附件处理出错
if (CommonAttach::FLAG_ERROR == $flag) {
$this->_result = array(
'atId' => $this->_atId,
'atMqStatus' => 2
);
return true;
} elseif (CommonAttach::FLAG_WAITING == $flag) { // 上传中
$this->_result = array(
'atId' => $this->_atId,
'atMqStatus' => 0
);
return true;
}
// 获取附件信息
$attach = $this->_attach->fetch(array(
'atId' => $this->_atId
));
// 非图片附件
if (stripos($attach['atAttachment'], '/image/') === false) {
$this->_result = array(
'wxid' => $this->_wxid,
'atId' => $this->_atId,
'atMqStatus' => 0
);
} else {
$this->_result = array(
'wxid' => $this->_wxid,
'atId' => $attach['atId'],
'atFilename' => $attach['atFilename'],
'atFilesize' => formatBytes($attach['atFilesize']),
'atAttachment' => imgUrl($attach['atId']),
'atMqStatus' => 1,
'atCreated' => $attach['atCreated']
);
}
return true;
}
}