menu.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package menu
  2. import (
  3. "encoding/json"
  4. "github.com/yaotian/gowechat/mp/base"
  5. "github.com/yaotian/gowechat/util"
  6. "github.com/yaotian/gowechat/wxcontext"
  7. )
  8. const (
  9. menuCreateURL = "https://api.weixin.qq.com/cgi-bin/menu/create"
  10. menuGetURL = "https://api.weixin.qq.com/cgi-bin/menu/get"
  11. menuDeleteURL = "https://api.weixin.qq.com/cgi-bin/menu/delete"
  12. menuAddConditionalURL = "https://api.weixin.qq.com/cgi-bin/menu/addconditional"
  13. menuDeleteConditionalURL = "https://api.weixin.qq.com/cgi-bin/menu/delconditional"
  14. menuTryMatchURL = "https://api.weixin.qq.com/cgi-bin/menu/trymatch"
  15. menuSelfMenuInfoURL = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info"
  16. )
  17. //Menu struct
  18. type Menu struct {
  19. base.MpBase
  20. }
  21. //reqMenu 设置菜单请求数据
  22. type reqMenu struct {
  23. Button []*Button `json:"button,omitempty"`
  24. MatchRule *MatchRule `json:"matchrule,omitempty"`
  25. }
  26. //reqDeleteConditional 删除个性化菜单请求数据
  27. type reqDeleteConditional struct {
  28. MenuID int64 `json:"menuid"`
  29. }
  30. //reqMenuTryMatch 菜单匹配请求
  31. type reqMenuTryMatch struct {
  32. UserID string `json:"user_id"`
  33. }
  34. //resConditionalMenu 个性化菜单返回结果
  35. type resConditionalMenu struct {
  36. Button []Button `json:"button"`
  37. MatchRule MatchRule `json:"matchrule"`
  38. MenuID int64 `json:"menuid"`
  39. }
  40. //resMenuTryMatch 菜单匹配请求结果
  41. type resMenuTryMatch struct {
  42. util.CommonError
  43. Button []Button `json:"button"`
  44. }
  45. //ResMenu 查询菜单的返回数据
  46. type ResMenu struct {
  47. util.CommonError
  48. Menu struct {
  49. Button []Button `json:"button"`
  50. MenuID int64 `json:"menuid"`
  51. } `json:"menu"`
  52. Conditionalmenu []resConditionalMenu `json:"conditionalmenu"`
  53. }
  54. //ResSelfMenuInfo 自定义菜单配置返回结果
  55. type ResSelfMenuInfo struct {
  56. util.CommonError
  57. IsMenuOpen int32 `json:"is_menu_open"`
  58. SelfMenuInfo struct {
  59. Button []SelfMenuButton `json:"button"`
  60. } `json:"selfmenu_info"`
  61. }
  62. //SelfMenuButton 自定义菜单配置详情
  63. type SelfMenuButton struct {
  64. Type string `json:"type"`
  65. Name string `json:"name"`
  66. Key string `json:"key"`
  67. URL string `json:"url,omitempty"`
  68. Value string `json:"value,omitempty"`
  69. SubButton struct {
  70. List []SelfMenuButton `json:"list"`
  71. } `json:"sub_button,omitempty"`
  72. NewsInfo struct {
  73. List []ButtonNew `json:"list"`
  74. } `json:"news_info,omitempty"`
  75. }
  76. //ButtonNew 图文消息菜单
  77. type ButtonNew struct {
  78. Title string `json:"title"`
  79. Author string `json:"author"`
  80. Digest string `json:"digest"`
  81. ShowCover int32 `json:"show_cover"`
  82. CoverURL string `json:"cover_url"`
  83. ContentURL string `json:"content_url"`
  84. SourceURL string `json:"source_url"`
  85. }
  86. //MatchRule 个性化菜单规则
  87. type MatchRule struct {
  88. GroupID int32 `json:"group_id,omitempty"`
  89. Sex int32 `json:"sex,omitempty"`
  90. Country string `json:"country,omitempty"`
  91. Province string `json:"province,omitempty"`
  92. City string `json:"city,omitempty"`
  93. ClientPlatformType int32 `json:"client_platform_type,omitempty"`
  94. Language string `json:"language,omitempty"`
  95. }
  96. //NewMenu 实例
  97. func NewMenu(context *wxcontext.Context) *Menu {
  98. menu := new(Menu)
  99. menu.Context = context
  100. return menu
  101. }
  102. //SetMenu 设置按钮
  103. func (menu *Menu) SetMenu(buttons []*Button) error {
  104. reqMenu := &reqMenu{
  105. Button: buttons,
  106. }
  107. _, err := menu.HTTPPostJSONWithAccessToken(menuCreateURL, reqMenu)
  108. if err != nil {
  109. return err
  110. }
  111. return nil
  112. }
  113. //GetMenu 获取菜单配置
  114. func (menu *Menu) GetMenu() (resMenu ResMenu, err error) {
  115. var response []byte
  116. response, err = menu.HTTPGetWithAccessToken(menuGetURL)
  117. if err != nil {
  118. return
  119. }
  120. err = json.Unmarshal(response, &resMenu)
  121. return
  122. }
  123. //DeleteMenu 删除菜单
  124. func (menu *Menu) DeleteMenu() (err error) {
  125. _, err = menu.HTTPGetWithAccessToken(menuDeleteURL)
  126. return
  127. }
  128. //AddConditional 添加个性化菜单
  129. func (menu *Menu) AddConditional(buttons []*Button, matchRule *MatchRule) error {
  130. reqMenu := &reqMenu{
  131. Button: buttons,
  132. MatchRule: matchRule,
  133. }
  134. _, err := menu.HTTPPostJSONWithAccessToken(menuAddConditionalURL, reqMenu)
  135. if err != nil {
  136. return err
  137. }
  138. return nil
  139. }
  140. //DeleteConditional 删除个性化菜单
  141. func (menu *Menu) DeleteConditional(menuID int64) error {
  142. reqDeleteConditional := &reqDeleteConditional{
  143. MenuID: menuID,
  144. }
  145. _, err := menu.HTTPPostJSONWithAccessToken(menuDeleteConditionalURL, reqDeleteConditional)
  146. if err != nil {
  147. return err
  148. }
  149. return nil
  150. }
  151. //MenuTryMatch 菜单匹配
  152. func (menu *Menu) MenuTryMatch(userID string) (buttons []Button, err error) {
  153. reqMenuTryMatch := &reqMenuTryMatch{userID}
  154. var response []byte
  155. response, err = menu.HTTPPostJSONWithAccessToken(menuTryMatchURL, reqMenuTryMatch)
  156. if err != nil {
  157. return
  158. }
  159. var resMenuTryMatch resMenuTryMatch
  160. err = json.Unmarshal(response, &resMenuTryMatch)
  161. if err != nil {
  162. return
  163. }
  164. buttons = resMenuTryMatch.Button
  165. return
  166. }
  167. //GetCurrentSelfMenuInfo 获取自定义菜单配置接口
  168. func (menu *Menu) GetCurrentSelfMenuInfo() (resSelfMenuInfo ResSelfMenuInfo, err error) {
  169. var response []byte
  170. response, err = menu.HTTPGetWithAccessToken(menuSelfMenuInfoURL)
  171. if err != nil {
  172. return
  173. }
  174. err = json.Unmarshal(response, &resSelfMenuInfo)
  175. return
  176. }