-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.go
134 lines (121 loc) · 3.13 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
package application
import (
"encoding/json"
"fmt"
"github.com/EricJSanchez/wecom/util"
)
const (
// 创建菜单
createMenuAddr = "https://qyapi.weixin.qq.com/cgi-bin/menu/create?access_token=%s&agentid=%s"
// 获取菜单
getMenuAddr = "https://qyapi.weixin.qq.com/cgi-bin/menu/get?access_token=%s&agentid=%s"
// 删除菜单
deleteMenuAddr = "https://qyapi.weixin.qq.com/cgi-bin/menu/delete?access_token=%s&agentid=%s"
)
type SubButton struct {
Type string `json:"type"`
Name string `json:"name"`
URL string `json:"url,omitempty"`
Key string `json:"key,omitempty"`
SubButton []interface{} `json:"sub_button,omitempty"`
Pagepath string `json:"pagepath,omitempty"`
Appid string `json:"appid,omitempty"`
}
type Button struct {
Name string `json:"name"`
SubButton []SubButton `json:"sub_button,omitempty"`
Type string `json:"type,omitempty"`
Key string `json:"key,omitempty"`
}
// CreateMenuOptions 创建菜单请求参数
type CreateMenuOptions struct {
Button []Button `json:"button"`
}
// CreateMenuSchema 创建菜单响应内容
type CreateMenuSchema struct {
util.CommonError
}
// CreateMenu 创建菜单
func (r *Client) CreateMenu(options CreateMenuOptions) (info CreateMenuSchema, err error) {
var (
accessToken string
data []byte
)
_ = util.Record(r.cache, createMenuAddr)
accessToken, err = r.ctx.GetAccessToken()
if err != nil {
return
}
data, err = util.PostJSON(fmt.Sprintf(createMenuAddr, accessToken, r.agentID), options)
if err != nil {
return
}
if err = json.Unmarshal(data, &info); err != nil {
return
}
if info.ErrCode != 0 {
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
}
return info, nil
}
// GetMenuOptions 获取菜单请求参数
type GetMenuOptions struct {
}
// GetMenuSchema 获取菜单响应内容
type GetMenuSchema struct {
util.CommonError
Button []Button `json:"button"`
}
// GetMenu 获取菜单
func (r *Client) GetMenu() (info GetMenuSchema, err error) {
var (
accessToken string
data []byte
)
_ = util.Record(r.cache, getMenuAddr)
accessToken, err = r.ctx.GetAccessToken()
if err != nil {
return
}
data, err = util.HTTPGet(fmt.Sprintf(getMenuAddr, accessToken, r.agentID))
if err != nil {
return
}
if err = json.Unmarshal(data, &info); err != nil {
return
}
if info.ErrCode != 0 {
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
}
return info, nil
}
// DeleteMenuOptions 删除菜单请求参数
type DeleteMenuOptions struct {
}
// DeleteMenuSchema 删除菜单响应内容
type DeleteMenuSchema struct {
util.CommonError
}
// DeleteMenu 删除菜单
func (r *Client) DeleteMenu() (info DeleteMenuSchema, err error) {
var (
accessToken string
data []byte
)
_ = util.Record(r.cache, deleteMenuAddr)
accessToken, err = r.ctx.GetAccessToken()
if err != nil {
return
}
data, err = util.HTTPGet(fmt.Sprintf(deleteMenuAddr, accessToken, r.agentID))
if err != nil {
return
}
if err = json.Unmarshal(data, &info); err != nil {
return
}
if info.ErrCode != 0 {
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
}
return info, nil
}