mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-31 18:46:10 +08:00 
			
		
		
		
	#915 企业微信WxCpUser对象增加order属性
This commit is contained in:
		| @ -21,6 +21,7 @@ public class WxCpUser implements Serializable { | |||||||
|   private String userId; |   private String userId; | ||||||
|   private String name; |   private String name; | ||||||
|   private Integer[] departIds; |   private Integer[] departIds; | ||||||
|  |   private Integer[] orders; | ||||||
|   private String position; |   private String position; | ||||||
|   private String mobile; |   private String mobile; | ||||||
|   private Gender gender; |   private Gender gender; | ||||||
|  | |||||||
| @ -6,6 +6,7 @@ | |||||||
|  * arose from modification of the original source, or other redistribution of this source |  * arose from modification of the original source, or other redistribution of this source | ||||||
|  * is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD. |  * is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD. | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
| package me.chanjar.weixin.cp.util.json; | package me.chanjar.weixin.cp.util.json; | ||||||
|  |  | ||||||
| import java.lang.reflect.Type; | import java.lang.reflect.Type; | ||||||
| @ -24,11 +25,14 @@ import me.chanjar.weixin.cp.bean.Gender; | |||||||
| import me.chanjar.weixin.cp.bean.WxCpUser; | import me.chanjar.weixin.cp.bean.WxCpUser; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  |  * cp user gson adapter. | ||||||
|  |  * | ||||||
|  * @author Daniel Qian |  * @author Daniel Qian | ||||||
|  */ |  */ | ||||||
| public class WxCpUserGsonAdapter implements JsonDeserializer<WxCpUser>, JsonSerializer<WxCpUser> { | public class WxCpUserGsonAdapter implements JsonDeserializer<WxCpUser>, JsonSerializer<WxCpUser> { | ||||||
|   private static final String EXTERNAL_PROFILE = "external_profile"; |   private static final String EXTERNAL_PROFILE = "external_profile"; | ||||||
|   private static final String EXTERNAL_ATTR = "external_attr"; |   private static final String EXTERNAL_ATTR = "external_attr"; | ||||||
|  |   private static final String EXTATTR = "extattr"; | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public WxCpUser deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { |   public WxCpUser deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { | ||||||
| @ -45,6 +49,16 @@ public class WxCpUserGsonAdapter implements JsonDeserializer<WxCpUser>, JsonSeri | |||||||
|       user.setDepartIds(departIds); |       user.setDepartIds(departIds); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     if (o.get("order") != null) { | ||||||
|  |       JsonArray departJsonArray = o.get("order").getAsJsonArray(); | ||||||
|  |       Integer[] orders = new Integer[departJsonArray.size()]; | ||||||
|  |       int i = 0; | ||||||
|  |       for (JsonElement jsonElement : departJsonArray) { | ||||||
|  |         orders[i++] = jsonElement.getAsInt(); | ||||||
|  |       } | ||||||
|  |       user.setOrders(orders); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     user.setUserId(GsonHelper.getString(o, "userid")); |     user.setUserId(GsonHelper.getString(o, "userid")); | ||||||
|     user.setName(GsonHelper.getString(o, "name")); |     user.setName(GsonHelper.getString(o, "name")); | ||||||
|     user.setPosition(GsonHelper.getString(o, "position")); |     user.setPosition(GsonHelper.getString(o, "position")); | ||||||
| @ -62,8 +76,19 @@ public class WxCpUserGsonAdapter implements JsonDeserializer<WxCpUser>, JsonSeri | |||||||
|     user.setQrCode(GsonHelper.getString(o, "qr_code")); |     user.setQrCode(GsonHelper.getString(o, "qr_code")); | ||||||
|     user.setToInvite(GsonHelper.getBoolean(o, "to_invite")); |     user.setToInvite(GsonHelper.getBoolean(o, "to_invite")); | ||||||
|  |  | ||||||
|     if (GsonHelper.isNotNull(o.get("extattr"))) { |     if (GsonHelper.isNotNull(o.get(EXTATTR))) { | ||||||
|       JsonArray attrJsonElements = o.get("extattr").getAsJsonObject().get("attrs").getAsJsonArray(); |       this.buildExtraAttrs(o, user); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     if (GsonHelper.isNotNull(o.get(EXTERNAL_PROFILE))) { | ||||||
|  |       this.buildExternalAttrs(o, user); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     return user; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   private void buildExtraAttrs(JsonObject o, WxCpUser user) { | ||||||
|  |     JsonArray attrJsonElements = o.get(EXTATTR).getAsJsonObject().get("attrs").getAsJsonArray(); | ||||||
|     for (JsonElement attrJsonElement : attrJsonElements) { |     for (JsonElement attrJsonElement : attrJsonElements) { | ||||||
|       WxCpUser.Attr attr = new WxCpUser.Attr( |       WxCpUser.Attr attr = new WxCpUser.Attr( | ||||||
|         GsonHelper.getString(attrJsonElement.getAsJsonObject(), "name"), |         GsonHelper.getString(attrJsonElement.getAsJsonObject(), "name"), | ||||||
| @ -73,7 +98,7 @@ public class WxCpUserGsonAdapter implements JsonDeserializer<WxCpUser>, JsonSeri | |||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|     if (GsonHelper.isNotNull(o.get(EXTERNAL_PROFILE))) { |   private void buildExternalAttrs(JsonObject o, WxCpUser user) { | ||||||
|     JsonArray attrJsonElements = o.get(EXTERNAL_PROFILE).getAsJsonObject().get(EXTERNAL_ATTR).getAsJsonArray(); |     JsonArray attrJsonElements = o.get(EXTERNAL_PROFILE).getAsJsonObject().get(EXTERNAL_ATTR).getAsJsonArray(); | ||||||
|     for (JsonElement element : attrJsonElements) { |     for (JsonElement element : attrJsonElements) { | ||||||
|       final Integer type = GsonHelper.getInteger(element.getAsJsonObject(), "type"); |       final Integer type = GsonHelper.getInteger(element.getAsJsonObject(), "type"); | ||||||
| @ -119,8 +144,6 @@ public class WxCpUserGsonAdapter implements JsonDeserializer<WxCpUser>, JsonSeri | |||||||
|       } |       } | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|     return user; |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public JsonElement serialize(WxCpUser user, Type typeOfSrc, JsonSerializationContext context) { |   public JsonElement serialize(WxCpUser user, Type typeOfSrc, JsonSerializationContext context) { | ||||||
| @ -138,6 +161,15 @@ public class WxCpUserGsonAdapter implements JsonDeserializer<WxCpUser>, JsonSeri | |||||||
|       } |       } | ||||||
|       o.add("department", jsonArray); |       o.add("department", jsonArray); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     if (user.getOrders() != null) { | ||||||
|  |       JsonArray jsonArray = new JsonArray(); | ||||||
|  |       for (Integer order : user.getOrders()) { | ||||||
|  |         jsonArray.add(new JsonPrimitive(order)); | ||||||
|  |       } | ||||||
|  |       o.add("order", jsonArray); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     if (user.getPosition() != null) { |     if (user.getPosition() != null) { | ||||||
|       o.addProperty("position", user.getPosition()); |       o.addProperty("position", user.getPosition()); | ||||||
|     } |     } | ||||||
| @ -191,7 +223,7 @@ public class WxCpUserGsonAdapter implements JsonDeserializer<WxCpUser>, JsonSeri | |||||||
|       } |       } | ||||||
|       JsonObject attrsJson = new JsonObject(); |       JsonObject attrsJson = new JsonObject(); | ||||||
|       attrsJson.add("attrs", attrsJsonArray); |       attrsJson.add("attrs", attrsJsonArray); | ||||||
|       o.add("extattr", attrsJson); |       o.add(EXTATTR, attrsJson); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     if (user.getExternalAttrs().size() > 0) { |     if (user.getExternalAttrs().size() > 0) { | ||||||
|  | |||||||
| @ -76,6 +76,13 @@ public class WxCpUserGsonAdapterTest { | |||||||
|  |  | ||||||
|     final WxCpUser user = WxCpUser.fromJson(userJson); |     final WxCpUser user = WxCpUser.fromJson(userJson); | ||||||
|     assertThat(user).isNotNull(); |     assertThat(user).isNotNull(); | ||||||
|  |  | ||||||
|  |     assertThat(user.getOrders()).isNotEmpty(); | ||||||
|  |     assertThat(user.getOrders().length).isEqualTo(2); | ||||||
|  |     assertThat(user.getOrders()[0]).isEqualTo(1); | ||||||
|  |     assertThat(user.getOrders()[1]).isEqualTo(2); | ||||||
|  |  | ||||||
|  |  | ||||||
|     assertThat(user.getExternalAttrs()).isNotEmpty(); |     assertThat(user.getExternalAttrs()).isNotEmpty(); | ||||||
|  |  | ||||||
|     final WxCpUser.ExternalAttribute externalAttr1 = user.getExternalAttrs().get(0); |     final WxCpUser.ExternalAttribute externalAttr1 = user.getExternalAttrs().get(0); | ||||||
| @ -100,6 +107,7 @@ public class WxCpUserGsonAdapterTest { | |||||||
|   @Test |   @Test | ||||||
|   public void testSerialize() { |   public void testSerialize() { | ||||||
|     WxCpUser user = new WxCpUser(); |     WxCpUser user = new WxCpUser(); | ||||||
|  |     user.setOrders(new Integer[]{1, 2}); | ||||||
|     user.addExternalAttr(WxCpUser.ExternalAttribute.builder() |     user.addExternalAttr(WxCpUser.ExternalAttribute.builder() | ||||||
|       .type(0) |       .type(0) | ||||||
|       .name("文本名称") |       .name("文本名称") | ||||||
| @ -119,6 +127,10 @@ public class WxCpUserGsonAdapterTest { | |||||||
|       .title("my miniprogram") |       .title("my miniprogram") | ||||||
|       .build()); |       .build()); | ||||||
|  |  | ||||||
|     assertThat(user.toJson()).isEqualTo("{\"external_profile\":{\"external_attr\":[{\"type\":0,\"name\":\"文本名称\",\"text\":{\"value\":\"文本\"}},{\"type\":1,\"name\":\"网页名称\",\"web\":{\"url\":\"http://www.test.com\",\"title\":\"标题\"}},{\"type\":2,\"name\":\"测试app\",\"miniprogram\":{\"appid\":\"wx8bd80126147df384\",\"pagepath\":\"/index\",\"title\":\"my miniprogram\"}}]}}"); |     assertThat(user.toJson()).isEqualTo("{\"order\":[1,2],\"external_profile\":{\"external_attr\":" + | ||||||
|  |       "[{\"type\":0,\"name\":\"文本名称\",\"text\":{\"value\":\"文本\"}}," + | ||||||
|  |       "{\"type\":1,\"name\":\"网页名称\",\"web\":{\"url\":\"http://www.test.com\",\"title\":\"标题\"}}," + | ||||||
|  |       "{\"type\":2,\"name\":\"测试app\"," + | ||||||
|  |       "\"miniprogram\":{\"appid\":\"wx8bd80126147df384\",\"pagepath\":\"/index\",\"title\":\"my miniprogram\"}}]}}"); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Binary Wang
					Binary Wang