5 Star 20 Fork 6

samoye/GoWeChat

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Menu.go 5.18 KB
一键复制 编辑 原始数据 按行查看 历史
samoye 提交于 2020-06-04 14:58 +08:00 . change module name
package account
import (
"encoding/json"
"errors"
"fmt"
"gitee.com/yizhisamoye/GoWeChat/entity/common"
"gitee.com/yizhisamoye/GoWeChat/entity/menu"
"gitee.com/yizhisamoye/GoWeChat/entity/user"
"gitee.com/yizhisamoye/GoWeChat/utils"
)
/**
菜单管理
*/
func(w WxAccount) CreateMenu(menu menu.Menu)error{
url:=fmt.Sprintf(BaseUrl+"/cgi-bin/menu/create?access_token=%s",w.accessToken)
_,err:=utils.SendJson(url,map[string]interface{}{
"button":menu.Button,
})
return err
}
//获取所有菜单
func(w WxAccount) GetAllMenu()(menu.Menu,error){
resultMenu:= menu.Menu{}
url:=fmt.Sprintf(BaseUrl+"/cgi-bin/get_current_selfmenu_info?access_token=%s",w.accessToken)
result,err:=utils.GetJson(url)
if err!=nil{
return resultMenu,err
}
if isMenuOpen:=int(result["is_menu_open"].(float64));isMenuOpen==0{
return resultMenu,errors.New("菜单未开启,请前往微信公众号设置")
}
selfMenuInfo:=result["selfmenu_info"].(map[string]interface{})
tmpButton:=selfMenuInfo["button"].([]interface{})
buttons:=make([]menu.MenuButton,0)
for _,v:=range tmpButton{
vMap:=v.(map[string]interface{})
button:=new(menu.MenuButton)
if tmpSubButtons,exist:=vMap["sub_button"];exist{
button.Name=vMap["name"].(string)
button.SubButton=make([]menu.MenuButton,0)
singleSubButton:=new(menu.MenuButton)
subButtons:=(tmpSubButtons.(map[string]interface{}))["list"].([]interface{})
for _,tmpSubButton:=range subButtons{
subButton:=tmpSubButton.(map[string]interface{})
if tmpNewsInfos,exist:=subButton["news_info"];exist{
singleSubButton.Type=subButton["type"].(string)
singleSubButton.Name=subButton["name"].(string)
singleSubButton.Value=subButton["value"].(string)
singleSubButton.NewsInfo=make([]common.News,0)
newsInfos:=(tmpNewsInfos.(map[string][]interface{}))["list"]
for _,newInfo:=range newsInfos{
news:=new(common.News)
newInfoByte,_:=json.Marshal(newInfo)
json.Unmarshal(newInfoByte,news)
singleSubButton.NewsInfo=append(singleSubButton.NewsInfo,*news)
}
}else{
subButtonJsonByte,_:=json.Marshal(subButton)
json.Unmarshal(subButtonJsonByte,singleSubButton)
}
button.SubButton=append(button.SubButton,*singleSubButton)
}
}else{
vJsonByte,_:=json.Marshal(v)
json.Unmarshal(vJsonByte,button)
}
buttons=append(buttons,*button)
}
resultMenu.Button=buttons
return resultMenu,nil
}
//获取自定义菜单配置,只能获取通过api设置的菜单
func(w WxAccount) GetMenu()(*menu.Menu,[]menu.Menu,error){
url:=fmt.Sprintf(BaseUrl+"/cgi-bin/menu/get?access_token=%s",w.accessToken)
result,err:=utils.GetJson(url)
if err!=nil{
return nil,nil,err
}
//普通菜单
normalMenu:=new(menu.Menu)
tmpNormalMenu:=result["menu"].(map[string]interface{})
menuByte,_:=json.Marshal(tmpNormalMenu)
json.Unmarshal(menuByte,normalMenu)
//normalMenu.MenuId=tmpNormalMenu["menuid"].(string)
//tmpButton:=(tmpNormalMenu)["button"]
//tmpButtonByte,_:=json.Marshal(tmpButton)
//normalMenu.Button=make([]menu.MenuButton,0)
//json.Unmarshal(tmpButtonByte,normalMenu.Button)
//个性化菜单
conditionMenus:=make([]menu.Menu,0)
tmpConditionMenus:=result["conditionalmenu"].([]interface{})
menuByte,_=json.Marshal(tmpConditionMenus)
json.Unmarshal(menuByte,conditionMenus)
//for _,v:=range tmpConditionMenus{
// m:=new(menu.Menu)
// //menuid
// m.MenuId=(v.(map[string]interface{}))["menuid"].(string)
// //匹配规则
// tmpMatchRule:=new(menu.MenuMatchRule)
// tmpMatchRuleBytes,_:=json.Marshal((v.(map[string]interface{}))["matchrule"])
// json.Unmarshal(tmpMatchRuleBytes,tmpMatchRule)
// m.MatchRule=*tmpMatchRule
// //菜单按钮
// m.Button=make([]menu.MenuButton,0)
// tmpButtonByte,_:=json.Marshal((v.(map[string]interface{}))["button"].([]interface{}))
// json.Unmarshal(tmpButtonByte,m.Button)
// conditionMenus=append(conditionMenus,*m)
//}
return normalMenu,conditionMenus,nil
}
//删除菜单
func(w WxAccount) DeleteMenu()error{
url:=fmt.Sprintf(BaseUrl+"/cgi-bin/menu/delete?access_token=%s",w.accessToken)
_,err:=utils.GetJson(url)
return err
}
//创建个性化菜单
func(w WxAccount) CreateConditionMenu(m menu.Menu)error{
url:=fmt.Sprintf(BaseUrl+"/cgi-bin/menu/addconditional?access_token=%s",w.accessToken)
_,err:=utils.SendJson(url,map[string]interface{}{
"button":m.Button,
"matchrule":m.MatchRule,
})
return err
}
//删除个性化菜单
func(w WxAccount) DeleteConditionMenu(menuId string)error{
url:=fmt.Sprintf(BaseUrl+"/cgi-bin/menu/delconditional?access_token=%s",w.accessToken)
_,err:=utils.SendJson(url,map[string]string{
"menuid":menuId,
})
return err
}
//获取用户匹配的菜单
func(w WxAccount)MatchConditionMenu(user user.WxUser)(*menu.Menu,error){
url:=fmt.Sprintf(BaseUrl+"/cgi-bin/menu/trymatch?access_token=%s",w.accessToken)
result,err:=utils.SendJson(url,map[string]interface{}{
"user_id":user.OpenId,
})
if err!=nil{
return nil,err
}
m:=new(menu.Menu)
menuByte,_:=json.Marshal(result)
err=json.Unmarshal(menuByte,m)
return m,err
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/yizhisamoye/GoWeChat.git
git@gitee.com:yizhisamoye/GoWeChat.git
yizhisamoye
GoWeChat
GoWeChat
ed8de99d2f46

搜索帮助