forked from chanxuehong/wechat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.go
221 lines (190 loc) · 4.71 KB
/
api.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
package merchant
import (
"github.com/charsunny/wechat/mp/core"
)
// 创建商品.
func Create(clt *core.Client, product *Product) (err error) {
const incompleteURL = "https://api.weixin.qq.com/merchant/create?access_token="
var result struct{
core.Error
ProductId string `json:"product_id"`
}
if err = clt.PostJSON(incompleteURL, product, &result); err != nil {
return
}
if result.ErrCode != core.ErrCodeOK {
err = &result.Error
return
}
product.ProductId = result.ProductId
return
}
// 更新商品信息
func Update(clt *core.Client, product *Product) (err error) {
const incompleteURL = "https://api.weixin.qq.com/merchant/update?access_token="
var result struct{
core.Error
}
if err = clt.PostJSON(incompleteURL, product, &result); err != nil {
return
}
if result.ErrCode != core.ErrCodeOK {
err = &result.Error
return
}
return
}
// 查询商品.
func Get(clt *core.Client, productId string) (product *Product, err error) {
const incompleteURL = "https://api.weixin.qq.com/merchant/get?access_token="
var request = struct {
ProductId string `json:"product_id"`
}{
ProductId:productId,
}
var result struct {
core.Error
Product Product `json:"product_info"`
}
if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
return
}
if result.ErrCode != core.ErrCodeOK {
err = &result.Error
return
}
product = &result.Product
return
}
// 获取指定状态的所有商品
// @param status 商品状态(0-全部, 1-上架, 2-下架)
func GetByStatus(clt *core.Client, status int) (list []*Product, err error) {
const incompleteURL = "https://api.weixin.qq.com/merchant/getbystatus?access_token="
var request = struct {
Status int `json:"status"`
}{
Status:status,
}
var result struct {
core.Error
Products []*Product `json:"products_info"`
}
if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
return
}
if result.ErrCode != core.ErrCodeOK {
err = &result.Error
return
}
list = result.Products
return
}
// 商品上下架接口
// @param status
func UpdateStatus(clt *core.Client, productId string, status int) (err error) {
const incompleteURL = "https://api.weixin.qq.com/merchant/modproductstatus?access_token="
var request = struct {
ProductId string `json:"product_id"`
Status int `json:"status"`
}{
ProductId:productId,
Status:status,
}
var result struct {
core.Error
}
if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
return
}
if result.ErrCode != core.ErrCodeOK {
err = &result.Error
return
}
return
}
// 删除商品.
func Delete(clt *core.Client, productId string) (err error) {
const incompleteURL = "https://api.weixin.qq.com/merchant/del?access_token="
var request = struct {
ProductId string `json:"product_id"`
}{
ProductId:productId,
}
var result core.Error
if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
return
}
if result.ErrCode != core.ErrCodeOK {
err = &result
return
}
return
}
// 获取分类的所有子分类接口
// @param cateId: 大分类ID(根节点分类id为1)
// 微信上所有的商品类目分为四级,估计是要拉四级类目
func GetSubCate(clt *core.Client, cateId string) (list []*CateInfo, err error) {
const incompleteURL = "https://api.weixin.qq.com/merchant/category/getsub?access_token="
var request = struct {
CateId string `json:"cate_id"`
}{
CateId:cateId,
}
var result struct {
core.Error
Cates []*CateInfo `json:"cate_list"`
}
if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
return
}
if result.ErrCode != core.ErrCodeOK {
err = &result.Error
return
}
list = result.Cates
return
}
// 获取分类sku信息
func GetCateSku(clt *core.Client, cateId string) (list []*SkuTable, err error) {
const incompleteURL = "https://api.weixin.qq.com/merchant/category/getsku?access_token="
var request = struct {
CateId string `json:"cate_id"`
}{
CateId:cateId,
}
var result struct {
core.Error
List []*SkuTable `json:"sku_table"`
}
if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
return
}
if result.ErrCode != core.ErrCodeOK {
err = &result.Error
return
}
list = result.List
return
}
// 获取分类属性信息
func GetCateProperties(clt *core.Client, cateId string) (list []*CateProperty, err error) {
const incompleteURL = "https://api.weixin.qq.com/merchant/category/getproperty?access_token="
var request = struct {
CateId string `json:"cate_id"`
}{
CateId:cateId,
}
var result struct {
core.Error
List []*CateProperty `json:"properties"`
}
if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
return
}
if result.ErrCode != core.ErrCodeOK {
err = &result.Error
return
}
list = result.List
return
}