我们引入的开源微信开发的依赖中,对于微信菜单的创建所需要的参数进行了很好的封装,我们只需要按照微信文档开发的规范,进行参数的设置就好了--
关于微信菜单开发的格式:
{ "button":[ { "type":"click", "name":"今日歌曲", "key":"V1001_TODAY_MUSIC" }, { "name":"菜单", "sub_button":[ { "type":"view", "name":"搜索", "url":"http://www.soso.com/" }, { "type":"miniprogram", "name":"wxa", "url":"http://mp.weixin.qq.com", "appid":"wx286b93c14bbf93aa", "pagepath":"pages/lunar/index" }, { "type":"click", "name":"赞一下我们", "key":"V1001_GOOD" }] }] }
对于这种格式,我们的sdk将其封装在了 WxMenu 类中
然后调用 WxMpServiceImpl 的 getMenuService().menuCreate(menu); 方法
代码如下(红色需要配置成我们自己的)
@Autowired private WxMpInMemoryConfigStorage config; @Autowired private WxMpService wxMpService; @RequestMapping("/wx/menu") public void createMenu() throws WxErrorException { WxMenu menu = new WxMenu(); WxMenuButton button1 = new WxMenuButton(); button1.setType(WxConsts.MenuButtonType.CLICK); button1.setName("今日歌曲"); button1.setKey("V1001_TODAY_MUSIC"); // WxMenuButton button2 = new WxMenuButton(); // button2.setType(WxConsts.MenuButtonType.MINIPROGRAM); // button2.setName("小程序"); // button2.setAppId("wx286b93c14bbf93aa"); // button2.setPagePath("pages/lunar/index.html"); // button2.setUrl("http://mp.weixin.qq.com"); WxMenuButton button3 = new WxMenuButton(); button3.setName("菜单"); menu.getButtons().add(button1); // menu.getButtons().add(button2); menu.getButtons().add(button3); WxMenuButton button31 = new WxMenuButton(); button31.setType(WxConsts.MenuButtonType.VIEW); button31.setName("搜索"); button31.setUrl("http://www.soso.com/"); WxMenuButton button32 = new WxMenuButton(); button32.setType(WxConsts.MenuButtonType.VIEW); button32.setName("视频"); button32.setUrl("http://v.qq.com/"); WxMenuButton button33 = new WxMenuButton(); button33.setType(WxConsts.MenuButtonType.CLICK); button33.setName("爱你呦"); button33.setKey("V1001_GOOD"); button3.getSubButtons().add(button31); button3.getSubButtons().add(button32); button3.getSubButtons().add(button33); //我们可以将config配置在xml文件中管理 config.setAppId("wx426aad126775582c"); config.setSecret("d79b69215c50cee0c848415eb34c659a"); wxMpService.setWxMpConfigStorage(config); wxMpService.getMenuService().menuCreate(menu); }