forked from silenceper/wechat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
material.go
350 lines (303 loc) · 9.21 KB
/
material.go
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package material
import (
"encoding/json"
"errors"
"fmt"
"github.com/silenceper/wechat/v2/officialaccount/context"
"github.com/silenceper/wechat/v2/util"
)
const (
addNewsURL = "https://api.weixin.qq.com/cgi-bin/material/add_news"
updateNewsURL = "https://api.weixin.qq.com/cgi-bin/material/update_news"
addMaterialURL = "https://api.weixin.qq.com/cgi-bin/material/add_material"
delMaterialURL = "https://api.weixin.qq.com/cgi-bin/material/del_material"
getMaterialURL = "https://api.weixin.qq.com/cgi-bin/material/get_material"
getMaterialCountURL = "https://api.weixin.qq.com/cgi-bin/material/get_materialcount"
batchGetMaterialURL = "https://api.weixin.qq.com/cgi-bin/material/batchget_material"
)
//PermanentMaterialType 永久素材类型
type PermanentMaterialType string
const (
//PermanentMaterialTypeImage 永久素材图片类型(image)
PermanentMaterialTypeImage PermanentMaterialType = "image"
//PermanentMaterialTypeVideo 永久素材视频类型(video)
PermanentMaterialTypeVideo PermanentMaterialType = "video"
//PermanentMaterialTypeVoice 永久素材语音类型 (voice)
PermanentMaterialTypeVoice PermanentMaterialType = "voice"
//PermanentMaterialTypeNews 永久素材图文类型(news)
PermanentMaterialTypeNews PermanentMaterialType = "news"
)
//Material 素材管理
type Material struct {
*context.Context
}
//NewMaterial init
func NewMaterial(context *context.Context) *Material {
material := new(Material)
material.Context = context
return material
}
//Article 永久图文素材
type Article struct {
Title string `json:"title"`
ThumbMediaID string `json:"thumb_media_id"`
ThumbURL string `json:"thumb_url"`
Author string `json:"author"`
Digest string `json:"digest"`
ShowCoverPic int `json:"show_cover_pic"`
Content string `json:"content"`
ContentSourceURL string `json:"content_source_url"`
URL string `json:"url"`
DownURL string `json:"down_url"`
}
// GetNews 获取/下载永久素材
func (material *Material) GetNews(id string) ([]*Article, error) {
accessToken, err := material.GetAccessToken()
if err != nil {
return nil, err
}
uri := fmt.Sprintf("%s?access_token=%s", getMaterialURL, accessToken)
var req struct {
MediaID string `json:"media_id"`
}
req.MediaID = id
responseBytes, err := util.PostJSON(uri, req)
if err != nil {
return nil, err
}
var res struct {
NewsItem []*Article `json:"news_item"`
}
err = json.Unmarshal(responseBytes, &res)
if err != nil {
return nil, err
}
return res.NewsItem, nil
}
//reqArticles 永久性图文素材请求信息
type reqArticles struct {
Articles []*Article `json:"articles"`
}
//resArticles 永久性图文素材返回结果
type resArticles struct {
util.CommonError
MediaID string `json:"media_id"`
}
//AddNews 新增永久图文素材
func (material *Material) AddNews(articles []*Article) (mediaID string, err error) {
req := &reqArticles{articles}
var accessToken string
accessToken, err = material.GetAccessToken()
if err != nil {
return
}
uri := fmt.Sprintf("%s?access_token=%s", addNewsURL, accessToken)
responseBytes, err := util.PostJSON(uri, req)
if err != nil {
return
}
var res resArticles
err = json.Unmarshal(responseBytes, &res)
if err != nil {
return
}
mediaID = res.MediaID
return
}
//reqUpdateArticle 更新永久性图文素材请求信息
type reqUpdateArticle struct {
MediaID string `json:"media_id"`
Index int64 `json:"index"`
Articles *Article `json:"articles"`
}
// UpdateNews 更新永久图文素材
func (material *Material) UpdateNews(article *Article, mediaID string, index int64) (err error) {
req := &reqUpdateArticle{mediaID, index, article}
var accessToken string
accessToken, err = material.GetAccessToken()
if err != nil {
return
}
uri := fmt.Sprintf("%s?access_token=%s", updateNewsURL, accessToken)
var response []byte
response, err = util.PostJSON(uri, req)
if err != nil {
return
}
return util.DecodeWithCommonError(response, "UpdateNews")
}
//resAddMaterial 永久性素材上传返回的结果
type resAddMaterial struct {
util.CommonError
MediaID string `json:"media_id"`
URL string `json:"url"`
}
//AddMaterial 上传永久性素材(处理视频需要单独上传)
func (material *Material) AddMaterial(mediaType MediaType, filename string) (mediaID string, url string, err error) {
if mediaType == MediaTypeVideo {
err = errors.New("永久视频素材上传使用 AddVideo 方法")
return
}
var accessToken string
accessToken, err = material.GetAccessToken()
if err != nil {
return
}
uri := fmt.Sprintf("%s?access_token=%s&type=%s", addMaterialURL, accessToken, mediaType)
var response []byte
response, err = util.PostFile("media", filename, uri)
if err != nil {
return
}
var resMaterial resAddMaterial
err = json.Unmarshal(response, &resMaterial)
if err != nil {
return
}
if resMaterial.ErrCode != 0 {
err = fmt.Errorf("AddMaterial error : errcode=%v , errmsg=%v", resMaterial.ErrCode, resMaterial.ErrMsg)
return
}
mediaID = resMaterial.MediaID
url = resMaterial.URL
return
}
type reqVideo struct {
Title string `json:"title"`
Introduction string `json:"introduction"`
}
//AddVideo 永久视频素材文件上传
func (material *Material) AddVideo(filename, title, introduction string) (mediaID string, url string, err error) {
var accessToken string
accessToken, err = material.GetAccessToken()
if err != nil {
return
}
uri := fmt.Sprintf("%s?access_token=%s&type=video", addMaterialURL, accessToken)
videoDesc := &reqVideo{
Title: title,
Introduction: introduction,
}
var fieldValue []byte
fieldValue, err = json.Marshal(videoDesc)
if err != nil {
return
}
fields := []util.MultipartFormField{
{
IsFile: true,
Fieldname: "media",
Filename: filename,
},
{
IsFile: false,
Fieldname: "description",
Value: fieldValue,
},
}
var response []byte
response, err = util.PostMultipartForm(fields, uri)
if err != nil {
return
}
var resMaterial resAddMaterial
err = json.Unmarshal(response, &resMaterial)
if err != nil {
return
}
if resMaterial.ErrCode != 0 {
err = fmt.Errorf("AddMaterial error : errcode=%v , errmsg=%v", resMaterial.ErrCode, resMaterial.ErrMsg)
return
}
mediaID = resMaterial.MediaID
url = resMaterial.URL
return
}
type reqDeleteMaterial struct {
MediaID string `json:"media_id"`
}
//DeleteMaterial 删除永久素材
func (material *Material) DeleteMaterial(mediaID string) error {
accessToken, err := material.GetAccessToken()
if err != nil {
return err
}
uri := fmt.Sprintf("%s?access_token=%s", delMaterialURL, accessToken)
response, err := util.PostJSON(uri, reqDeleteMaterial{mediaID})
if err != nil {
return err
}
return util.DecodeWithCommonError(response, "DeleteMaterial")
}
//ArticleList 永久素材列表
type ArticleList struct {
TotalCount int64 `json:"total_count"`
ItemCount int64 `json:"item_count"`
Item []ArticleListItem `json:"item"`
}
//ArticleListItem 用于ArticleList的item节点
type ArticleListItem struct {
MediaID string `json:"media_id"`
Content ArticleListContent `json:"content"`
Name string `json:"name"`
URL string `json:"url"`
UpdateTime int64 `json:"update_time"`
}
//ArticleListContent 用于ArticleListItem的content节点
type ArticleListContent struct {
NewsItem []Article `json:"news_item"`
UpdateTime int64 `json:"update_time"`
CreateTime int64 `json:"create_time"`
}
//reqBatchGetMaterial BatchGetMaterial请求参数
type reqBatchGetMaterial struct {
Type PermanentMaterialType `json:"type"`
Count int64 `json:"count"`
Offset int64 `json:"offset"`
}
// BatchGetMaterial 批量获取永久素材
//reference:https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Get_materials_list.html
func (material *Material) BatchGetMaterial(permanentMaterialType PermanentMaterialType, offset, count int64) (list ArticleList, err error) {
var accessToken string
accessToken, err = material.GetAccessToken()
if err != nil {
return
}
uri := fmt.Sprintf("%s?access_token=%s", batchGetMaterialURL, accessToken)
req := reqBatchGetMaterial{
Type: permanentMaterialType,
Offset: offset,
Count: count,
}
var response []byte
response, err = util.PostJSON(uri, req)
if err != nil {
return
}
err = util.DecodeWithError(response, &list, "BatchGetMaterial")
return
}
// ResMaterialCount 素材总数
type ResMaterialCount struct {
util.CommonError
VoiceCount int64 `json:"voice_count"` // 语音总数量
VideoCount int64 `json:"video_count"` // 视频总数量
ImageCount int64 `json:"image_count"` // 图片总数量
NewsCount int64 `json:"news_count"` // 图文总数量
}
// GetMaterialCount 获取素材总数.
func (material *Material) GetMaterialCount() (res ResMaterialCount, err error) {
var accessToken string
accessToken, err = material.GetAccessToken()
if err != nil {
return
}
uri := fmt.Sprintf("%s?access_token=%s", getMaterialCountURL, accessToken)
var response []byte
response, err = util.HTTPGet(uri)
if err != nil {
return
}
err = util.DecodeWithError(response, &res, "GetMaterialCount")
return
}