Skip to content

Commit 89870bb

Browse files
committed
自定义菜单加入对小程序的支持 #170
1 parent 7ce46ed commit 89870bb

File tree

2 files changed

+40
-39
lines changed

2 files changed

+40
-39
lines changed

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/WxMenuGsonAdapter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ protected JsonObject convertToJson(WxMenuButton button) {
4646
buttonJson.addProperty("key", button.getKey());
4747
buttonJson.addProperty("url", button.getUrl());
4848
buttonJson.addProperty("media_id", button.getMediaId());
49+
buttonJson.addProperty("appid", button.getAppId());
50+
buttonJson.addProperty("pagepath", button.getPagePath());
4951
if (button.getSubButtons() != null && button.getSubButtons().size() > 0) {
5052
JsonArray buttonArray = new JsonArray();
5153
for (WxMenuButton sub_button : button.getSubButtons()) {
@@ -114,6 +116,8 @@ protected WxMenuButton convertFromJson(JsonObject json) {
114116
button.setUrl(GsonHelper.getString(json, "url"));
115117
button.setType(GsonHelper.getString(json, "type"));
116118
button.setMediaId(GsonHelper.getString(json, "media_id"));
119+
button.setAppId(GsonHelper.getString(json, "appid"));
120+
button.setPagePath(GsonHelper.getString(json, "pagepath"));
117121
return button;
118122
}
119123

weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpMenuServiceImplTest.java

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -94,42 +94,37 @@ public void testDeleteConditionalMenu() throws WxErrorException {
9494

9595
@Test
9696
public void testCreateMenu_by_json() throws WxErrorException {
97-
String a = "{\n"
98-
+ " \"menu\": {\n"
99-
+ " \"button\": [\n"
100-
+ " {\n"
101-
+ " \"type\": \"click\",\n"
102-
+ " \"name\": \"今日歌曲\",\n"
103-
+ " \"key\": \"V1001_TODAY_MUSIC\"\n"
104-
+ " },\n"
105-
+ " {\n"
106-
+ " \"type\": \"click\",\n"
107-
+ " \"name\": \"歌手简介\",\n"
108-
+ " \"key\": \"V1001_TODAY_SINGER\"\n"
109-
+ " },\n"
110-
+ " {\n"
111-
+ " \"name\": \"菜单\",\n"
112-
+ " \"sub_button\": [\n"
113-
+ " {\n"
114-
+ " \"type\": \"view\",\n"
115-
+ " \"name\": \"搜索\",\n"
116-
+ " \"url\": \"http://www.soso.com/\"\n"
117-
+ " },\n"
118-
+ " {\n"
119-
+ " \"type\": \"view\",\n"
120-
+ " \"name\": \"视频\",\n"
121-
+ " \"url\": \"http://v.qq.com/\"\n"
122-
+ " },\n"
123-
+ " {\n"
124-
+ " \"type\": \"click\",\n"
125-
+ " \"name\": \"赞一下我们\",\n"
126-
+ " \"key\": \"V1001_GOOD\"\n"
127-
+ " }\n"
128-
+ " ]\n"
129-
+ " }\n"
130-
+ " ]\n"
131-
+ " }\n"
132-
+ "}";
97+
String a = "{\n" +
98+
" \"button\": [\n" +
99+
" {\n" +
100+
" \"type\": \"click\",\n" +
101+
" \"name\": \"今日歌曲\",\n" +
102+
" \"key\": \"V1001_TODAY_MUSIC\"\n" +
103+
" },\n" +
104+
" {\n" +
105+
" \"name\": \"菜单\",\n" +
106+
" \"sub_button\": [\n" +
107+
" {\n" +
108+
" \"type\": \"view\",\n" +
109+
" \"name\": \"搜索\",\n" +
110+
" \"url\": \"http://www.soso.com/\"\n" +
111+
" },\n" +
112+
" {\n" +
113+
" \"type\": \"miniprogram\",\n" +
114+
" \"name\": \"wxa\",\n" +
115+
" \"url\": \"http://mp.weixin.qq.com\",\n" +
116+
" \"appid\": \"wx286b93c14bbf93aa\",\n" +
117+
" \"pagepath\": \"pages/lunar/index.html\"\n" +
118+
" },\n" +
119+
" {\n" +
120+
" \"type\": \"click\",\n" +
121+
" \"name\": \"赞一下我们\",\n" +
122+
" \"key\": \"V1001_GOOD\"\n" +
123+
" }\n" +
124+
" ]\n" +
125+
" }\n" +
126+
" ]\n" +
127+
"}";
133128

134129
WxMenu menu = WxMenu.fromJson(a);
135130
System.out.println(menu.toJson());
@@ -157,9 +152,11 @@ public Object[][] getMenu() {
157152
button1.setKey("V1001_TODAY_MUSIC");
158153

159154
WxMenuButton button2 = new WxMenuButton();
160-
button2.setType(WxConsts.BUTTON_CLICK);
161-
button2.setName("歌手简介");
162-
button2.setKey("V1001_TODAY_SINGER");
155+
button2.setType(WxConsts.BUTTON_MINIPROGRAM);
156+
button2.setName("小程序");
157+
button2.setAppId("wx286b93c14bbf93aa");
158+
button2.setPagePath("pages/lunar/index.html");
159+
button2.setUrl("http://mp.weixin.qq.com");
163160

164161
WxMenuButton button3 = new WxMenuButton();
165162
button3.setName("菜单");

0 commit comments

Comments
 (0)