-
Notifications
You must be signed in to change notification settings - Fork 0
/
card.go
304 lines (251 loc) · 5.87 KB
/
card.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
package card
import (
"fmt"
"strings"
"github.com/fideism/golang-wechat/officialaccount/context"
"github.com/fideism/golang-wechat/util"
)
// CouponType 券类型
type CouponType string
const (
// Groupon 团购券类型
Groupon CouponType = "GROUPON"
// Cash 代金券类型
Cash CouponType = "CASH"
// Discount 折扣券类型
Discount CouponType = "DISCOUNT"
// Gift 兑换券类型
Gift CouponType = "GIFT"
// GeneralCoupon 通用券。
GeneralCoupon CouponType = "GENERAL_COUPON"
// MemberCard 会员卡
MemberCard CouponType = "MEMBER_CARD"
// GeneralCard 礼品卡
GeneralCard CouponType = "GENERAL_CARD"
)
// Status 卡券状态
type Status string
const (
// StatusNotVerify 待审核
StatusNotVerify Status = "CARD_STATUS_NOT_VERIFY"
// StatusVerifyFail 审核失败
StatusVerifyFail Status = "CARD_STATUS_VERIFY_FAIL"
// StatusVerifyOk 审核成功
StatusVerifyOk Status = "CARD_STATUS_VERIFY_OK"
// StatusDelete 卡券被商户删除
StatusDelete Status = "CARD_STATUS_DELETE"
// StatusDispatch 在公众平台投放过的卡券
StatusDispatch Status = "CARD_STATUS_DISPATCH"
)
// Card 卡券
type Card struct {
*context.Context
}
//NewCard init
func NewCard(context *context.Context) *Card {
card := new(Card)
card.Context = context
return card
}
// CreateCard 创建卡券
func (card *Card) CreateCard(t CouponType, attrs util.Params) (cardID string, err error) {
var token string
token, err = card.GetAccessToken()
if err != nil {
return
}
url := fmt.Sprintf(createCardURL, token)
params := map[string]interface{}{
"card": map[string]interface{}{
"card_type": t,
strings.ToLower(string(t)): attrs,
},
}
var response []byte
response, err = util.PostJSON(url, params)
if err != nil {
return
}
var res struct {
util.CommonError
CardID string `json:"card_id"`
}
err = util.DecodeWithError(response, &res, "CreateCard")
if err != nil {
return
}
cardID = res.CardID
return
}
// UpdateCard 修改卡券信息
func (card *Card) UpdateCard(cardID string, t CouponType, attrs util.Params) (check bool, err error) {
var token string
token, err = card.GetAccessToken()
if err != nil {
return
}
url := fmt.Sprintf(updateURL, token)
params := map[string]interface{}{
"card_id": cardID,
strings.ToLower(string(t)): attrs,
}
var response []byte
response, err = util.PostJSON(url, params)
if err != nil {
return
}
var res struct {
util.CommonError
SendCheck bool `json:"send_check"`
}
err = util.DecodeWithError(response, &res, "UpdateCard")
if err != nil {
return
}
check = res.SendCheck
return
}
// GetCard 获取卡券详情
func (card *Card) GetCard(cardID string) (res util.Params, err error) {
var token string
token, err = card.GetAccessToken()
if err != nil {
return
}
url := fmt.Sprintf(getCardURL, token)
var response []byte
response, err = util.PostJSON(url, map[string]interface{}{
"card_id": cardID,
})
if err != nil {
return
}
var cardInfo struct {
util.CommonError
Card util.Params `json:"card"`
}
err = util.DecodeWithError(response, &cardInfo, "GetCard")
if err != nil {
return
}
res = cardInfo.Card
return
}
// BatchGetRequest 批量查询卡券列表 请求参数
type BatchGetRequest struct {
Offset int `json:"offset"`
Count int `json:"count"`
StatusList Status `json:"status_list"`
}
// BatchCardList 批量查询卡券列表 返回结果
type BatchCardList struct {
CardIDList []string `json:"card_id_list"`
TotalNum int `json:"total_num"`
}
// BatchGet 批量查询卡券列表
func (card *Card) BatchGet(req BatchGetRequest) (res BatchCardList, err error) {
var token string
token, err = card.GetAccessToken()
if err != nil {
return
}
url := fmt.Sprintf(batchGetURL, token)
var response []byte
response, err = util.PostJSON(url, req)
if err != nil {
return
}
var items struct {
util.CommonError
BatchCardList
}
err = util.DecodeWithError(response, &items, "CreateCard")
if err != nil {
return
}
res = BatchCardList{
CardIDList: items.CardIDList,
TotalNum: items.TotalNum,
}
return
}
// DeleteCard 删除卡券
func (card *Card) DeleteCard(cardID string) (err error) {
var token string
token, err = card.GetAccessToken()
if err != nil {
return
}
url := fmt.Sprintf(deleteURL, token)
var response []byte
response, err = util.PostJSON(url, map[string]interface{}{
"card_id": cardID,
})
if err != nil {
return
}
err = util.DecodeWithCommonError(response, "DeleteCard")
return
}
// Qrcode 卡券二维码
type Qrcode struct {
Ticket string `json:"ticket"`
ExpireSeconds int64 `json:"expire_seconds"`
URL string `json:"url"`
ShowQrcodeURL string `json:"show_qrcode_url"`
}
// CreateCardQrcode 创建卡券二维码
func (card *Card) CreateCardQrcode(attr util.Params) (res Qrcode, err error) {
var token string
token, err = card.GetAccessToken()
if err != nil {
return
}
url := fmt.Sprintf(createQrcodeURL, token)
var response []byte
response, err = util.PostJSON(url, attr)
if err != nil {
return
}
var qrcode struct {
util.CommonError
Qrcode
}
err = util.DecodeWithError(response, &qrcode, "GetColors")
if err != nil {
return
}
res = Qrcode{
Ticket: qrcode.Ticket,
ExpireSeconds: qrcode.ExpireSeconds,
URL: qrcode.URL,
ShowQrcodeURL: qrcode.ShowQrcodeURL,
}
return
}
// GetHTML 图文消息群发卡券
func (card *Card) GetHTML(cardID string) (content string, err error) {
var token string
token, err = card.GetAccessToken()
if err != nil {
return
}
url := fmt.Sprintf(getHTMLURL, token)
var response []byte
response, err = util.PostJSON(url, map[string]interface{}{
"card_id": cardID,
})
if err != nil {
return
}
var res struct {
util.CommonError
Content string `json:"content"`
}
err = util.DecodeWithError(response, &res, "GetHtml")
if err != nil {
return
}
content = res.Content
return
}