自定义菜单加入对小程序的支持 #170

This commit is contained in:
Binary Wang
2017-03-30 19:30:36 +08:00
parent 7ce46ed0a2
commit 89870bbb92
2 changed files with 40 additions and 39 deletions

View File

@ -46,6 +46,8 @@ public class WxMenuGsonAdapter implements JsonSerializer<WxMenu>, JsonDeserializ
buttonJson.addProperty("key", button.getKey()); buttonJson.addProperty("key", button.getKey());
buttonJson.addProperty("url", button.getUrl()); buttonJson.addProperty("url", button.getUrl());
buttonJson.addProperty("media_id", button.getMediaId()); buttonJson.addProperty("media_id", button.getMediaId());
buttonJson.addProperty("appid", button.getAppId());
buttonJson.addProperty("pagepath", button.getPagePath());
if (button.getSubButtons() != null && button.getSubButtons().size() > 0) { if (button.getSubButtons() != null && button.getSubButtons().size() > 0) {
JsonArray buttonArray = new JsonArray(); JsonArray buttonArray = new JsonArray();
for (WxMenuButton sub_button : button.getSubButtons()) { for (WxMenuButton sub_button : button.getSubButtons()) {
@ -114,6 +116,8 @@ public class WxMenuGsonAdapter implements JsonSerializer<WxMenu>, JsonDeserializ
button.setUrl(GsonHelper.getString(json, "url")); button.setUrl(GsonHelper.getString(json, "url"));
button.setType(GsonHelper.getString(json, "type")); button.setType(GsonHelper.getString(json, "type"));
button.setMediaId(GsonHelper.getString(json, "media_id")); button.setMediaId(GsonHelper.getString(json, "media_id"));
button.setAppId(GsonHelper.getString(json, "appid"));
button.setPagePath(GsonHelper.getString(json, "pagepath"));
return button; return button;
} }

View File

@ -94,42 +94,37 @@ public class WxMpMenuServiceImplTest {
@Test @Test
public void testCreateMenu_by_json() throws WxErrorException { public void testCreateMenu_by_json() throws WxErrorException {
String a = "{\n" String a = "{\n" +
+ " \"menu\": {\n" " \"button\": [\n" +
+ " \"button\": [\n" " {\n" +
+ " {\n" " \"type\": \"click\",\n" +
+ " \"type\": \"click\",\n" " \"name\": \"今日歌曲\",\n" +
+ " \"name\": \"今日歌曲\",\n" " \"key\": \"V1001_TODAY_MUSIC\"\n" +
+ " \"key\": \"V1001_TODAY_MUSIC\"\n" " },\n" +
+ " },\n" " {\n" +
+ " {\n" " \"name\": \"菜单\",\n" +
+ " \"type\": \"click\",\n" " \"sub_button\": [\n" +
+ " \"name\": \"歌手简介\",\n" " {\n" +
+ " \"key\": \"V1001_TODAY_SINGER\"\n" " \"type\": \"view\",\n" +
+ " },\n" " \"name\": \"搜索\",\n" +
+ " {\n" " \"url\": \"http://www.soso.com/\"\n" +
+ " \"name\": \"菜单\",\n" " },\n" +
+ " \"sub_button\": [\n" " {\n" +
+ " {\n" " \"type\": \"miniprogram\",\n" +
+ " \"type\": \"view\",\n" " \"name\": \"wxa\",\n" +
+ " \"name\": \"搜索\",\n" " \"url\": \"http://mp.weixin.qq.com\",\n" +
+ " \"url\": \"http://www.soso.com/\"\n" " \"appid\": \"wx286b93c14bbf93aa\",\n" +
+ " },\n" " \"pagepath\": \"pages/lunar/index.html\"\n" +
+ " {\n" " },\n" +
+ " \"type\": \"view\",\n" " {\n" +
+ " \"name\": \"视频\",\n" " \"type\": \"click\",\n" +
+ " \"url\": \"http://v.qq.com/\"\n" " \"name\": \"赞一下我们\",\n" +
+ " },\n" " \"key\": \"V1001_GOOD\"\n" +
+ " {\n" " }\n" +
+ " \"type\": \"click\",\n" " ]\n" +
+ " \"name\": \"赞一下我们\",\n" " }\n" +
+ " \"key\": \"V1001_GOOD\"\n" " ]\n" +
+ " }\n" "}";
+ " ]\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ "}";
WxMenu menu = WxMenu.fromJson(a); WxMenu menu = WxMenu.fromJson(a);
System.out.println(menu.toJson()); System.out.println(menu.toJson());
@ -157,9 +152,11 @@ public class WxMpMenuServiceImplTest {
button1.setKey("V1001_TODAY_MUSIC"); button1.setKey("V1001_TODAY_MUSIC");
WxMenuButton button2 = new WxMenuButton(); WxMenuButton button2 = new WxMenuButton();
button2.setType(WxConsts.BUTTON_CLICK); button2.setType(WxConsts.BUTTON_MINIPROGRAM);
button2.setName("歌手简介"); button2.setName("小程序");
button2.setKey("V1001_TODAY_SINGER"); button2.setAppId("wx286b93c14bbf93aa");
button2.setPagePath("pages/lunar/index.html");
button2.setUrl("http://mp.weixin.qq.com");
WxMenuButton button3 = new WxMenuButton(); WxMenuButton button3 = new WxMenuButton();
button3.setName("菜单"); button3.setName("菜单");