forked from heroicyang/wechat-qy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
menu.go
104 lines (82 loc) · 2.37 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
package api
import (
"encoding/json"
"net/url"
"strconv"
)
const (
createMenuURI = "https://qyapi.weixin.qq.com/cgi-bin/menu/create"
deleteMenuURI = "https://qyapi.weixin.qq.com/cgi-bin/menu/delete"
getMenuURI = "https://qyapi.weixin.qq.com/cgi-bin/menu/get"
)
// 自定义菜单按钮类型
const (
MenuButtonTypeClick = "click"
MenuButtonTypeView = "view"
MenuButtonTypeScanCodePush = "scancode_push"
MenuButtonTypeScanCodeWaitMsg = "scancode_waitmsg"
MenuButtonTypePicSysPhoto = "pic_sysphoto"
MenuButtonTypePicPhotoOrAlbum = "pic_photo_or_album"
MenuButtonTypePicWeixin = "pic_weixin"
MenuButtonTypeLocationSelect = "location_select"
)
// MenuButton 中最多包含 5 个子菜单(二级菜单)
type MenuButton struct {
Type string `json:"type"`
Name string `json:"name"`
Key string `json:"key,omitempty"`
URL string `json:"url,omitempty"`
SubButtons []MenuButton `json:"sub_button,omitempty"`
}
// Menu 中最多包含 3 个菜单按钮(一级菜单)
type Menu struct {
Buttons []MenuButton `json:"button"`
}
// CreateMenu 方法用于创建某个应用的菜单
func (a *API) CreateMenu(agentID int64, menu Menu) error {
token, err := a.Tokener.Token()
if err != nil {
return err
}
qs := make(url.Values)
qs.Add("access_token", token)
qs.Add("agentid", strconv.FormatInt(agentID, 10))
url := createMenuURI + "?" + qs.Encode()
data, err := json.Marshal(menu)
if err != nil {
return err
}
_, err = a.Client.PostJSON(url, data)
return err
}
// DeleteMenu 方法用于删除某个应用的菜单
func (a *API) DeleteMenu(agentID int64) error {
token, err := a.Tokener.Token()
if err != nil {
return err
}
qs := make(url.Values)
qs.Add("access_token", token)
qs.Add("agentid", strconv.FormatInt(agentID, 10))
url := deleteMenuURI + "?" + qs.Encode()
_, err = a.Client.GetJSON(url)
return err
}
// GetMenu 方法用于获取某个应用的菜单
func (a *API) GetMenu(agentID int64) (Menu, error) {
var menu Menu
token, err := a.Tokener.Token()
if err != nil {
return menu, err
}
qs := make(url.Values)
qs.Add("access_token", token)
qs.Add("agentid", strconv.FormatInt(agentID, 10))
url := getMenuURI + "?" + qs.Encode()
body, err := a.Client.GetJSON(url)
if err != nil {
return menu, err
}
err = json.Unmarshal(body, &menu)
return menu, err
}