forked from rosbit/go-wx-api
/
menu-api.go
48 lines (40 loc) · 1.55 KB
/
menu-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
package wxtools
import (
"fmt"
"net/url"
"os"
"github.com/donpools/go-wx-api/auth"
)
func CreateMenu(accessToken string, menuJsonFile string) ([]byte, error) {
fpMenuJson, err := os.Open(menuJsonFile)
if err != nil {
return nil, err
}
defer fpMenuJson.Close()
url := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=%s", accessToken)
return wxauth.JsonCall(url, "POST", fpMenuJson)
}
func QueryMenu(accessToken string) ([]byte, error) {
url := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/menu/get?access_token=%s", accessToken)
return wxauth.CallWxAPI(url, "GET", nil)
}
func DeleteMenu(accessToken string) ([]byte, error) {
url := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=%s", accessToken)
return wxauth.CallWxAPI(url, "GET", nil)
}
func CurrentSelfmenuInfo(accessToken string) ([]byte, error) {
url := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=%s", accessToken)
return wxauth.CallWxAPI(url, "GET", nil)
}
var _valid_scopes = map[string]bool{"snsapi_base":true, "snsapi_userinfo":true}
func isValidScope(scope string) bool {
_, ok := _valid_scopes[scope]
return ok
}
func MakeAuthUrl(appId, redirectUri, scope, state string) (string, error) {
if !isValidScope(scope) {
return "", fmt.Errorf("unknown scope value: %s", scope)
}
encUri := url.QueryEscape(redirectUri)
return fmt.Sprintf("https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect", appId, encUri, scope, state), nil
}