Skip to content

【企业微信】weixin-java-cp 字段更新 WxCpDepart.java #2452

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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