forked from silenceper/wechat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.go
270 lines (235 loc) · 6.8 KB
/
menu.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
package menu
import (
"encoding/json"
"fmt"
"github.com/silenceper/wechat/context"
"github.com/silenceper/wechat/util"
)
const (
menuCreateURL = "https://api.weixin.qq.com/cgi-bin/menu/create"
menuGetURL = "https://api.weixin.qq.com/cgi-bin/menu/get"
menuDeleteURL = "https://api.weixin.qq.com/cgi-bin/menu/delete"
menuAddConditionalURL = "https://api.weixin.qq.com/cgi-bin/menu/addconditional"
menuDeleteConditionalURL = "https://api.weixin.qq.com/cgi-bin/menu/delconditional"
menuTryMatchURL = "https://api.weixin.qq.com/cgi-bin/menu/trymatch"
menuSelfMenuInfoURL = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info"
)
//Menu struct
type Menu struct {
*context.Context
}
//reqMenu 设置菜单请求数据
type reqMenu struct {
Button []*Button `json:"button,omitempty"`
MatchRule *MatchRule `json:"matchrule,omitempty"`
}
//reqDeleteConditional 删除个性化菜单请求数据
type reqDeleteConditional struct {
MenuID int64 `json:"menuid"`
}
//reqMenuTryMatch 菜单匹配请求
type reqMenuTryMatch struct {
UserID string `json:"user_id"`
}
//resConditionalMenu 个性化菜单返回结果
type resConditionalMenu struct {
Button []Button `json:"button"`
MatchRule MatchRule `json:"matchrule"`
MenuID int64 `json:"menuid"`
}
//resMenuTryMatch 菜单匹配请求结果
type resMenuTryMatch struct {
util.CommonError
Button []Button `json:"button"`
}
//ResMenu 查询菜单的返回数据
type ResMenu struct {
util.CommonError
Menu struct {
Button []Button `json:"button"`
MenuID int64 `json:"menuid"`
} `json:"menu"`
Conditionalmenu []resConditionalMenu `json:"conditionalmenu"`
}
//ResSelfMenuInfo 自定义菜单配置返回结果
type ResSelfMenuInfo struct {
util.CommonError
IsMenuOpen int32 `json:"is_menu_open"`
SelfMenuInfo struct {
Button []SelfMenuButton `json:"button"`
} `json:"selfmenu_info"`
}
//SelfMenuButton 自定义菜单配置详情
type SelfMenuButton struct {
Type string `json:"type"`
Name string `json:"name"`
Key string `json:"key"`
URL string `json:"url,omitempty"`
Value string `json:"value,omitempty"`
SubButton struct {
List []SelfMenuButton `json:"list"`
} `json:"sub_button,omitempty"`
NewsInfo struct {
List []ButtonNew `json:"list"`
} `json:"news_info,omitempty"`
}
//ButtonNew 图文消息菜单
type ButtonNew struct {
Title string `json:"title"`
Author string `json:"author"`
Digest string `json:"digest"`
ShowCover int32 `json:"show_cover"`
CoverURL string `json:"cover_url"`
ContentURL string `json:"content_url"`
SourceURL string `json:"source_url"`
}
//MatchRule 个性化菜单规则
type MatchRule struct {
GroupID int32 `json:"group_id,omitempty"`
Sex int32 `json:"sex,omitempty"`
Country string `json:"country,omitempty"`
Province string `json:"province,omitempty"`
City string `json:"city,omitempty"`
ClientPlatformType int32 `json:"client_platform_type,omitempty"`
Language string `json:"language,omitempty"`
}
//NewMenu 实例
func NewMenu(context *context.Context) *Menu {
menu := new(Menu)
menu.Context = context
return menu
}
//SetMenu 设置按钮
func (menu *Menu) SetMenu(buttons []*Button) error {
accessToken, err := menu.GetAccessToken()
if err != nil {
return err
}
uri := fmt.Sprintf("%s?access_token=%s", menuCreateURL, accessToken)
reqMenu := &reqMenu{
Button: buttons,
}
response, err := util.PostJSON(uri, reqMenu)
if err != nil {
return err
}
return util.DecodeWithCommonError(response, "SetMenu")
}
//GetMenu 获取菜单配置
func (menu *Menu) GetMenu() (resMenu ResMenu, err error) {
var accessToken string
accessToken, err = menu.GetAccessToken()
if err != nil {
return
}
uri := fmt.Sprintf("%s?access_token=%s", menuGetURL, accessToken)
var response []byte
response, err = util.HTTPGet(uri)
if err != nil {
return
}
err = json.Unmarshal(response, &resMenu)
if err != nil {
return
}
if resMenu.ErrCode != 0 {
err = fmt.Errorf("GetMenu Error , errcode=%d , errmsg=%s", resMenu.ErrCode, resMenu.ErrMsg)
return
}
return
}
//DeleteMenu 删除菜单
func (menu *Menu) DeleteMenu() error {
accessToken, err := menu.GetAccessToken()
if err != nil {
return err
}
uri := fmt.Sprintf("%s?access_token=%s", menuDeleteURL, accessToken)
response, err := util.HTTPGet(uri)
if err != nil {
return err
}
return util.DecodeWithCommonError(response, "GetMenu")
}
//AddConditional 添加个性化菜单
func (menu *Menu) AddConditional(buttons []*Button, matchRule *MatchRule) error {
accessToken, err := menu.GetAccessToken()
if err != nil {
return err
}
uri := fmt.Sprintf("%s?access_token=%s", menuAddConditionalURL, accessToken)
reqMenu := &reqMenu{
Button: buttons,
MatchRule: matchRule,
}
response, err := util.PostJSON(uri, reqMenu)
if err != nil {
return err
}
return util.DecodeWithCommonError(response, "AddConditional")
}
//DeleteConditional 删除个性化菜单
func (menu *Menu) DeleteConditional(menuID int64) error {
accessToken, err := menu.GetAccessToken()
if err != nil {
return err
}
uri := fmt.Sprintf("%s?access_token=%s", menuDeleteConditionalURL, accessToken)
reqDeleteConditional := &reqDeleteConditional{
MenuID: menuID,
}
response, err := util.PostJSON(uri, reqDeleteConditional)
if err != nil {
return err
}
return util.DecodeWithCommonError(response, "DeleteConditional")
}
//MenuTryMatch 菜单匹配
func (menu *Menu) MenuTryMatch(userID string) (buttons []Button, err error) {
var accessToken string
accessToken, err = menu.GetAccessToken()
if err != nil {
return
}
uri := fmt.Sprintf("%s?access_token=%s", menuTryMatchURL, accessToken)
reqMenuTryMatch := &reqMenuTryMatch{userID}
var response []byte
response, err = util.PostJSON(uri, reqMenuTryMatch)
if err != nil {
return
}
var resMenuTryMatch resMenuTryMatch
err = json.Unmarshal(response, &resMenuTryMatch)
if err != nil {
return
}
if resMenuTryMatch.ErrCode != 0 {
err = fmt.Errorf("MenuTryMatch Error , errcode=%d , errmsg=%s", resMenuTryMatch.ErrCode, resMenuTryMatch.ErrMsg)
return
}
buttons = resMenuTryMatch.Button
return
}
//GetCurrentSelfMenuInfo 获取自定义菜单配置接口
func (menu *Menu) GetCurrentSelfMenuInfo() (resSelfMenuInfo ResSelfMenuInfo, err error) {
var accessToken string
accessToken, err = menu.GetAccessToken()
if err != nil {
return
}
uri := fmt.Sprintf("%s?access_token=%s", menuSelfMenuInfoURL, accessToken)
var response []byte
response, err = util.HTTPGet(uri)
if err != nil {
return
}
err = json.Unmarshal(response, &resSelfMenuInfo)
if err != nil {
return
}
if resSelfMenuInfo.ErrCode != 0 {
err = fmt.Errorf("GetCurrentSelfMenuInfo Error , errcode=%d , errmsg=%s", resSelfMenuInfo.ErrCode, resSelfMenuInfo.ErrMsg)
return
}
return
}