mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-11-01 03:25:35 +08:00 
			
		
		
		
	改造查询菜单接口,以支持个性化菜单的conditionalmenu和menuid属性, #126
This commit is contained in:
		| @ -3,6 +3,7 @@ package me.chanjar.weixin.mp.api; | ||||
| import me.chanjar.weixin.common.bean.menu.WxMenu; | ||||
| import me.chanjar.weixin.common.exception.WxErrorException; | ||||
| import me.chanjar.weixin.mp.bean.menu.WxMpGetSelfMenuInfoResult; | ||||
| import me.chanjar.weixin.mp.bean.menu.WxMpMenu; | ||||
|  | ||||
| /** | ||||
|  * 菜单相关操作接口 | ||||
| @ -54,10 +55,10 @@ public interface WxMpMenuService { | ||||
|   /** | ||||
|    * <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> | ||||
|    */ | ||||
|   WxMenu menuGet() throws WxErrorException; | ||||
|   WxMpMenu menuGet() throws WxErrorException; | ||||
|  | ||||
|   /** | ||||
|    * <pre> | ||||
|  | ||||
| @ -7,6 +7,7 @@ import me.chanjar.weixin.common.exception.WxErrorException; | ||||
| import me.chanjar.weixin.mp.api.WxMpMenuService; | ||||
| import me.chanjar.weixin.mp.api.WxMpService; | ||||
| import me.chanjar.weixin.mp.bean.menu.WxMpGetSelfMenuInfoResult; | ||||
| import me.chanjar.weixin.mp.bean.menu.WxMpMenu; | ||||
| import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | ||||
|  | ||||
| @ -78,11 +79,11 @@ public class WxMpMenuServiceImpl implements WxMpMenuService { | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public WxMenu menuGet() throws WxErrorException { | ||||
|   public WxMpMenu menuGet() throws WxErrorException { | ||||
|     String url = API_URL_PREFIX + "/get"; | ||||
|     try { | ||||
|       String resultContent = this.wxMpService.get(url, null); | ||||
|       return WxMenu.fromJson(resultContent); | ||||
|       return WxMpMenu.fromJson(resultContent); | ||||
|     } catch (WxErrorException e) { | ||||
|       // 46003 不存在的菜单数据 | ||||
|       if (e.getError().getErrorCode() == 46003) { | ||||
|  | ||||
| @ -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; | ||||
|     } | ||||
|   } | ||||
|  | ||||
| } | ||||
| @ -8,6 +8,7 @@ import me.chanjar.weixin.common.exception.WxErrorException; | ||||
| import me.chanjar.weixin.mp.api.ApiTestModule; | ||||
| import me.chanjar.weixin.mp.api.WxMpService; | ||||
| import me.chanjar.weixin.mp.bean.menu.WxMpGetSelfMenuInfoResult; | ||||
| import me.chanjar.weixin.mp.bean.menu.WxMpMenu; | ||||
| import org.testng.*; | ||||
| import org.testng.annotations.*; | ||||
|  | ||||
| @ -137,7 +138,7 @@ public class WxMpMenuServiceImplTest { | ||||
|  | ||||
|   @Test(dependsOnMethods = {"testMenuCreate"}) | ||||
|   public void testMenuGet() throws WxErrorException { | ||||
|     WxMenu wxMenu = this.wxService.getMenuService().menuGet(); | ||||
|     WxMpMenu wxMenu = this.wxService.getMenuService().menuGet(); | ||||
|     Assert.assertNotNull(wxMenu); | ||||
|     System.out.println(wxMenu.toJson()); | ||||
|   } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Binary Wang
					Binary Wang