Skip to content

Commit b805a99

Browse files
committed
1.WxMenuRule增加language字段
2.更改了WxMenu的toJson方法使menurule转换成正确的字符串 3.WxMenuTest中增加了相应的测试用例
1 parent d253bde commit b805a99

File tree

3 files changed

+79
-12
lines changed

3 files changed

+79
-12
lines changed

weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/WxMenu.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package me.chanjar.weixin.common.bean;
22

3+
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
4+
import org.apache.commons.codec.Charsets;
5+
36
import java.io.InputStream;
47
import java.io.InputStreamReader;
58
import java.io.Serializable;
69
import java.util.ArrayList;
710
import java.util.List;
811

9-
import org.apache.commons.codec.Charsets;
10-
11-
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
12-
1312
/**
1413
* 企业号菜单
1514
* @author Daniel Qian
@@ -136,6 +135,7 @@ public static class WxMenuRule {
136135
private String province;
137136
private String city;
138137
private String clientPlatformType;
138+
private String language;
139139

140140
public String getGroupId() {
141141
return groupId;
@@ -184,8 +184,16 @@ public String getClientPlatformType() {
184184
public void setClientPlatformType(String clientPlatformType) {
185185
this.clientPlatformType = clientPlatformType;
186186
}
187-
188-
@Override
187+
188+
public String getLanguage() {
189+
return language;
190+
}
191+
192+
public void setLanguage(String language) {
193+
this.language = language;
194+
}
195+
196+
@Override
189197
public String toString() {
190198
return "matchrule:{" +
191199
"group_id='" + groupId + '\'' +
@@ -194,6 +202,7 @@ public String toString() {
194202
", province" + province + '\'' +
195203
", city" + city + '\'' +
196204
", client_platform_type" + clientPlatformType + '\'' +
205+
", language" + language + '\'' +
197206
"}";
198207
}
199208
}

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

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

11-
import java.lang.reflect.Type;
12-
13-
import com.google.gson.Gson;
1411
import com.google.gson.JsonArray;
1512
import com.google.gson.JsonDeserializationContext;
1613
import com.google.gson.JsonDeserializer;
@@ -19,9 +16,10 @@
1916
import com.google.gson.JsonParseException;
2017
import com.google.gson.JsonSerializationContext;
2118
import com.google.gson.JsonSerializer;
22-
2319
import me.chanjar.weixin.common.bean.WxMenu;
2420

21+
import java.lang.reflect.Type;
22+
2523
/**
2624
*
2725
* @author Daniel Qian
@@ -40,8 +38,7 @@ public JsonElement serialize(WxMenu menu, Type typeOfSrc, JsonSerializationConte
4038
json.add("button", buttonArray);
4139

4240
if (menu.getMatchRule() != null) {
43-
Gson gson = new Gson();
44-
json.add("matchrule", gson.toJsonTree(menu.getMatchRule()));
41+
json.add("matchrule", convertToJson(menu.getMatchRule()));
4542
}
4643

4744
return json;
@@ -63,6 +60,18 @@ protected JsonObject convertToJson(WxMenu.WxMenuButton button) {
6360
return buttonJson;
6461
}
6562

63+
protected JsonObject convertToJson(WxMenu.WxMenuRule menuRule){
64+
JsonObject matchRule = new JsonObject();
65+
matchRule.addProperty("group_id",menuRule.getGroupId());
66+
matchRule.addProperty("sex",menuRule.getSex());
67+
matchRule.addProperty("country",menuRule.getCountry());
68+
matchRule.addProperty("province",menuRule.getProvince());
69+
matchRule.addProperty("city",menuRule.getCity());
70+
matchRule.addProperty("client_platform_type",menuRule.getClientPlatformType());
71+
matchRule.addProperty("language",menuRule.getLanguage());
72+
return matchRule;
73+
}
74+
6675
public WxMenu deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
6776
/*
6877
* 操蛋的微信

weixin-java-common/src/test/java/me/chanjar/weixin/common/bean/WxMenuTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,29 @@ public void testToJson(String json) {
5555

5656
Assert.assertEquals(menu.toJson(), json);
5757
}
58+
59+
@Test(dataProvider = "wxAddConditionalMenu")
60+
public void testAddConditionalToJson(String json) {
61+
WxMenu menu = new WxMenu();
62+
WxMenuButton button1 = new WxMenuButton();
63+
button1.setType("click");
64+
button1.setName("今日歌曲");
65+
button1.setKey("V1001_TODAY_MUSIC");
66+
67+
menu.getButtons().add(button1);
68+
69+
WxMenu.WxMenuRule wxMenuRule = new WxMenu.WxMenuRule();
70+
wxMenuRule.setGroupId("2");
71+
wxMenuRule.setSex("1");
72+
wxMenuRule.setCountry("中国");
73+
wxMenuRule.setProvince("广东");
74+
wxMenuRule.setCity("广州");
75+
wxMenuRule.setClientPlatformType("2");
76+
wxMenuRule.setLanguage("zh_CN");
77+
menu.setMatchRule(wxMenuRule);
78+
79+
Assert.assertEquals(menu.toJson(), json);
80+
}
5881

5982
@DataProvider
6083
public Object[][] wxReturnMenu() {
@@ -106,5 +129,31 @@ public Object[][] menuJson() {
106129
new Object[] { json }
107130
};
108131
}
132+
133+
@DataProvider(name = "wxAddConditionalMenu")
134+
public Object[][] addConditionalMenuJson(){
135+
String json =
136+
"{"
137+
+"\"button\":["
138+
+"{"
139+
+"\"type\":\"click\","
140+
+"\"name\":\"今日歌曲\","
141+
+"\"key\":\"V1001_TODAY_MUSIC\""
142+
+"}"
143+
+"],"
144+
+"\"matchrule\":{"
145+
+"\"group_id\":\"2\","
146+
+"\"sex\":\"1\","
147+
+"\"country\":\"中国\","
148+
+"\"province\":\"广东\","
149+
+"\"city\":\"广州\","
150+
+"\"client_platform_type\":\"2\","
151+
+"\"language\":\"zh_CN\""
152+
+"}"
153+
+"}";
154+
return new Object[][]{
155+
new Object[]{json}
156+
};
157+
}
109158

110159
}

0 commit comments

Comments
 (0)