|
| 1 | +package com.github.binarywang.demo.spring.controller; |
| 2 | + |
| 3 | +import me.chanjar.weixin.common.api.WxConsts; |
| 4 | +import me.chanjar.weixin.common.bean.menu.WxMenu; |
| 5 | +import me.chanjar.weixin.common.bean.menu.WxMenuButton; |
| 6 | +import me.chanjar.weixin.common.exception.WxErrorException; |
| 7 | +import me.chanjar.weixin.mp.api.WxMpMenuService; |
| 8 | +import me.chanjar.weixin.mp.api.WxMpService; |
| 9 | +import me.chanjar.weixin.mp.bean.menu.WxMpGetSelfMenuInfoResult; |
| 10 | +import me.chanjar.weixin.mp.bean.menu.WxMpMenu; |
| 11 | +import org.springframework.beans.factory.annotation.Autowired; |
| 12 | +import org.springframework.web.bind.annotation.*; |
| 13 | + |
| 14 | +/** |
| 15 | + * <pre> |
| 16 | + * 注意:此contorller 实现WxMpMenuService接口,仅是为了演示如何调用所有菜单相关操作接口, |
| 17 | + * 实际项目中无需这样,根据自己需要添加对应接口即可 |
| 18 | + * </pre> |
| 19 | + * |
| 20 | + * @author Binary Wang(https://github.com/binarywang) |
| 21 | + */ |
| 22 | +@RestController |
| 23 | +@RequestMapping("/wechat/menu") |
| 24 | +public class WxMenuController implements WxMpMenuService { |
| 25 | + |
| 26 | + @Autowired |
| 27 | + private WxMpService wxService; |
| 28 | + |
| 29 | + /** |
| 30 | + * <pre> |
| 31 | + * 自定义菜单创建接口 |
| 32 | + * 详情请见:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141013&token=&lang=zh_CN |
| 33 | + * 如果要创建个性化菜单,请设置matchrule属性 |
| 34 | + * 详情请见:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1455782296&token=&lang=zh_CN |
| 35 | + * </pre> |
| 36 | + * |
| 37 | + * @param menu |
| 38 | + * @return 如果是个性化菜单,则返回menuid,否则返回null |
| 39 | + */ |
| 40 | + @Override |
| 41 | + @PostMapping("/create") |
| 42 | + public String menuCreate(@RequestBody WxMenu menu) throws WxErrorException { |
| 43 | + return this.wxService.getMenuService().menuCreate(menu); |
| 44 | + } |
| 45 | + |
| 46 | + @GetMapping("/create") |
| 47 | + public String menuCreateSample() throws WxErrorException { |
| 48 | + WxMenu menu = new WxMenu(); |
| 49 | + WxMenuButton button1 = new WxMenuButton(); |
| 50 | + button1.setType(WxConsts.BUTTON_CLICK); |
| 51 | + button1.setName("今日歌曲"); |
| 52 | + button1.setKey("V1001_TODAY_MUSIC"); |
| 53 | + |
| 54 | +// WxMenuButton button2 = new WxMenuButton(); |
| 55 | +// button2.setType(WxConsts.BUTTON_MINIPROGRAM); |
| 56 | +// button2.setName("小程序"); |
| 57 | +// button2.setAppId("wx286b93c14bbf93aa"); |
| 58 | +// button2.setPagePath("pages/lunar/index.html"); |
| 59 | +// button2.setUrl("http://mp.weixin.qq.com"); |
| 60 | + |
| 61 | + WxMenuButton button3 = new WxMenuButton(); |
| 62 | + button3.setName("菜单"); |
| 63 | + |
| 64 | + menu.getButtons().add(button1); |
| 65 | +// menu.getButtons().add(button2); |
| 66 | + menu.getButtons().add(button3); |
| 67 | + |
| 68 | + WxMenuButton button31 = new WxMenuButton(); |
| 69 | + button31.setType(WxConsts.BUTTON_VIEW); |
| 70 | + button31.setName("搜索"); |
| 71 | + button31.setUrl("http://www.soso.com/"); |
| 72 | + |
| 73 | + WxMenuButton button32 = new WxMenuButton(); |
| 74 | + button32.setType(WxConsts.BUTTON_VIEW); |
| 75 | + button32.setName("视频"); |
| 76 | + button32.setUrl("http://v.qq.com/"); |
| 77 | + |
| 78 | + WxMenuButton button33 = new WxMenuButton(); |
| 79 | + button33.setType(WxConsts.BUTTON_CLICK); |
| 80 | + button33.setName("赞一下我们"); |
| 81 | + button33.setKey("V1001_GOOD"); |
| 82 | + |
| 83 | + button3.getSubButtons().add(button31); |
| 84 | + button3.getSubButtons().add(button32); |
| 85 | + button3.getSubButtons().add(button33); |
| 86 | + |
| 87 | + return this.wxService.getMenuService().menuCreate(menu); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * <pre> |
| 92 | + * 自定义菜单创建接口 |
| 93 | + * 详情请见: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141013&token=&lang=zh_CN |
| 94 | + * 如果要创建个性化菜单,请设置matchrule属性 |
| 95 | + * 详情请见:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1455782296&token=&lang=zh_CN |
| 96 | + * </pre> |
| 97 | + * |
| 98 | + * @param json |
| 99 | + * @return 如果是个性化菜单,则返回menuid,否则返回null |
| 100 | + */ |
| 101 | + @Override |
| 102 | + @GetMapping("/create/{json}") |
| 103 | + public String menuCreate(@PathVariable String json) throws WxErrorException { |
| 104 | + return this.wxService.getMenuService().menuCreate(json); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * <pre> |
| 109 | + * 自定义菜单删除接口 |
| 110 | + * 详情请见: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141015&token=&lang=zh_CN |
| 111 | + * </pre> |
| 112 | + */ |
| 113 | + @Override |
| 114 | + @GetMapping("/delete") |
| 115 | + public void menuDelete() throws WxErrorException { |
| 116 | + this.wxService.getMenuService().menuDelete(); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * <pre> |
| 121 | + * 删除个性化菜单接口 |
| 122 | + * 详情请见: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1455782296&token=&lang=zh_CN |
| 123 | + * </pre> |
| 124 | + * |
| 125 | + * @param menuId 个性化菜单的menuid |
| 126 | + */ |
| 127 | + @Override |
| 128 | + @GetMapping("/delete/{menuId}") |
| 129 | + public void menuDelete(@PathVariable String menuId) throws WxErrorException { |
| 130 | + this.wxService.getMenuService().menuDelete(menuId); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * <pre> |
| 135 | + * 自定义菜单查询接口 |
| 136 | + * 详情请见: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141014&token=&lang=zh_CN |
| 137 | + * </pre> |
| 138 | + */ |
| 139 | + @Override |
| 140 | + @GetMapping("/get") |
| 141 | + public WxMpMenu menuGet() throws WxErrorException { |
| 142 | + return this.wxService.getMenuService().menuGet(); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * <pre> |
| 147 | + * 测试个性化菜单匹配结果 |
| 148 | + * 详情请见: http://mp.weixin.qq.com/wiki/0/c48ccd12b69ae023159b4bfaa7c39c20.html |
| 149 | + * </pre> |
| 150 | + * |
| 151 | + * @param userid 可以是粉丝的OpenID,也可以是粉丝的微信号。 |
| 152 | + */ |
| 153 | + @Override |
| 154 | + @GetMapping("/menuTryMatch/{userid}") |
| 155 | + public WxMenu menuTryMatch(@PathVariable String userid) throws WxErrorException { |
| 156 | + return this.wxService.getMenuService().menuTryMatch(userid); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * <pre> |
| 161 | + * 获取自定义菜单配置接口 |
| 162 | + * 本接口将会提供公众号当前使用的自定义菜单的配置,如果公众号是通过API调用设置的菜单,则返回菜单的开发配置,而如果公众号是在公众平台官网通过网站功能发布菜单,则本接口返回运营者设置的菜单配置。 |
| 163 | + * 请注意: |
| 164 | + * 1、第三方平台开发者可以通过本接口,在旗下公众号将业务授权给你后,立即通过本接口检测公众号的自定义菜单配置,并通过接口再次给公众号设置好自动回复规则,以提升公众号运营者的业务体验。 |
| 165 | + * 2、本接口与自定义菜单查询接口的不同之处在于,本接口无论公众号的接口是如何设置的,都能查询到接口,而自定义菜单查询接口则仅能查询到使用API设置的菜单配置。 |
| 166 | + * 3、认证/未认证的服务号/订阅号,以及接口测试号,均拥有该接口权限。 |
| 167 | + * 4、从第三方平台的公众号登录授权机制上来说,该接口从属于消息与菜单权限集。 |
| 168 | + * 5、本接口中返回的图片/语音/视频为临时素材(临时素材每次获取都不同,3天内有效,通过素材管理-获取临时素材接口来获取这些素材),本接口返回的图文消息为永久素材素材(通过素材管理-获取永久素材接口来获取这些素材)。 |
| 169 | + * 接口调用请求说明: |
| 170 | + * http请求方式: GET(请使用https协议) |
| 171 | + * https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=ACCESS_TOKEN |
| 172 | + * </pre> |
| 173 | + */ |
| 174 | + @Override |
| 175 | + @GetMapping("/getSelfMenuInfo") |
| 176 | + public WxMpGetSelfMenuInfoResult getSelfMenuInfo() throws WxErrorException { |
| 177 | + return this.wxService.getMenuService().getSelfMenuInfo(); |
| 178 | + } |
| 179 | +} |
0 commit comments