Skip to content

Commit

Permalink
🎨 #2452 【企业微信】获取部门列表接口添加返回字段 departmentLeader(部门负责人的UserID)
Browse files Browse the repository at this point in the history
  • Loading branch information
0katekate0 authored Dec 17, 2021
1 parent ef293d2 commit 228c71c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -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;

/**
* 企业微信的部门.
*
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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适配器.
*
Expand All @@ -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";

Expand All @@ -44,6 +39,13 @@ public JsonElement serialize(WxCpDepart group, Type typeOfSrc, JsonSerialization
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());
}
Expand All @@ -67,6 +69,15 @@ public WxCpDepart deserialize(JsonElement json, Type typeOfT, JsonDeserializatio
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)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,44 @@ public void testFromJson() {
" }\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();
Expand Down

0 comments on commit 228c71c

Please sign in to comment.