mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-04-22 21:00:51 +08:00
🎨 #1456 企业微信通讯录成员属性相关代码根据最新接口文档进行同步完善,并增加几个新的属性
This commit is contained in:
parent
83d1b933e6
commit
8d6978d757
@ -4,6 +4,7 @@ import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
|
||||
|
||||
import java.io.Serializable;
|
||||
@ -16,8 +17,10 @@ import java.util.List;
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class WxCpUser implements Serializable {
|
||||
private static final long serialVersionUID = -5696099236344075582L;
|
||||
|
||||
private String userId;
|
||||
private String name;
|
||||
private Long[] departIds;
|
||||
@ -56,13 +59,19 @@ public class WxCpUser implements Serializable {
|
||||
* 成员对外信息.
|
||||
*/
|
||||
private List<ExternalAttribute> externalAttrs = new ArrayList<>();
|
||||
private String externalPosition;
|
||||
private String externalCorpName;
|
||||
|
||||
public void addExternalAttr(ExternalAttribute externalAttr) {
|
||||
this.externalAttrs.add(externalAttr);
|
||||
}
|
||||
|
||||
public void addExtAttr(String name, String value) {
|
||||
this.extAttrs.add(new Attr(name, value));
|
||||
this.extAttrs.add(new Attr().setType(0).setName(name).setTextValue(value));
|
||||
}
|
||||
|
||||
public void addExtAttr(Attr attr) {
|
||||
this.extAttrs.add(attr);
|
||||
}
|
||||
|
||||
public static WxCpUser fromJson(String json) {
|
||||
@ -74,10 +83,16 @@ public class WxCpUser implements Serializable {
|
||||
}
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@Accessors(chain = true)
|
||||
public static class Attr {
|
||||
/**
|
||||
* 属性类型: 0-文本 1-网页
|
||||
*/
|
||||
private int type;
|
||||
private String name;
|
||||
private String value;
|
||||
private String textValue;
|
||||
private String webUrl;
|
||||
private String webTitle;
|
||||
}
|
||||
|
||||
@Data
|
||||
|
@ -9,21 +9,13 @@
|
||||
|
||||
package me.chanjar.weixin.cp.util.json;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import com.google.gson.*;
|
||||
import me.chanjar.weixin.common.util.json.GsonHelper;
|
||||
import me.chanjar.weixin.cp.bean.Gender;
|
||||
import me.chanjar.weixin.cp.bean.WxCpUser;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* cp user gson adapter.
|
||||
*
|
||||
@ -32,15 +24,18 @@ import me.chanjar.weixin.cp.bean.WxCpUser;
|
||||
public class WxCpUserGsonAdapter implements JsonDeserializer<WxCpUser>, JsonSerializer<WxCpUser> {
|
||||
private static final String EXTERNAL_PROFILE = "external_profile";
|
||||
private static final String EXTERNAL_ATTR = "external_attr";
|
||||
private static final String EXTATTR = "extattr";
|
||||
private static final String EXTRA_ATTR = "extattr";
|
||||
private static final String EXTERNAL_POSITION = "external_position";
|
||||
private static final String DEPARTMENT = "department";
|
||||
private static final String EXTERNAL_CORP_NAME = "external_corp_name";
|
||||
|
||||
@Override
|
||||
public WxCpUser deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||
JsonObject o = json.getAsJsonObject();
|
||||
WxCpUser user = new WxCpUser();
|
||||
|
||||
if (o.get("department") != null) {
|
||||
JsonArray departJsonArray = o.get("department").getAsJsonArray();
|
||||
if (o.get(DEPARTMENT) != null) {
|
||||
JsonArray departJsonArray = o.get(DEPARTMENT).getAsJsonArray();
|
||||
Long[] departIds = new Long[departJsonArray.size()];
|
||||
int i = 0;
|
||||
for (JsonElement jsonElement : departJsonArray) {
|
||||
@ -80,25 +75,41 @@ public class WxCpUserGsonAdapter implements JsonDeserializer<WxCpUser>, JsonSeri
|
||||
user.setQrCode(GsonHelper.getString(o, "qr_code"));
|
||||
user.setToInvite(GsonHelper.getBoolean(o, "to_invite"));
|
||||
|
||||
if (GsonHelper.isNotNull(o.get(EXTATTR))) {
|
||||
if (GsonHelper.isNotNull(o.get(EXTRA_ATTR))) {
|
||||
this.buildExtraAttrs(o, user);
|
||||
}
|
||||
|
||||
if (GsonHelper.isNotNull(o.get(EXTERNAL_PROFILE))) {
|
||||
user.setExternalCorpName(GsonHelper.getString(o.getAsJsonObject().get(EXTERNAL_PROFILE).getAsJsonObject(), EXTERNAL_CORP_NAME));
|
||||
this.buildExternalAttrs(o, user);
|
||||
}
|
||||
|
||||
user.setExternalPosition(GsonHelper.getString(o, EXTERNAL_POSITION));
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
private void buildExtraAttrs(JsonObject o, WxCpUser user) {
|
||||
JsonArray attrJsonElements = o.get(EXTATTR).getAsJsonObject().get("attrs").getAsJsonArray();
|
||||
JsonArray attrJsonElements = o.get(EXTRA_ATTR).getAsJsonObject().get("attrs").getAsJsonArray();
|
||||
for (JsonElement attrJsonElement : attrJsonElements) {
|
||||
WxCpUser.Attr attr = new WxCpUser.Attr(
|
||||
GsonHelper.getString(attrJsonElement.getAsJsonObject(), "name"),
|
||||
GsonHelper.getString(attrJsonElement.getAsJsonObject(), "value")
|
||||
);
|
||||
final Integer type = GsonHelper.getInteger(attrJsonElement.getAsJsonObject(), "type");
|
||||
final WxCpUser.Attr attr = new WxCpUser.Attr().setType(type)
|
||||
.setName(GsonHelper.getString(attrJsonElement.getAsJsonObject(), "name"));
|
||||
user.getExtAttrs().add(attr);
|
||||
|
||||
switch (type) {
|
||||
case 0: {
|
||||
attr.setTextValue(GsonHelper.getString(attrJsonElement.getAsJsonObject().get("text").getAsJsonObject(), "value"));
|
||||
break;
|
||||
}
|
||||
case 1: {
|
||||
final JsonObject web = attrJsonElement.getAsJsonObject().get("web").getAsJsonObject();
|
||||
attr.setWebTitle(GsonHelper.getString(web, "title"))
|
||||
.setWebUrl(GsonHelper.getString(web, "url"));
|
||||
break;
|
||||
}
|
||||
default://ignored
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -237,13 +248,39 @@ public class WxCpUserGsonAdapter implements JsonDeserializer<WxCpUser>, JsonSeri
|
||||
JsonArray attrsJsonArray = new JsonArray();
|
||||
for (WxCpUser.Attr attr : user.getExtAttrs()) {
|
||||
JsonObject attrJson = new JsonObject();
|
||||
attrJson.addProperty("name", attr.getName());
|
||||
attrJson.addProperty("value", attr.getValue());
|
||||
|
||||
switch (attr.getType()) {
|
||||
case 0: {
|
||||
JsonObject text = new JsonObject();
|
||||
text.addProperty("value", attr.getTextValue());
|
||||
attrJson.add("text", text);
|
||||
break;
|
||||
}
|
||||
case 1: {
|
||||
JsonObject web = new JsonObject();
|
||||
web.addProperty("url", attr.getWebUrl());
|
||||
web.addProperty("title", attr.getWebTitle());
|
||||
attrJson.add("web", web);
|
||||
break;
|
||||
}
|
||||
default: //ignored
|
||||
}
|
||||
attrsJsonArray.add(attrJson);
|
||||
}
|
||||
JsonObject attrsJson = new JsonObject();
|
||||
attrsJson.add("attrs", attrsJsonArray);
|
||||
o.add(EXTATTR, attrsJson);
|
||||
o.add(EXTRA_ATTR, attrsJson);
|
||||
}
|
||||
|
||||
if (user.getExternalPosition() != null) {
|
||||
o.addProperty(EXTERNAL_POSITION, user.getExternalPosition());
|
||||
}
|
||||
|
||||
JsonObject attrsJson = new JsonObject();
|
||||
o.add(EXTERNAL_PROFILE, attrsJson);
|
||||
|
||||
if (user.getExternalCorpName() != null) {
|
||||
attrsJson.addProperty(EXTERNAL_CORP_NAME, user.getExternalCorpName());
|
||||
}
|
||||
|
||||
if (user.getExternalAttrs().size() > 0) {
|
||||
@ -279,9 +316,7 @@ public class WxCpUserGsonAdapter implements JsonDeserializer<WxCpUser>, JsonSeri
|
||||
attrsJsonArray.add(attrJson);
|
||||
}
|
||||
|
||||
JsonObject attrsJson = new JsonObject();
|
||||
attrsJson.add(EXTERNAL_ATTR, attrsJsonArray);
|
||||
o.add(EXTERNAL_PROFILE, attrsJson);
|
||||
}
|
||||
|
||||
return o;
|
||||
|
@ -35,18 +35,31 @@ public class WxCpUserGsonAdapterTest {
|
||||
" \"enable\": 1,\n" +
|
||||
" \"alias\": \"jackzhang\",\n" +
|
||||
" \"extattr\": {\n" +
|
||||
" \"attrs\": [{\n" +
|
||||
" \"name\": \"爱好\",\n" +
|
||||
" \"value\": \"旅游\"\n" +
|
||||
" }, {\n" +
|
||||
" \"name\": \"卡号\",\n" +
|
||||
" \"value\": \"1234567234\"\n" +
|
||||
" }]\n" +
|
||||
" },\n" +
|
||||
" \"attrs\": [\n" +
|
||||
" {\n" +
|
||||
" \"type\": 0,\n" +
|
||||
" \"name\": \"文本名称\",\n" +
|
||||
" \"text\": {\n" +
|
||||
" \"value\": \"文本\"\n" +
|
||||
" }\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"type\": 1,\n" +
|
||||
" \"name\": \"网页名称\",\n" +
|
||||
" \"web\": {\n" +
|
||||
" \"url\": \"http://www.test.com\",\n" +
|
||||
" \"title\": \"标题\"\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
" ]\n" +
|
||||
" }," +
|
||||
" \"status\": 1,\n" +
|
||||
" \"qr_code\": \"https://open.work.weixin.qq.com/wwopen/userQRCode?vcode=xxx\",\n" +
|
||||
" \"external_position\": \"高级产品经理\",\n" +
|
||||
" \"external_profile\": {\n" +
|
||||
" \"external_attr\": [{\n" +
|
||||
" \"external_corp_name\": \"企业简称\",\n" +
|
||||
" \"external_attr\": [\n" +
|
||||
" {\n" +
|
||||
" \"type\": 0,\n" +
|
||||
" \"name\": \"文本名称\",\n" +
|
||||
" \"text\": {\n" +
|
||||
@ -65,13 +78,13 @@ public class WxCpUserGsonAdapterTest {
|
||||
" \"type\": 2,\n" +
|
||||
" \"name\": \"测试app\",\n" +
|
||||
" \"miniprogram\": {\n" +
|
||||
" \"appid\": \"wx8bd80126147df384\",\n" +
|
||||
" \"appid\": \"wx8bd8012614784fake\",\n" +
|
||||
" \"pagepath\": \"/index\",\n" +
|
||||
" \"title\": \"my miniprogram\"\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
" ]\n" +
|
||||
" }\n" +
|
||||
" }" +
|
||||
"}";
|
||||
|
||||
final WxCpUser user = WxCpUser.fromJson(userJson);
|
||||
@ -84,6 +97,23 @@ public class WxCpUserGsonAdapterTest {
|
||||
|
||||
assertThat(user.getAddress()).isEqualTo("广州市海珠区新港中路");
|
||||
assertThat(user.getAlias()).isEqualTo("jackzhang");
|
||||
|
||||
assertThat(user.getExtAttrs()).isNotEmpty();
|
||||
|
||||
final WxCpUser.Attr extraAttr1 = user.getExtAttrs().get(0);
|
||||
assertThat(extraAttr1.getType()).isEqualTo(0);
|
||||
assertThat(extraAttr1.getName()).isEqualTo("文本名称");
|
||||
assertThat(extraAttr1.getTextValue()).isEqualTo("文本");
|
||||
|
||||
final WxCpUser.Attr extraAttr2 = user.getExtAttrs().get(1);
|
||||
assertThat(extraAttr2.getType()).isEqualTo(1);
|
||||
assertThat(extraAttr2.getName()).isEqualTo("网页名称");
|
||||
assertThat(extraAttr2.getWebTitle()).isEqualTo("标题");
|
||||
assertThat(extraAttr2.getWebUrl()).isEqualTo("http://www.test.com");
|
||||
|
||||
assertThat(user.getExternalPosition()).isEqualTo("高级产品经理");
|
||||
assertThat(user.getExternalCorpName()).isEqualTo("企业简称");
|
||||
|
||||
assertThat(user.getExternalAttrs()).isNotEmpty();
|
||||
|
||||
final WxCpUser.ExternalAttribute externalAttr1 = user.getExternalAttrs().get(0);
|
||||
@ -100,7 +130,7 @@ public class WxCpUserGsonAdapterTest {
|
||||
final WxCpUser.ExternalAttribute externalAttr3 = user.getExternalAttrs().get(2);
|
||||
assertThat(externalAttr3.getType()).isEqualTo(2);
|
||||
assertThat(externalAttr3.getName()).isEqualTo("测试app");
|
||||
assertThat(externalAttr3.getAppid()).isEqualTo("wx8bd80126147df384");
|
||||
assertThat(externalAttr3.getAppid()).isEqualTo("wx8bd8012614784fake");
|
||||
assertThat(externalAttr3.getPagePath()).isEqualTo("/index");
|
||||
assertThat(externalAttr3.getTitle()).isEqualTo("my miniprogram");
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user