menu.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. package menu
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/silenceper/wechat/context"
  6. "github.com/silenceper/wechat/util"
  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. *context.Context
  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 *context.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. accessToken, err := menu.GetAccessToken()
  105. if err != nil {
  106. return err
  107. }
  108. uri := fmt.Sprintf("%s?access_token=%s", menuCreateURL, accessToken)
  109. reqMenu := &reqMenu{
  110. Button: buttons,
  111. }
  112. response, err := util.PostJSON(uri, reqMenu)
  113. if err != nil {
  114. return err
  115. }
  116. var commError util.CommonError
  117. err = json.Unmarshal(response, &commError)
  118. if err != nil {
  119. return err
  120. }
  121. if commError.ErrCode != 0 {
  122. return fmt.Errorf("SetMenu Error , errcode=%d , errmsg=%s", commError.ErrCode, commError.ErrMsg)
  123. }
  124. return nil
  125. }
  126. //GetMenu 获取菜单配置
  127. func (menu *Menu) GetMenu() (resMenu ResMenu, err error) {
  128. var accessToken string
  129. accessToken, err = menu.GetAccessToken()
  130. if err != nil {
  131. return
  132. }
  133. uri := fmt.Sprintf("%s?access_token=%s", menuGetURL, accessToken)
  134. var response []byte
  135. response, err = util.HTTPGet(uri)
  136. if err != nil {
  137. return
  138. }
  139. err = json.Unmarshal(response, &resMenu)
  140. if err != nil {
  141. return
  142. }
  143. if resMenu.ErrCode != 0 {
  144. err = fmt.Errorf("GetMenu Error , errcode=%d , errmsg=%s", resMenu.ErrCode, resMenu.ErrMsg)
  145. return
  146. }
  147. return
  148. }
  149. //DeleteMenu 删除菜单
  150. func (menu *Menu) DeleteMenu() error {
  151. accessToken, err := menu.GetAccessToken()
  152. if err != nil {
  153. return err
  154. }
  155. uri := fmt.Sprintf("%s?access_token=%s", menuDeleteURL, accessToken)
  156. response, err := util.HTTPGet(uri)
  157. if err != nil {
  158. return err
  159. }
  160. var commError util.CommonError
  161. err = json.Unmarshal(response, &commError)
  162. if err != nil {
  163. return err
  164. }
  165. if commError.ErrCode != 0 {
  166. return fmt.Errorf("GetMenu Error , errcode=%d , errmsg=%s", commError.ErrCode, commError.ErrMsg)
  167. }
  168. return nil
  169. }
  170. //AddConditional 添加个性化菜单
  171. func (menu *Menu) AddConditional(buttons []*Button, matchRule *MatchRule) error {
  172. accessToken, err := menu.GetAccessToken()
  173. if err != nil {
  174. return err
  175. }
  176. uri := fmt.Sprintf("%s?access_token=%s", menuAddConditionalURL, accessToken)
  177. reqMenu := &reqMenu{
  178. Button: buttons,
  179. MatchRule: matchRule,
  180. }
  181. response, err := util.PostJSON(uri, reqMenu)
  182. if err != nil {
  183. return err
  184. }
  185. var commError util.CommonError
  186. err = json.Unmarshal(response, &commError)
  187. if err != nil {
  188. return err
  189. }
  190. if commError.ErrCode != 0 {
  191. return fmt.Errorf("AddConditional Error , errcode=%d , errmsg=%s", commError.ErrCode, commError.ErrMsg)
  192. }
  193. return nil
  194. }
  195. //DeleteConditional 删除个性化菜单
  196. func (menu *Menu) DeleteConditional(menuID int64) error {
  197. accessToken, err := menu.GetAccessToken()
  198. if err != nil {
  199. return err
  200. }
  201. uri := fmt.Sprintf("%s?access_token=%s", menuDeleteConditionalURL, accessToken)
  202. reqDeleteConditional := &reqDeleteConditional{
  203. MenuID: menuID,
  204. }
  205. response, err := util.PostJSON(uri, reqDeleteConditional)
  206. if err != nil {
  207. return err
  208. }
  209. var commError util.CommonError
  210. err = json.Unmarshal(response, &commError)
  211. if err != nil {
  212. return err
  213. }
  214. if commError.ErrCode != 0 {
  215. return fmt.Errorf("DeleteConditional Error , errcode=%d , errmsg=%s", commError.ErrCode, commError.ErrMsg)
  216. }
  217. return nil
  218. }
  219. //MenuTryMatch 菜单匹配
  220. func (menu *Menu) MenuTryMatch(userID string) (buttons []Button, err error) {
  221. var accessToken string
  222. accessToken, err = menu.GetAccessToken()
  223. if err != nil {
  224. return
  225. }
  226. uri := fmt.Sprintf("%s?access_token=%s", menuTryMatchURL, accessToken)
  227. reqMenuTryMatch := &reqMenuTryMatch{userID}
  228. var response []byte
  229. response, err = util.PostJSON(uri, reqMenuTryMatch)
  230. if err != nil {
  231. return
  232. }
  233. var resMenuTryMatch resMenuTryMatch
  234. err = json.Unmarshal(response, &resMenuTryMatch)
  235. if err != nil {
  236. return
  237. }
  238. if resMenuTryMatch.ErrCode != 0 {
  239. err = fmt.Errorf("MenuTryMatch Error , errcode=%d , errmsg=%s", resMenuTryMatch.ErrCode, resMenuTryMatch.ErrMsg)
  240. return
  241. }
  242. buttons = resMenuTryMatch.Button
  243. return
  244. }
  245. //GetCurrentSelfMenuInfo 获取自定义菜单配置接口
  246. func (menu *Menu) GetCurrentSelfMenuInfo() (resSelfMenuInfo ResSelfMenuInfo, err error) {
  247. var accessToken string
  248. accessToken, err = menu.GetAccessToken()
  249. if err != nil {
  250. return
  251. }
  252. uri := fmt.Sprintf("%s?access_token=%s", menuSelfMenuInfoURL, accessToken)
  253. var response []byte
  254. response, err = util.HTTPGet(uri)
  255. if err != nil {
  256. return
  257. }
  258. err = json.Unmarshal(response, &resSelfMenuInfo)
  259. if err != nil {
  260. return
  261. }
  262. if resSelfMenuInfo.ErrCode != 0 {
  263. err = fmt.Errorf("GetCurrentSelfMenuInfo Error , errcode=%d , errmsg=%s", resSelfMenuInfo.ErrCode, resSelfMenuInfo.ErrMsg)
  264. return
  265. }
  266. return
  267. }