1
0
mirror of https://github.com/silenceper/wechat.git synced 2026-02-04 21:02:25 +08:00

Compare commits

..

20 Commits

Author SHA1 Message Date
silenceper
5b578ebd3c Update README.md 2021-08-26 10:40:21 +08:00
hewen
843ed3fa8a material add BatchGetMaterial (#258)
* material add BatchGetMaterial

1. fix material golint error

2. BatchGetMaterial use util.DecodeWithError check error

Co-authored-by: hewen <hewen@liao.com>
2020-05-28 16:20:22 +08:00
silenceper
8762af2441 Merge pull request #252 from silenceper/silenceper-patch-1
Update README.md
2020-05-25 10:24:00 +08:00
silenceper
bfd23056eb Update README.md 2020-05-25 10:23:24 +08:00
silenceper
fedcd371d0 Merge pull request #250 from silenceper/silenceper-patch-1
Update README.md
2020-05-24 22:16:42 +08:00
silenceper
6569d47301 Update README.md 2020-05-24 22:16:06 +08:00
silenceper
05e23e0d88 Merge pull request #237 from silenceper/fix-redis-import
更新依赖
2020-05-22 12:33:50 +08:00
silenceper
6279dadd29 更新依赖 2020-05-22 12:27:26 +08:00
silenceper
a357c82080 Merge pull request #236 from silenceper/revert-232-fix-go.mod
Revert "更新go.mod,redis依赖"
2020-05-22 12:23:56 +08:00
silenceper
ae271960e2 Revert "更新go.mod,redis依赖" 2020-05-22 12:23:44 +08:00
silenceper
45522f003f Merge pull request #235 from silenceper/revert-233-fix-go.mod
Revert "Fix go.mod"
2020-05-22 12:23:11 +08:00
silenceper
a264ce4266 Revert "Fix go.mod" 2020-05-22 12:22:53 +08:00
silenceper
3c881e3885 Merge pull request #233 from silenceper/fix-go.mod
Fix go.mod
2020-05-22 11:16:51 +08:00
silenceper
3740bb55c3 更新redis依赖 2020-05-22 11:12:40 +08:00
silenceper
c42d799367 更新redis依赖 2020-05-22 10:42:45 +08:00
silenceper
798c5b081c 更新redis依赖 2020-05-22 10:39:50 +08:00
silenceper
491ee80136 更新 github.com/gomodule/redigo@1.8.1 2020-05-22 10:28:12 +08:00
silenceper
a54b03a918 更新go.mod依赖 2020-05-22 10:23:30 +08:00
silenceper
eec3233134 Merge pull request #232 from silenceper/fix-go.mod
更新go.mod,redis依赖
2020-05-22 10:13:00 +08:00
silenceper
587ce04b5f 更新go.mod,redis依赖 2020-05-22 10:11:51 +08:00
4 changed files with 102 additions and 11 deletions

View File

@@ -1,3 +1,5 @@
## 📢 注意: 此分支为v1版本已不再维护更新请切换至 [v2](https://github.com/silenceper/wechat/tree/release-2.0)
# WeChat SDK for Go
[![Build Status](https://travis-ci.org/silenceper/wechat.svg?branch=master)](https://travis-ci.org/silenceper/wechat)
[![Go Report Card](https://goreportcard.com/badge/github.com/silenceper/wechat)](https://goreportcard.com/report/github.com/silenceper/wechat)
@@ -530,6 +532,17 @@ type Config struct {
[素材管理API](https://godoc.org/github.com/silenceper/wechat/material#Material)
### 批量获取永久素材
```go
list, err := wc.GetMaterial().BatchGetMaterial(material.PermanentMaterialTypeNews, 0, 10)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(list)
```
## 小程序开发
获取小程序操作对象

5
go.mod
View File

@@ -8,12 +8,11 @@ require (
github.com/fatih/structs v1.1.0
github.com/gin-gonic/gin v1.1.4
github.com/golang/protobuf v0.0.0-20161117033126-8ee79997227b // indirect
github.com/gomodule/redigo v2.0.1-0.20180627144507-2cd21d9966bf+incompatible
github.com/kr/pretty v0.1.0
github.com/gomodule/redigo v1.8.1
github.com/kr/pretty v0.1.0 // indirect
github.com/manucorporat/sse v0.0.0-20160126180136-ee05b128a739 // indirect
github.com/mattn/go-isatty v0.0.0-20161123143637-30a891c33c7c // indirect
github.com/spf13/cast v1.3.1
github.com/stretchr/testify v1.4.0 // indirect
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
golang.org/x/net v0.0.0-20191125084936-ffdde1057850 // indirect
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect

8
go.sum
View File

@@ -4,6 +4,7 @@ github.com/bradfitz/gomemcache v0.0.0-20160117192205-fb1f79c6b65a h1:k5TuEkqEYCR
github.com/bradfitz/gomemcache v0.0.0-20160117192205-fb1f79c6b65a/go.mod h1:PmM6Mmwb0LSuEubjR8N7PtNe1KxZLtOUHtbeikc5h60=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
@@ -11,8 +12,8 @@ github.com/gin-gonic/gin v1.1.4 h1:XLaCFbU39SSGRQrEeP7Z7mM3lvRqC4vE5tEaVdLDdSE=
github.com/gin-gonic/gin v1.1.4/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y=
github.com/golang/protobuf v0.0.0-20161117033126-8ee79997227b h1:fE/yi9pibxGEc0gSJuEShcsBXE2d5FW3OudsjE9tKzQ=
github.com/golang/protobuf v0.0.0-20161117033126-8ee79997227b/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/gomodule/redigo v2.0.1-0.20180627144507-2cd21d9966bf+incompatible h1:QJ4V3LjaRe/6NKoaaj2QzQZcezt5gNXdPv0axxS4VNA=
github.com/gomodule/redigo v2.0.1-0.20180627144507-2cd21d9966bf+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
github.com/gomodule/redigo v1.8.1 h1:Abmo0bI7Xf0IhdIPc7HZQzZcShdnmxeoVuDDtIQp8N8=
github.com/gomodule/redigo v1.8.1/go.mod h1:P9dn9mFrCBvWhGE1wpxx6fgq7BAeLBk+UUUzlpkBYO0=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@@ -28,8 +29,7 @@ github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng=
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20191125084936-ffdde1057850 h1:Vq85/r8R9IdcUHmZ0/nQlUg1v15rzvQ2sHdnZAj/x7s=

View File

@@ -10,10 +10,25 @@ import (
)
const (
addNewsURL = "https://api.weixin.qq.com/cgi-bin/material/add_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"
addNewsURL = "https://api.weixin.qq.com/cgi-bin/material/add_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"
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 素材管理
@@ -55,6 +70,9 @@ func (material *Material) GetNews(id string) ([]*Article, error) {
}
req.MediaID = id
responseBytes, err := util.PostJSON(uri, req)
if err != nil {
return nil, err
}
var res struct {
NewsItem []*Article `json:"news_item"`
@@ -91,6 +109,10 @@ func (material *Material) AddNews(articles []*Article) (mediaID string, err erro
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 {
@@ -112,6 +134,7 @@ type resAddMaterial struct {
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()
@@ -216,3 +239,59 @@ func (material *Material) DeleteMaterial(mediaID string) error {
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
}