改造查询菜单接口,以支持个性化菜单的conditionalmenu和menuid属性, #126

This commit is contained in:
Binary Wang 2017-01-17 15:15:21 +08:00
parent 5018c31e60
commit 39c7f7cd9f
4 changed files with 100 additions and 5 deletions

View File

@ -3,6 +3,7 @@ package me.chanjar.weixin.mp.api;
import me.chanjar.weixin.common.bean.menu.WxMenu; import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.bean.menu.WxMpGetSelfMenuInfoResult; import me.chanjar.weixin.mp.bean.menu.WxMpGetSelfMenuInfoResult;
import me.chanjar.weixin.mp.bean.menu.WxMpMenu;
/** /**
* 菜单相关操作接口 * 菜单相关操作接口
@ -54,10 +55,10 @@ public interface WxMpMenuService {
/** /**
* <pre> * <pre>
* 自定义菜单查询接口 * 自定义菜单查询接口
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单查询接口 * 详情请见 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141014&token=&lang=zh_CN
* </pre> * </pre>
*/ */
WxMenu menuGet() throws WxErrorException; WxMpMenu menuGet() throws WxErrorException;
/** /**
* <pre> * <pre>

View File

@ -7,6 +7,7 @@ import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpMenuService; import me.chanjar.weixin.mp.api.WxMpMenuService;
import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.menu.WxMpGetSelfMenuInfoResult; import me.chanjar.weixin.mp.bean.menu.WxMpGetSelfMenuInfoResult;
import me.chanjar.weixin.mp.bean.menu.WxMpMenu;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -78,11 +79,11 @@ public class WxMpMenuServiceImpl implements WxMpMenuService {
} }
@Override @Override
public WxMenu menuGet() throws WxErrorException { public WxMpMenu menuGet() throws WxErrorException {
String url = API_URL_PREFIX + "/get"; String url = API_URL_PREFIX + "/get";
try { try {
String resultContent = this.wxMpService.get(url, null); String resultContent = this.wxMpService.get(url, null);
return WxMenu.fromJson(resultContent); return WxMpMenu.fromJson(resultContent);
} catch (WxErrorException e) { } catch (WxErrorException e) {
// 46003 不存在的菜单数据 // 46003 不存在的菜单数据
if (e.getError().getErrorCode() == 46003) { if (e.getError().getErrorCode() == 46003) {

View File

@ -0,0 +1,92 @@
package me.chanjar.weixin.mp.bean.menu;
import com.google.gson.annotations.SerializedName;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.bean.menu.WxMenuRule;
import me.chanjar.weixin.common.util.ToStringUtils;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import java.util.List;
/**
* <pre>
* 公众号专用的菜单类可能包含个性化菜单
* Created by Binary Wang on 2017-1-17.
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
* </pre>
*/
public class WxMpMenu {
@SerializedName("menu")
private WxMpConditionalMenu menu;
@SerializedName("conditionalmenu")
private List<WxMpConditionalMenu> conditionalMenu;
public static WxMpMenu fromJson(String json) {
return WxGsonBuilder.create().fromJson(json, WxMpMenu.class);
}
public WxMpConditionalMenu getMenu() {
return menu;
}
public void setMenu(WxMpConditionalMenu menu) {
this.menu = menu;
}
public List<WxMpConditionalMenu> getConditionalMenu() {
return conditionalMenu;
}
public void setConditionalMenu(List<WxMpConditionalMenu> conditionalMenu) {
this.conditionalMenu = conditionalMenu;
}
@Override
public String toString() {
return ToStringUtils.toSimpleString(this);
}
public String toJson() {
return WxGsonBuilder.create().toJson(this);
}
public static class WxMpConditionalMenu {
@SerializedName("button")
private List<WxMenuButton> buttons;
@SerializedName("matchrule")
private WxMenuRule rule;
@SerializedName("menuid")
private String menuId;
@Override
public String toString() {
return ToStringUtils.toSimpleString(this);
}
public List<WxMenuButton> getButtons() {
return buttons;
}
public void setButtons(List<WxMenuButton> buttons) {
this.buttons = buttons;
}
public WxMenuRule getRule() {
return rule;
}
public void setRule(WxMenuRule rule) {
this.rule = rule;
}
public String getMenuId() {
return menuId;
}
public void setMenuId(String menuId) {
this.menuId = menuId;
}
}
}

View File

@ -8,6 +8,7 @@ import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.ApiTestModule; import me.chanjar.weixin.mp.api.ApiTestModule;
import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.menu.WxMpGetSelfMenuInfoResult; import me.chanjar.weixin.mp.bean.menu.WxMpGetSelfMenuInfoResult;
import me.chanjar.weixin.mp.bean.menu.WxMpMenu;
import org.testng.*; import org.testng.*;
import org.testng.annotations.*; import org.testng.annotations.*;
@ -137,7 +138,7 @@ public class WxMpMenuServiceImplTest {
@Test(dependsOnMethods = {"testMenuCreate"}) @Test(dependsOnMethods = {"testMenuCreate"})
public void testMenuGet() throws WxErrorException { public void testMenuGet() throws WxErrorException {
WxMenu wxMenu = this.wxService.getMenuService().menuGet(); WxMpMenu wxMenu = this.wxService.getMenuService().menuGet();
Assert.assertNotNull(wxMenu); Assert.assertNotNull(wxMenu);
System.out.println(wxMenu.toJson()); System.out.println(wxMenu.toJson());
} }