Merge pull request #249 from tianmu/master

add conditional menu method
This commit is contained in:
Daniel Qian
2015-12-23 08:46:56 +08:00
4 changed files with 138 additions and 7 deletions

View File

@@ -324,6 +324,8 @@ public interface WxMpService {
* <pre>
* 自定义菜单创建接口
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单创建接口
* 如果要创建个性化菜单请设置matchrule属性
* 详情请见:http://mp.weixin.qq.com/wiki/0/c48ccd12b69ae023159b4bfaa7c39c20.html
* </pre>
* @param menu
* @throws WxErrorException
@@ -339,6 +341,16 @@ public interface WxMpService {
*/
public void menuDelete() throws WxErrorException;
/**
* <pre>
* 删除个性化菜单接口
* 详情请见: http://mp.weixin.qq.com/wiki/0/c48ccd12b69ae023159b4bfaa7c39c20.html
* </pre>
* @param menuid
* @throws WxErrorException
*/
public void menuDelete(String menuid) throws WxErrorException;
/**
* <pre>
* 自定义菜单查询接口
@@ -348,6 +360,16 @@ public interface WxMpService {
* @throws WxErrorException
*/
public WxMenu menuGet() throws WxErrorException;
/**
* <pre>
* 测试个性化菜单匹配结果
* 详情请见: http://mp.weixin.qq.com/wiki/0/c48ccd12b69ae023159b4bfaa7c39c20.html
* </pre>
* @param userid 可以是粉丝的OpenID也可以是粉丝的微信号。
* @throws WxErrorException
*/
public WxMenu menuTryMatch(String userid) throws WxErrorException;
/**
* <pre>

View File

@@ -230,14 +230,24 @@ public class WxMpServiceImpl implements WxMpService {
}
public void menuCreate(WxMenu menu) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/menu/create";
execute(new SimplePostRequestExecutor(), url, menu.toJson());
if (menu.getMatchRule() != null) {
String url = "https://api.weixin.qq.com/cgi-bin/menu/addconditional";
execute(new SimplePostRequestExecutor(), url, menu.toJson());
} else {
String url = "https://api.weixin.qq.com/cgi-bin/menu/create";
execute(new SimplePostRequestExecutor(), url, menu.toJson());
}
}
public void menuDelete() throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/menu/delete";
execute(new SimpleGetRequestExecutor(), url, null);
}
public void menuDelete(String menuid) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/menu/delconditional";
execute(new SimpleGetRequestExecutor(), url, "menuid=" + menuid);
}
public WxMenu menuGet() throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/menu/get";
@@ -252,6 +262,20 @@ public class WxMpServiceImpl implements WxMpService {
throw e;
}
}
public WxMenu menuTryMatch(String userid) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/menu/trymatch";
try {
String resultContent = execute(new SimpleGetRequestExecutor(), url, "user_id=" + userid);
return WxMenu.fromJson(resultContent);
} catch (WxErrorException e) {
// 46003 不存在的菜单数据 46002 不存在的菜单版本
if (e.getError().getErrorCode() == 46003 || e.getError().getErrorCode() == 46002) {
return null;
}
throw e;
}
}
public WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream) throws WxErrorException, IOException {
return mediaUpload(mediaType, FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType));