Skip to content

Commit 34fda26

Browse files
committed
add conditional menu method
1 parent 780074f commit 34fda26

File tree

4 files changed

+138
-7
lines changed

4 files changed

+138
-7
lines changed

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

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
import java.io.InputStream;
44
import java.io.InputStreamReader;
55
import java.io.Serializable;
6-
import java.nio.charset.Charset;
76
import java.util.ArrayList;
87
import java.util.List;
98

10-
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
119
import org.apache.commons.codec.Charsets;
1210

11+
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
12+
1313
/**
1414
* 企业号菜单
1515
* @author Daniel Qian
@@ -19,6 +19,8 @@ public class WxMenu implements Serializable {
1919

2020
private List<WxMenuButton> buttons = new ArrayList<WxMenuButton>();
2121

22+
private WxMenuRule matchrule;
23+
2224
public List<WxMenuButton> getButtons() {
2325
return buttons;
2426
}
@@ -27,6 +29,14 @@ public void setButtons(List<WxMenuButton> buttons) {
2729
this.buttons = buttons;
2830
}
2931

32+
public WxMenuRule getMatchrule() {
33+
return matchrule;
34+
}
35+
36+
public void setMatchrule(WxMenuRule matchrule) {
37+
this.matchrule = matchrule;
38+
}
39+
3040
public String toJson() {
3141
return WxGsonBuilder.create().toJson(this);
3242
}
@@ -118,5 +128,74 @@ public String toString() {
118128
'}';
119129
}
120130
}
121-
131+
132+
public static class WxMenuRule {
133+
private String groupId;
134+
private String sex;
135+
private String country;
136+
private String province;
137+
private String city;
138+
private String clientPlatformType;
139+
140+
public String getGroupId() {
141+
return groupId;
142+
}
143+
144+
public void setGroupId(String groupId) {
145+
this.groupId = groupId;
146+
}
147+
148+
public String getSex() {
149+
return sex;
150+
}
151+
152+
public void setSex(String sex) {
153+
this.sex = sex;
154+
}
155+
156+
public String getCountry() {
157+
return country;
158+
}
159+
160+
public void setCountry(String country) {
161+
this.country = country;
162+
}
163+
164+
public String getProvince() {
165+
return province;
166+
}
167+
168+
public void setProvince(String province) {
169+
this.province = province;
170+
}
171+
172+
public String getCity() {
173+
return city;
174+
}
175+
176+
public void setCity(String city) {
177+
this.city = city;
178+
}
179+
180+
public String getClientPlatformType() {
181+
return clientPlatformType;
182+
}
183+
184+
public void setClientPlatformType(String clientPlatformType) {
185+
this.clientPlatformType = clientPlatformType;
186+
}
187+
188+
@Override
189+
public String toString() {
190+
return "matchrule:{" +
191+
"group_id='" + groupId + '\'' +
192+
", sex='" + sex + '\'' +
193+
", country" + country + '\'' +
194+
", province" + province + '\'' +
195+
", city" + city + '\'' +
196+
", client_platform_type" + clientPlatformType + '\'' +
197+
"}";
198+
}
199+
}
200+
122201
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
import java.lang.reflect.Type;
1212

13-
import me.chanjar.weixin.common.bean.WxMenu;
14-
13+
import com.google.gson.Gson;
1514
import com.google.gson.JsonArray;
1615
import com.google.gson.JsonDeserializationContext;
1716
import com.google.gson.JsonDeserializer;
@@ -21,6 +20,8 @@
2120
import com.google.gson.JsonSerializationContext;
2221
import com.google.gson.JsonSerializer;
2322

23+
import me.chanjar.weixin.common.bean.WxMenu;
24+
2425
/**
2526
*
2627
* @author Daniel Qian
@@ -38,6 +39,11 @@ public JsonElement serialize(WxMenu menu, Type typeOfSrc, JsonSerializationConte
3839
}
3940
json.add("button", buttonArray);
4041

42+
if (menu.getMatchrule() != null) {
43+
Gson gson = new Gson();
44+
json.add("matchrule", gson.toJsonTree(menu.getMatchrule()));
45+
}
46+
4147
return json;
4248
}
4349

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,8 @@ public interface WxMpService {
324324
* <pre>
325325
* 自定义菜单创建接口
326326
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单创建接口
327+
* 如果要创建个性化菜单,请设置matchrule属性
328+
* 详情请见:http://mp.weixin.qq.com/wiki/0/c48ccd12b69ae023159b4bfaa7c39c20.html
327329
* </pre>
328330
* @param menu
329331
* @throws WxErrorException
@@ -339,6 +341,16 @@ public interface WxMpService {
339341
*/
340342
public void menuDelete() throws WxErrorException;
341343

344+
/**
345+
* <pre>
346+
* 删除个性化菜单接口
347+
* 详情请见: http://mp.weixin.qq.com/wiki/0/c48ccd12b69ae023159b4bfaa7c39c20.html
348+
* </pre>
349+
* @param menuid
350+
* @throws WxErrorException
351+
*/
352+
public void menuDelete(String menuid) throws WxErrorException;
353+
342354
/**
343355
* <pre>
344356
* 自定义菜单查询接口
@@ -348,6 +360,16 @@ public interface WxMpService {
348360
* @throws WxErrorException
349361
*/
350362
public WxMenu menuGet() throws WxErrorException;
363+
364+
/**
365+
* <pre>
366+
* 测试个性化菜单匹配结果
367+
* 详情请见: http://mp.weixin.qq.com/wiki/0/c48ccd12b69ae023159b4bfaa7c39c20.html
368+
* </pre>
369+
* @param userid 可以是粉丝的OpenID,也可以是粉丝的微信号。
370+
* @throws WxErrorException
371+
*/
372+
public WxMenu menuTryMatch(String userid) throws WxErrorException;
351373

352374
/**
353375
* <pre>

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,14 +230,24 @@ public void customMessageSend(WxMpCustomMessage message) throws WxErrorException
230230
}
231231

232232
public void menuCreate(WxMenu menu) throws WxErrorException {
233-
String url = "https://api.weixin.qq.com/cgi-bin/menu/create";
234-
execute(new SimplePostRequestExecutor(), url, menu.toJson());
233+
if (menu.getMatchrule() != null) {
234+
String url = "https://api.weixin.qq.com/cgi-bin/menu/addconditional";
235+
execute(new SimplePostRequestExecutor(), url, menu.toJson());
236+
} else {
237+
String url = "https://api.weixin.qq.com/cgi-bin/menu/create";
238+
execute(new SimplePostRequestExecutor(), url, menu.toJson());
239+
}
235240
}
236241

237242
public void menuDelete() throws WxErrorException {
238243
String url = "https://api.weixin.qq.com/cgi-bin/menu/delete";
239244
execute(new SimpleGetRequestExecutor(), url, null);
240245
}
246+
247+
public void menuDelete(String menuid) throws WxErrorException {
248+
String url = "https://api.weixin.qq.com/cgi-bin/menu/delconditional";
249+
execute(new SimpleGetRequestExecutor(), url, "menuid=" + menuid);
250+
}
241251

242252
public WxMenu menuGet() throws WxErrorException {
243253
String url = "https://api.weixin.qq.com/cgi-bin/menu/get";
@@ -252,6 +262,20 @@ public WxMenu menuGet() throws WxErrorException {
252262
throw e;
253263
}
254264
}
265+
266+
public WxMenu menuTryMatch(String userid) throws WxErrorException {
267+
String url = "https://api.weixin.qq.com/cgi-bin/menu/trymatch";
268+
try {
269+
String resultContent = execute(new SimpleGetRequestExecutor(), url, "user_id=" + userid);
270+
return WxMenu.fromJson(resultContent);
271+
} catch (WxErrorException e) {
272+
// 46003 不存在的菜单数据 46002 不存在的菜单版本
273+
if (e.getError().getErrorCode() == 46003 || e.getError().getErrorCode() == 46002) {
274+
return null;
275+
}
276+
throw e;
277+
}
278+
}
255279

256280
public WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream) throws WxErrorException, IOException {
257281
return mediaUpload(mediaType, FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType));

0 commit comments

Comments
 (0)