mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-04-05 17:38:05 +08:00
🎨 #2452 【企业微信】获取部门列表接口添加返回字段 departmentLeader(部门负责人的UserID)
This commit is contained in:
parent
ef293d284e
commit
228c71c42a
@ -1,10 +1,10 @@
|
||||
package me.chanjar.weixin.cp.bean;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import lombok.Data;
|
||||
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 企业微信的部门.
|
||||
*
|
||||
@ -17,6 +17,7 @@ public class WxCpDepart implements Serializable {
|
||||
private Long id;
|
||||
private String name;
|
||||
private String enName;
|
||||
private String[] departmentLeader;
|
||||
private Long parentId;
|
||||
private Long order;
|
||||
|
||||
|
@ -8,18 +8,12 @@
|
||||
*/
|
||||
package me.chanjar.weixin.cp.util.json;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
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.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import com.google.gson.*;
|
||||
import me.chanjar.weixin.common.util.json.GsonHelper;
|
||||
import me.chanjar.weixin.cp.bean.WxCpDepart;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* WxCpDepart的gson适配器.
|
||||
*
|
||||
@ -29,6 +23,7 @@ public class WxCpDepartGsonAdapter implements JsonSerializer<WxCpDepart>, JsonDe
|
||||
private static final String ID = "id";
|
||||
private static final String NAME = "name";
|
||||
private static final String EN_NAME = "name_en";
|
||||
private static final String DEPARTMENT_LEADER = "department_leader";
|
||||
private static final String PARENT_ID = "parentid";
|
||||
private static final String ORDER = "order";
|
||||
|
||||
@ -44,6 +39,13 @@ public class WxCpDepartGsonAdapter implements JsonSerializer<WxCpDepart>, JsonDe
|
||||
if (group.getEnName() != null) {
|
||||
json.addProperty(EN_NAME, group.getEnName());
|
||||
}
|
||||
if (group.getDepartmentLeader() != null) {
|
||||
JsonArray jsonArray = new JsonArray();
|
||||
for (String department : group.getDepartmentLeader()) {
|
||||
jsonArray.add(new JsonPrimitive(department));
|
||||
}
|
||||
json.add(DEPARTMENT_LEADER, jsonArray);
|
||||
}
|
||||
if (group.getParentId() != null) {
|
||||
json.addProperty(PARENT_ID, group.getParentId());
|
||||
}
|
||||
@ -67,6 +69,15 @@ public class WxCpDepartGsonAdapter implements JsonSerializer<WxCpDepart>, JsonDe
|
||||
if (departJson.get(EN_NAME) != null && !departJson.get(EN_NAME).isJsonNull()) {
|
||||
depart.setEnName(GsonHelper.getAsString(departJson.get(EN_NAME)));
|
||||
}
|
||||
if (departJson.getAsJsonArray(DEPARTMENT_LEADER) != null && !departJson.get(DEPARTMENT_LEADER).isJsonNull()) {
|
||||
JsonArray jsonArray = departJson.getAsJsonArray(DEPARTMENT_LEADER);
|
||||
String[] departments = new String[jsonArray.size()];
|
||||
int i = 0;
|
||||
for (JsonElement jsonElement : jsonArray) {
|
||||
departments[i++] = jsonElement.getAsString();
|
||||
}
|
||||
depart.setDepartmentLeader(departments);
|
||||
}
|
||||
if (departJson.get(ORDER) != null && !departJson.get(ORDER).isJsonNull()) {
|
||||
depart.setOrder(GsonHelper.getAsLong(departJson.get(ORDER)));
|
||||
}
|
||||
|
@ -78,6 +78,44 @@ public class WxCpUserExternalContactInfoTest {
|
||||
" }\n" +
|
||||
" ]\n" +
|
||||
"}";
|
||||
|
||||
final String testJson = "{\n" +
|
||||
" \"errcode\": 0,\n" +
|
||||
" \"errmsg\": \"ok\",\n" +
|
||||
" \"department\": [\n" +
|
||||
" {\n" +
|
||||
" \"id\": 2,\n" +
|
||||
" \"name\": \"广州研发中心\",\n" +
|
||||
" \"name_en\": \"RDGZ\",\n" +
|
||||
" \"department_leader\":[\"zhangsan\",\"lisi\"],\n" +
|
||||
" \"parentid\": 1,\n" +
|
||||
" \"order\": 10\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"id\": 3,\n" +
|
||||
" \"name\": \"邮箱产品部\",\n" +
|
||||
" \"name_en\": \"mail\",\n" +
|
||||
" \"department_leader\":[\"lisi\",\"wangwu\"],\n" +
|
||||
" \"parentid\": 2,\n" +
|
||||
" \"order\": 40\n" +
|
||||
" }\n" +
|
||||
" ]\n" +
|
||||
"}\n";
|
||||
|
||||
// 测试序列化
|
||||
val depart = new WxCpDepart();
|
||||
depart.setId(8L);
|
||||
depart.setName("name");
|
||||
depart.setEnName("enName");
|
||||
depart.setDepartmentLeader(new String[]{"zhangsan", "lisi"});
|
||||
depart.setParentId(88L);
|
||||
depart.setOrder(99L);
|
||||
|
||||
String toJson = depart.toJson();
|
||||
|
||||
// 测试企业微信字段返回
|
||||
List<WxCpDepart> department = WxCpGsonBuilder.create().fromJson(GsonParser.parse(two).get("department"), new TypeToken<List<WxCpDepart>>() {
|
||||
}.getType());
|
||||
|
||||
final WxCpExternalContactInfo contactInfo = WxCpExternalContactInfo.fromJson(json);
|
||||
assertThat(contactInfo).isNotNull();
|
||||
|
Loading…
Reference in New Issue
Block a user