mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-31 10:38:42 +08:00 
			
		
		
		
	| @ -1,15 +1,14 @@ | |||||||
| package me.chanjar.weixin.common.bean; | package me.chanjar.weixin.common.bean; | ||||||
|  |  | ||||||
|  | import me.chanjar.weixin.common.util.json.WxGsonBuilder; | ||||||
|  | import org.apache.commons.codec.Charsets; | ||||||
|  |  | ||||||
| import java.io.InputStream; | import java.io.InputStream; | ||||||
| import java.io.InputStreamReader; | import java.io.InputStreamReader; | ||||||
| import java.io.Serializable; | import java.io.Serializable; | ||||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||||
| import java.util.List; | import java.util.List; | ||||||
|  |  | ||||||
| import org.apache.commons.codec.Charsets; |  | ||||||
|  |  | ||||||
| import me.chanjar.weixin.common.util.json.WxGsonBuilder; |  | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * 企业号菜单 |  * 企业号菜单 | ||||||
|  * @author Daniel Qian |  * @author Daniel Qian | ||||||
| @ -136,6 +135,7 @@ public class WxMenu implements Serializable { | |||||||
|     private String province; |     private String province; | ||||||
|     private String city; |     private String city; | ||||||
|     private String clientPlatformType; |     private String clientPlatformType; | ||||||
|  |     private String language; | ||||||
|      |      | ||||||
|     public String getGroupId() { |     public String getGroupId() { | ||||||
|       return groupId; |       return groupId; | ||||||
| @ -185,6 +185,14 @@ public class WxMenu implements Serializable { | |||||||
|       this.clientPlatformType = clientPlatformType; |       this.clientPlatformType = clientPlatformType; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     public String getLanguage() { | ||||||
|  |       return language; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public void setLanguage(String language) { | ||||||
|  |       this.language = language; | ||||||
|  |     } | ||||||
|  |  | ||||||
|       @Override |       @Override | ||||||
|     public String toString() { |     public String toString() { | ||||||
|       return "matchrule:{" + |       return "matchrule:{" + | ||||||
| @ -194,6 +202,7 @@ public class WxMenu implements Serializable { | |||||||
|           ", province" + province + '\'' + |           ", province" + province + '\'' + | ||||||
|           ", city" + city + '\'' + |           ", city" + city + '\'' + | ||||||
|           ", client_platform_type" + clientPlatformType + '\'' + |           ", client_platform_type" + clientPlatformType + '\'' + | ||||||
|  |           ", language" + language + '\'' + | ||||||
|           "}"; |           "}"; | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  | |||||||
| @ -8,9 +8,6 @@ | |||||||
|  */ |  */ | ||||||
| package me.chanjar.weixin.common.util.json; | package me.chanjar.weixin.common.util.json; | ||||||
|  |  | ||||||
| import java.lang.reflect.Type; |  | ||||||
|  |  | ||||||
| import com.google.gson.Gson; |  | ||||||
| import com.google.gson.JsonArray; | import com.google.gson.JsonArray; | ||||||
| import com.google.gson.JsonDeserializationContext; | import com.google.gson.JsonDeserializationContext; | ||||||
| import com.google.gson.JsonDeserializer; | import com.google.gson.JsonDeserializer; | ||||||
| @ -19,9 +16,10 @@ import com.google.gson.JsonObject; | |||||||
| import com.google.gson.JsonParseException; | import com.google.gson.JsonParseException; | ||||||
| import com.google.gson.JsonSerializationContext; | import com.google.gson.JsonSerializationContext; | ||||||
| import com.google.gson.JsonSerializer; | import com.google.gson.JsonSerializer; | ||||||
|  |  | ||||||
| import me.chanjar.weixin.common.bean.WxMenu; | import me.chanjar.weixin.common.bean.WxMenu; | ||||||
|  |  | ||||||
|  | import java.lang.reflect.Type; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  *  |  *  | ||||||
|  * @author Daniel Qian |  * @author Daniel Qian | ||||||
| @ -40,8 +38,7 @@ public class WxMenuGsonAdapter implements JsonSerializer<WxMenu>, JsonDeserializ | |||||||
|     json.add("button", buttonArray); |     json.add("button", buttonArray); | ||||||
|      |      | ||||||
|     if (menu.getMatchRule() != null) { |     if (menu.getMatchRule() != null) { | ||||||
|       Gson gson = new Gson(); |       json.add("matchrule", convertToJson(menu.getMatchRule())); | ||||||
|       json.add("matchrule", gson.toJsonTree(menu.getMatchRule())); |  | ||||||
|     } |     } | ||||||
|      |      | ||||||
|     return json; |     return json; | ||||||
| @ -63,6 +60,18 @@ public class WxMenuGsonAdapter implements JsonSerializer<WxMenu>, JsonDeserializ | |||||||
|     return buttonJson; |     return buttonJson; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   protected JsonObject convertToJson(WxMenu.WxMenuRule menuRule){ | ||||||
|  |     JsonObject matchRule = new JsonObject(); | ||||||
|  |     matchRule.addProperty("group_id",menuRule.getGroupId()); | ||||||
|  |     matchRule.addProperty("sex",menuRule.getSex()); | ||||||
|  |     matchRule.addProperty("country",menuRule.getCountry()); | ||||||
|  |     matchRule.addProperty("province",menuRule.getProvince()); | ||||||
|  |     matchRule.addProperty("city",menuRule.getCity()); | ||||||
|  |     matchRule.addProperty("client_platform_type",menuRule.getClientPlatformType()); | ||||||
|  |     matchRule.addProperty("language",menuRule.getLanguage()); | ||||||
|  |     return matchRule; | ||||||
|  |   } | ||||||
|  |  | ||||||
|   public WxMenu deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { |   public WxMenu deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { | ||||||
|     /* |     /* | ||||||
|      * 操蛋的微信 |      * 操蛋的微信 | ||||||
|  | |||||||
| @ -56,6 +56,29 @@ public class WxMenuTest { | |||||||
|     Assert.assertEquals(menu.toJson(), json); |     Assert.assertEquals(menu.toJson(), json); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   @Test(dataProvider = "wxAddConditionalMenu") | ||||||
|  |   public void testAddConditionalToJson(String json) { | ||||||
|  |     WxMenu menu = new WxMenu(); | ||||||
|  |     WxMenuButton button1 = new WxMenuButton(); | ||||||
|  |     button1.setType("click"); | ||||||
|  |     button1.setName("今日歌曲"); | ||||||
|  |     button1.setKey("V1001_TODAY_MUSIC"); | ||||||
|  |  | ||||||
|  |     menu.getButtons().add(button1); | ||||||
|  |  | ||||||
|  |     WxMenu.WxMenuRule wxMenuRule = new WxMenu.WxMenuRule(); | ||||||
|  |     wxMenuRule.setGroupId("2"); | ||||||
|  |     wxMenuRule.setSex("1"); | ||||||
|  |     wxMenuRule.setCountry("中国"); | ||||||
|  |     wxMenuRule.setProvince("广东"); | ||||||
|  |     wxMenuRule.setCity("广州"); | ||||||
|  |     wxMenuRule.setClientPlatformType("2"); | ||||||
|  |     wxMenuRule.setLanguage("zh_CN"); | ||||||
|  |     menu.setMatchRule(wxMenuRule); | ||||||
|  |  | ||||||
|  |     Assert.assertEquals(menu.toJson(), json); | ||||||
|  |   } | ||||||
|  |    | ||||||
|   @DataProvider |   @DataProvider | ||||||
|   public Object[][] wxReturnMenu() { |   public Object[][] wxReturnMenu() { | ||||||
|     Object[][]  res = menuJson(); |     Object[][]  res = menuJson(); | ||||||
| @ -107,4 +130,30 @@ public class WxMenuTest { | |||||||
|     }; |     }; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   @DataProvider(name = "wxAddConditionalMenu") | ||||||
|  |   public Object[][] addConditionalMenuJson(){ | ||||||
|  |     String json = | ||||||
|  |         "{" | ||||||
|  |             +"\"button\":[" | ||||||
|  |             +"{" | ||||||
|  |             +"\"type\":\"click\"," | ||||||
|  |             +"\"name\":\"今日歌曲\"," | ||||||
|  |             +"\"key\":\"V1001_TODAY_MUSIC\"" | ||||||
|  |             +"}" | ||||||
|  |             +"]," | ||||||
|  |             +"\"matchrule\":{" | ||||||
|  |               +"\"group_id\":\"2\"," | ||||||
|  |               +"\"sex\":\"1\"," | ||||||
|  |               +"\"country\":\"中国\"," | ||||||
|  |               +"\"province\":\"广东\"," | ||||||
|  |               +"\"city\":\"广州\"," | ||||||
|  |               +"\"client_platform_type\":\"2\"," | ||||||
|  |               +"\"language\":\"zh_CN\"" | ||||||
|  |             +"}" | ||||||
|  |             +"}"; | ||||||
|  |     return new Object[][]{ | ||||||
|  |         new Object[]{json} | ||||||
|  |     }; | ||||||
|  |   } | ||||||
|  |    | ||||||
| } | } | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Daniel Qian
					Daniel Qian