menu.go 6.6 KB

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