Attachment.class.php
3.88 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
<?php
/**
* Attachment.php
* 附件操作类
* @author Deepseath
* @version $Id$
* @copyright vchangyi.com
*/
namespace Com;
use VcySDK\Attach;
use VcySDK\Service as VcySDKService;
class Attachment
{
/**
* 文件类型:图片
*/
const FILE_TYPE_IMAGE = 1;
/**
* 文件类型:语音
*/
const FILE_TYPE_VOICE = 2;
/**
* VcySDK 附件操作类
*/
protected $_attach = null;
/**
* 构造方法
*/
public function __construct()
{
$serv = &VcySDKService::instance();
$this->_attach = new Attach($serv);
}
/**
* 实例化
*
* @return \Com\Attachment
*/
public static function &instance()
{
static $instance;
if (empty($instance)) {
$instance = new self();
}
return $instance;
}
/**
* 上传图片
*
* @param string $uid 用户 ID
* @param string $wxMid 微信文件 ID
* @return array
*/
public function uploadImage($uid, $wxMid)
{
$params = array(
'memUid' => $uid,
'mediaId' => $wxMid,
'fileType' => self::FILE_TYPE_IMAGE
);
return $this->_attach->getMedia($params);
}
/**
* 上传语音
*
* @param string $uid 用户 ID
* @param string $wxMid 微信文件 ID
* @param string $callback 回调地址
* @return boolean
*/
public function uploadVoice($uid, $wxMid, $callback)
{
$params = array(
'memUid' => $uid,
'mediaId' => $wxMid,
'fileType' => self::FILE_TYPE_VOICE,
'callbackUrl' => $callback
);
return $this->_attach->getMedia($params);
}
/**
* 根据附件 ID 获取附件 Url
*
* @param string $atId 附件 ID
* @return string
*/
public function getAttachUrl($aid)
{
$result = $this->_attach->fetch(array('atId' => $aid));
return $result['atAttachment'];
}
/**
* 根据附件 ID 获取附件信息
*
* @param string $aid 附件ID
* @return string
*/
public function getAttachDetail($aid)
{
return $this->_attach->fetch(array('atId' => $aid));
}
/**
* 根据附件 ID 数组获取附件 Url
* @param array $aids 附件ID数组
* @return array
*/
public function listAttachUrl($aids)
{
$aids = settype($aids, 'array');
// 查询SDK
$result = $this->_attach->listAll(['atIds' => $aids]);
if (empty($result)) {
return [];
}
return array_combine_by_key($result, 'atId');
}
/**
* 下载附件文件
* @param string $atId 附件 ID
* @return boolean
*/
public function downloadAttachmentByAid($atId = '')
{
// 获取附件的详细信息
$attachDetail = $this->getAttachDetail($atId);
if (empty($attachDetail)) {
return false;
}
// 附件原始文件名,转换编码为 GBK
$showname = iconv('UTF-8', 'GBK//IGNORE', $attachDetail['atFilename']);
// 发送 Http Header 信息 开始下载
header('Pragma: public');
header('Cache-control: max-age=' . 180);
// header('Cache-Control: no-store, no-cache, must-revalidate');
header('Expires: ' . gmdate('D, d M Y H:i:s', NOW_TIME + 86400 * 7) . 'GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $attachDetail['atCreated']) . 'GMT');
header('Content-Disposition: attachment; filename=' . $showname);
header('Content-Length: ' . $attachDetail['atFilesize']);
header('Content-type: application/octet-stream');
header('Content-Encoding: none');
header('Content-Transfer-Encoding: binary');
// 读取远程文件输出
readfile($attachDetail['atAttachment']);
exit();
}
}