Skip to content

Commit 5018c31

Browse files
committed
修复完善菜单特别是个性化菜单的创建和删除相关代码
1 parent d81a5e3 commit 5018c31

File tree

4 files changed

+153
-73
lines changed

4 files changed

+153
-73
lines changed

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,13 @@
88
*/
99
package me.chanjar.weixin.common.util.json;
1010

11-
import java.lang.reflect.Type;
12-
13-
import com.google.gson.JsonArray;
14-
import com.google.gson.JsonDeserializationContext;
15-
import com.google.gson.JsonDeserializer;
16-
import com.google.gson.JsonElement;
17-
import com.google.gson.JsonObject;
18-
import com.google.gson.JsonParseException;
19-
import com.google.gson.JsonSerializationContext;
20-
import com.google.gson.JsonSerializer;
21-
11+
import com.google.gson.*;
2212
import me.chanjar.weixin.common.bean.menu.WxMenu;
2313
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
2414
import me.chanjar.weixin.common.bean.menu.WxMenuRule;
2515

16+
import java.lang.reflect.Type;
17+
2618

2719
/**
2820
* @author Daniel Qian
@@ -76,6 +68,18 @@ protected JsonObject convertToJson(WxMenuRule menuRule) {
7668
return matchRule;
7769
}
7870

71+
private WxMenuRule convertToRule(JsonObject json) {
72+
WxMenuRule menuRule = new WxMenuRule();
73+
menuRule.setTagId(GsonHelper.getString(json,"tag_id"));
74+
menuRule.setSex(GsonHelper.getString(json,"sex"));
75+
menuRule.setCountry(GsonHelper.getString(json,"country"));
76+
menuRule.setProvince(GsonHelper.getString(json,"province"));
77+
menuRule.setCity(GsonHelper.getString(json,"city"));
78+
menuRule.setClientPlatformType(GsonHelper.getString(json,"client_platform_type"));
79+
menuRule.setLanguage(GsonHelper.getString(json,"language"));
80+
return menuRule;
81+
}
82+
7983
@Override
8084
public WxMenu deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
8185
/*
@@ -84,8 +88,7 @@ public WxMenu deserialize(JsonElement json, Type typeOfT, JsonDeserializationCon
8488
* 查询菜单时是 { menu : { button : ... } }
8589
*/
8690
WxMenu menu = new WxMenu();
87-
JsonObject menuJson = json.getAsJsonObject().get("menu").getAsJsonObject();
88-
JsonArray buttonsJson = menuJson.get("button").getAsJsonArray();
91+
JsonArray buttonsJson = json.getAsJsonObject().get("menu").getAsJsonObject().get("button").getAsJsonArray();
8992
for (int i = 0; i < buttonsJson.size(); i++) {
9093
JsonObject buttonJson = buttonsJson.get(i).getAsJsonObject();
9194
WxMenuButton button = convertFromJson(buttonJson);

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpMenuService.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,24 @@ public interface WxMpMenuService {
1414
/**
1515
* <pre>
1616
* 自定义菜单创建接口
17-
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单创建接口
17+
* 详情请见:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141013&token=&lang=zh_CN
1818
* 如果要创建个性化菜单,请设置matchrule属性
19-
* 详情请见:http://mp.weixin.qq.com/wiki/0/c48ccd12b69ae023159b4bfaa7c39c20.html
19+
* 详情请见:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1455782296&token=&lang=zh_CN
2020
* </pre>
21+
* @return 如果是个性化菜单,则返回menuid,否则返回null
2122
*/
22-
void menuCreate(WxMenu menu) throws WxErrorException;
23+
String menuCreate(WxMenu menu) throws WxErrorException;
24+
25+
/**
26+
* <pre>
27+
* 自定义菜单创建接口
28+
* 详情请见: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141013&token=&lang=zh_CN
29+
* 如果要创建个性化菜单,请设置matchrule属性
30+
* 详情请见:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1455782296&token=&lang=zh_CN
31+
* </pre>
32+
* @return 如果是个性化菜单,则返回menuid,否则返回null
33+
*/
34+
String menuCreate(String json) throws WxErrorException;
2335

2436
/**
2537
* <pre>

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpMenuServiceImpl.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package me.chanjar.weixin.mp.api.impl;
22

33
import com.google.gson.JsonObject;
4+
import com.google.gson.JsonParser;
45
import me.chanjar.weixin.common.bean.menu.WxMenu;
56
import me.chanjar.weixin.common.exception.WxErrorException;
67
import me.chanjar.weixin.mp.api.WxMpMenuService;
@@ -24,7 +25,7 @@ public WxMpMenuServiceImpl(WxMpService wxMpService) {
2425
}
2526

2627
@Override
27-
public void menuCreate(WxMenu menu) throws WxErrorException {
28+
public String menuCreate(WxMenu menu) throws WxErrorException {
2829
String menuJson = menu.toJson();
2930
String url = API_URL_PREFIX + "/create";
3031
if (menu.getMatchRule() != null) {
@@ -35,6 +36,29 @@ public void menuCreate(WxMenu menu) throws WxErrorException {
3536

3637
String result = this.wxMpService.post(url, menuJson);
3738
log.debug("创建菜单:{},结果:{}", menuJson, result);
39+
40+
if (menu.getMatchRule() != null) {
41+
return new JsonParser().parse(result).getAsJsonObject().get("menuid").getAsString();
42+
}
43+
44+
return null;
45+
}
46+
47+
@Override
48+
public String menuCreate(String json) throws WxErrorException {
49+
JsonParser jsonParser = new JsonParser();
50+
JsonObject jsonObject = jsonParser.parse(json).getAsJsonObject();
51+
String url = API_URL_PREFIX + "/create";
52+
if (jsonObject.get("matchrule") != null) {
53+
url = API_URL_PREFIX + "/addconditional";
54+
}
55+
56+
String result = this.wxMpService.post(url, json);
57+
if (jsonObject.get("matchrule") != null) {
58+
return jsonParser.parse(result).getAsJsonObject().get("menuid").getAsString();
59+
}
60+
61+
return null;
3862
}
3963

4064
@Override
@@ -50,7 +74,7 @@ public void menuDelete(String menuId) throws WxErrorException {
5074
JsonObject jsonObject = new JsonObject();
5175
jsonObject.addProperty("menuid", menuId);
5276
String result = this.wxMpService.post(url, jsonObject.toString());
53-
log.debug("根据MeunId({})删除菜单结果:{}", menuId, result);
77+
log.debug("根据MeunId({})删除个性化菜单结果:{}", menuId, result);
5478
}
5579

5680
@Override
@@ -77,7 +101,7 @@ public WxMenu menuTryMatch(String userid) throws WxErrorException {
77101
String resultContent = this.wxMpService.post(url, jsonObject.toString());
78102
return WxMenu.fromJson(resultContent);
79103
} catch (WxErrorException e) {
80-
// 46003 不存在的菜单数据 46002 不存在的菜单版本
104+
// 46003 不存在的菜单数据46002 不存在的菜单版本
81105
if (e.getError().getErrorCode() == 46003
82106
|| e.getError().getErrorCode() == 46002) {
83107
return null;

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

Lines changed: 95 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,22 @@
88
import me.chanjar.weixin.mp.api.ApiTestModule;
99
import me.chanjar.weixin.mp.api.WxMpService;
1010
import me.chanjar.weixin.mp.bean.menu.WxMpGetSelfMenuInfoResult;
11-
import org.testng.Assert;
12-
import org.testng.annotations.DataProvider;
13-
import org.testng.annotations.Guice;
14-
import org.testng.annotations.Test;
11+
import org.testng.*;
12+
import org.testng.annotations.*;
1513

1614
/**
1715
* 测试菜单
16+
*
1817
* @author chanjarster
1918
* @author Binary Wang
20-
*
2119
*/
22-
@Test(groups="menuAPI")
20+
@Test(groups = "menuAPI")
2321
@Guice(modules = ApiTestModule.class)
2422
public class WxMpMenuServiceImplTest {
2523

2624
@Inject
2725
protected WxMpService wxService;
26+
private String menuId = null;
2827

2928
@Test(dataProvider = "menu")
3029
public void testMenuCreate(WxMenu wxMenu) throws WxErrorException {
@@ -44,69 +43,111 @@ public void testGetSelfMenuInfo() throws Exception {
4443
System.out.println(selfMenuInfo);
4544
}
4645

46+
@Test
47+
public void testCreateConditionalMenu() throws WxErrorException {
48+
String json = "{\n" +
49+
" \"button\":[\n" +
50+
" { \n" +
51+
" \"type\":\"click\",\n" +
52+
" \"name\":\"今日歌曲\",\n" +
53+
" \"key\":\"V1001_TODAY_MUSIC\" \n" +
54+
" },\n" +
55+
" { \n" +
56+
" \"name\":\"菜单\",\n" +
57+
" \"sub_button\":[\n" +
58+
" { \n" +
59+
" \"type\":\"view\",\n" +
60+
" \"name\":\"搜索\",\n" +
61+
" \"url\":\"http://www.soso.com/\"\n" +
62+
" },\n" +
63+
" {\n" +
64+
" \"type\":\"view\",\n" +
65+
" \"name\":\"视频\",\n" +
66+
" \"url\":\"http://v.qq.com/\"\n" +
67+
" },\n" +
68+
" {\n" +
69+
" \"type\":\"click\",\n" +
70+
" \"name\":\"赞一下我们\",\n" +
71+
" \"key\":\"V1001_GOOD\"\n" +
72+
" }]\n" +
73+
" }],\n" +
74+
"\"matchrule\":{\n" +
75+
" \"tag_id\":\"2\",\n" +
76+
" \"sex\":\"1\",\n" +
77+
" \"country\":\"中国\",\n" +
78+
" \"province\":\"广东\",\n" +
79+
" \"city\":\"广州\",\n" +
80+
" \"client_platform_type\":\"2\",\n" +
81+
" \"language\":\"zh_CN\"\n" +
82+
" }\n" +
83+
"}";
84+
85+
this.menuId = this.wxService.getMenuService().menuCreate(json);
86+
System.out.println(this.menuId);
87+
}
88+
89+
@Test(dependsOnMethods = {"testCreateConditionalMenu"})
90+
public void testDeleteConditionalMenu() throws WxErrorException {
91+
this.wxService.getMenuService().menuDelete(menuId);
92+
}
93+
4794
@Test
4895
public void testCreateMenu2() throws WxErrorException {
4996
String a = "{\n"
50-
+ " \"menu\": {\n"
51-
+ " \"button\": [\n"
52-
+ " {\n"
53-
+ " \"type\": \"click\",\n"
54-
+ " \"name\": \"今日歌曲\",\n"
55-
+ " \"key\": \"V1001_TODAY_MUSIC\"\n"
56-
+ " },\n"
57-
+ " {\n"
58-
+ " \"type\": \"click\",\n"
59-
+ " \"name\": \"歌手简介\",\n"
60-
+ " \"key\": \"V1001_TODAY_SINGER\"\n"
61-
+ " },\n"
62-
+ " {\n"
63-
+ " \"name\": \"菜单\",\n"
64-
+ " \"sub_button\": [\n"
65-
+ " {\n"
66-
+ " \"type\": \"view\",\n"
67-
+ " \"name\": \"搜索\",\n"
68-
+ " \"url\": \"http://www.soso.com/\"\n"
69-
+ " },\n"
70-
+ " {\n"
71-
+ " \"type\": \"view\",\n"
72-
+ " \"name\": \"视频\",\n"
73-
+ " \"url\": \"http://v.qq.com/\"\n"
74-
+ " },\n"
75-
+ " {\n"
76-
+ " \"type\": \"click\",\n"
77-
+ " \"name\": \"赞一下我们\",\n"
78-
+ " \"key\": \"V1001_GOOD\"\n"
79-
+ " }\n"
80-
+ " ]\n"
81-
+ " }\n"
82-
+ " ]\n"
83-
+ " }\n"
84-
+ "}";
97+
+ " \"menu\": {\n"
98+
+ " \"button\": [\n"
99+
+ " {\n"
100+
+ " \"type\": \"click\",\n"
101+
+ " \"name\": \"今日歌曲\",\n"
102+
+ " \"key\": \"V1001_TODAY_MUSIC\"\n"
103+
+ " },\n"
104+
+ " {\n"
105+
+ " \"type\": \"click\",\n"
106+
+ " \"name\": \"歌手简介\",\n"
107+
+ " \"key\": \"V1001_TODAY_SINGER\"\n"
108+
+ " },\n"
109+
+ " {\n"
110+
+ " \"name\": \"菜单\",\n"
111+
+ " \"sub_button\": [\n"
112+
+ " {\n"
113+
+ " \"type\": \"view\",\n"
114+
+ " \"name\": \"搜索\",\n"
115+
+ " \"url\": \"http://www.soso.com/\"\n"
116+
+ " },\n"
117+
+ " {\n"
118+
+ " \"type\": \"view\",\n"
119+
+ " \"name\": \"视频\",\n"
120+
+ " \"url\": \"http://v.qq.com/\"\n"
121+
+ " },\n"
122+
+ " {\n"
123+
+ " \"type\": \"click\",\n"
124+
+ " \"name\": \"赞一下我们\",\n"
125+
+ " \"key\": \"V1001_GOOD\"\n"
126+
+ " }\n"
127+
+ " ]\n"
128+
+ " }\n"
129+
+ " ]\n"
130+
+ " }\n"
131+
+ "}";
85132

86133
WxMenu menu = WxMenu.fromJson(a);
87134
System.out.println(menu.toJson());
88135
this.wxService.getMenuService().menuCreate(menu);
89136
}
90137

91-
@Test(dependsOnMethods = { "testMenuCreate"})
138+
@Test(dependsOnMethods = {"testMenuCreate"})
92139
public void testMenuGet() throws WxErrorException {
93140
WxMenu wxMenu = this.wxService.getMenuService().menuGet();
94141
Assert.assertNotNull(wxMenu);
95142
System.out.println(wxMenu.toJson());
96143
}
97144

98-
@Test(dependsOnMethods = { "testMenuGet"})
145+
@Test(dependsOnMethods = {"testMenuGet"})
99146
public void testMenuDelete() throws WxErrorException {
100147
this.wxService.getMenuService().menuDelete();
101148
}
102149

103-
@Test
104-
public void testDeleteConditionalMenu() throws WxErrorException {
105-
String menuId = "123";
106-
this.wxService.getMenuService().menuDelete(menuId);
107-
}
108-
109-
@DataProvider(name="menu")
150+
@DataProvider(name = "menu")
110151
public Object[][] getMenu() {
111152
WxMenu menu = new WxMenu();
112153
WxMenuButton button1 = new WxMenuButton();
@@ -145,10 +186,10 @@ public Object[][] getMenu() {
145186
button3.getSubButtons().add(button32);
146187
button3.getSubButtons().add(button33);
147188

148-
return new Object[][] {
149-
new Object[] {
150-
menu
151-
}
189+
return new Object[][]{
190+
new Object[]{
191+
menu
192+
}
152193
};
153194

154195
}

0 commit comments

Comments
 (0)