menu.go 5.2 KB

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