This commit is contained in:
cn-hideyoshi 2024-12-10 19:19:12 +08:00
parent f06581c5d7
commit 45b0bfde33
2 changed files with 65 additions and 0 deletions

View File

@ -18,6 +18,14 @@ const (
code2UserinfoUrl string = "https://api.weixin.qq.com/sns/userinfo"
oaQrCodeCreateUrl string = "https://api.weixin.qq.com/cgi-bin/qrcode/create"
userInfoByOpenid string = "https://api.weixin.qq.com/cgi-bin/user/info"
CreateOaMenu string = "https://api.weixin.qq.com/cgi-bin/menu/create"
QueryOaMenu string = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info"
DeleteOaMenu string = "https://api.weixin.qq.com/cgi-bin/menu/delete"
)
const (
OaMenuTypeClick = "click"
OaMenuTypeView = "view"
)
type UserInfo struct {
@ -40,6 +48,14 @@ type BaseSdk struct {
lock sync.Mutex
}
type OaMenuButton struct {
Type string `json:"type"`
Name string `json:"name"`
Key string `json:"key,omitempty"`
Url string `json:"url,omitempty"`
SubButton []*OaMenuButton `json:"sub_button"`
}
func (o *BaseSdk) getAccessToken() (string, error) {
o.lock.Lock()
defer o.lock.Unlock()

View File

@ -97,3 +97,52 @@ func (o *OaSdk) GetUserInfoByOpenid(openid string) (*UserInfo, error) {
user.Openid = openid
return user, nil
}
func (o *OaSdk) CreateMenu(buttons []*OaMenuButton) error {
accessToken, err := o.getAccessToken()
if err != nil {
return err
}
bodyMp := make(map[string]interface{})
bodyMp["button"] = buttons
bodyBytes, _ := json.Marshal(bodyMp)
url := fmt.Sprintf("%s?access_token=%s", CreateOaMenu, accessToken)
res, err := http.Post(url, "application/json", bytes.NewBuffer(bodyBytes))
if err != nil {
return err
}
all, err := io.ReadAll(res.Body)
if err != nil {
return err
}
g := gjson.ParseBytes(all)
errCode := g.Get("errcode").Int()
if errCode != 0 {
return fmt.Errorf("%d:%s", errCode, g.Get("errmsg").String())
}
return nil
}
func (o *OaSdk) DeleteMenu() error {
accessToken, err := o.getAccessToken()
if err != nil {
return err
}
url := fmt.Sprintf("%s?access_token=%s", DeleteOaMenu, accessToken)
res, err := http.Get(url)
if err != nil {
return err
}
all, err := io.ReadAll(res.Body)
if err != nil {
return err
}
g := gjson.ParseBytes(all)
errCode := g.Get("errcode").Int()
if errCode != 0 {
return fmt.Errorf("%d:%s", errCode, g.Get("errmsg").String())
}
return nil
}