mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-06-28 13:16:19 +08:00
🐛 #1738 修复企业微信创建用户接口自定义字段缺失的问题
This commit is contained in:
parent
31ecd4d83d
commit
07352d183c
4
pom.xml
4
pom.xml
@ -172,7 +172,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.slf4j</groupId>
|
<groupId>org.slf4j</groupId>
|
||||||
<artifactId>slf4j-api</artifactId>
|
<artifactId>slf4j-api</artifactId>
|
||||||
<version>1.7.24</version>
|
<version>1.7.30</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.thoughtworks.xstream</groupId>
|
<groupId>com.thoughtworks.xstream</groupId>
|
||||||
@ -268,7 +268,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.data</groupId>
|
<groupId>org.springframework.data</groupId>
|
||||||
<artifactId>spring-data-redis</artifactId>
|
<artifactId>spring-data-redis</artifactId>
|
||||||
<version>1.8.23.RELEASE</version>
|
<version>2.3.3.RELEASE</version>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
@ -50,7 +50,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.slf4j</groupId>
|
<groupId>org.slf4j</groupId>
|
||||||
<artifactId>jcl-over-slf4j</artifactId>
|
<artifactId>jcl-over-slf4j</artifactId>
|
||||||
<version>1.7.24</version>
|
<version>1.7.30</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -4,6 +4,7 @@ import com.google.common.collect.Lists;
|
|||||||
import com.google.gson.JsonArray;
|
import com.google.gson.JsonArray;
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
import jodd.util.MathUtil;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -151,4 +152,50 @@ public class GsonHelper {
|
|||||||
public static JsonArray getAsJsonArray(JsonElement element) {
|
public static JsonArray getAsJsonArray(JsonElement element) {
|
||||||
return element == null ? null : element.getAsJsonArray();
|
return element == null ? null : element.getAsJsonArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 快速构建JsonObject对象,批量添加一堆属性
|
||||||
|
*
|
||||||
|
* @param keyOrValue 包含key或value的数组
|
||||||
|
* @return JsonObject对象.
|
||||||
|
*/
|
||||||
|
public static JsonObject buildJsonObject(Object... keyOrValue) {
|
||||||
|
JsonObject result = new JsonObject();
|
||||||
|
put(result, keyOrValue);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量向JsonObject对象中添加属性
|
||||||
|
*
|
||||||
|
* @param jsonObject 原始JsonObject对象
|
||||||
|
* @param keyOrValue 包含key或value的数组
|
||||||
|
*/
|
||||||
|
public static void put(JsonObject jsonObject, Object... keyOrValue) {
|
||||||
|
if (MathUtil.isOdd(keyOrValue.length)) {
|
||||||
|
throw new RuntimeException("参数个数必须为偶数");
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < keyOrValue.length / 2; i++) {
|
||||||
|
final Object key = keyOrValue[2 * i];
|
||||||
|
final Object value = keyOrValue[2 * i + 1];
|
||||||
|
if (value == null) {
|
||||||
|
jsonObject.add(key.toString(), null);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value instanceof Boolean) {
|
||||||
|
jsonObject.addProperty(key.toString(), (Boolean) value);
|
||||||
|
} else if (value instanceof Character) {
|
||||||
|
jsonObject.addProperty(key.toString(), (Character) value);
|
||||||
|
} else if (value instanceof Number) {
|
||||||
|
jsonObject.addProperty(key.toString(), (Number) value);
|
||||||
|
} else if (value instanceof JsonElement) {
|
||||||
|
jsonObject.add(key.toString(), (JsonElement) value);
|
||||||
|
} else {
|
||||||
|
jsonObject.addProperty(key.toString(), value.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,139 @@
|
|||||||
|
package me.chanjar.weixin.common.util.json;
|
||||||
|
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GsonHelper 的单元测试.
|
||||||
|
*
|
||||||
|
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||||
|
* @date 2020-09-04
|
||||||
|
*/
|
||||||
|
public class GsonHelperTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsNull() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsNotNull() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetLong() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetPrimitiveLong() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetInteger() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetPrimitiveInteger() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetDouble() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetPrimitiveDouble() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetFloat() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetPrimitiveFloat() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetBoolean() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetString() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAsString() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAsLong() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAsPrimitiveLong() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAsInteger() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAsPrimitiveInt() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAsBoolean() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAsPrimitiveBool() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAsDouble() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAsPrimitiveDouble() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAsFloat() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAsPrimitiveFloat() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetIntArray() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetStringArray() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetLongArray() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAsJsonArray() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBuildSimpleJsonObject() {
|
||||||
|
try {
|
||||||
|
GsonHelper.buildJsonObject(1, 2, 3);
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
assertThat(e.getMessage()).isEqualTo("参数个数必须为偶数");
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(GsonHelper.buildJsonObject(1, 2));
|
||||||
|
System.out.println(GsonHelper.buildJsonObject(1, null));
|
||||||
|
System.out.println(GsonHelper.buildJsonObject("int", 1, "float", 2.1f, "double", 2.5));
|
||||||
|
System.out.println(GsonHelper.buildJsonObject("boolean", true, "string", "1av"));
|
||||||
|
System.out.println(GsonHelper.buildJsonObject(1, true, "jsonElement", new JsonObject()));
|
||||||
|
System.out.println(GsonHelper.buildJsonObject("num", 2, "string", "cde", "char", 'a', "bool", true));
|
||||||
|
}
|
||||||
|
}
|
@ -86,6 +86,9 @@ public class WxCpUser implements Serializable {
|
|||||||
|
|
||||||
@Data
|
@Data
|
||||||
@Accessors(chain = true)
|
@Accessors(chain = true)
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
public static class Attr {
|
public static class Attr {
|
||||||
/**
|
/**
|
||||||
* 属性类型: 0-文本 1-网页
|
* 属性类型: 0-文本 1-网页
|
||||||
|
@ -276,11 +276,11 @@ public class WxCpUserGsonAdapter implements JsonDeserializer<WxCpUser>, JsonSeri
|
|||||||
o.addProperty("main_department", user.getMainDepartment());
|
o.addProperty("main_department", user.getMainDepartment());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (user.getExtAttrs().size() > 0) {
|
if (!user.getExtAttrs().isEmpty()) {
|
||||||
JsonArray attrsJsonArray = new JsonArray();
|
JsonArray attrsJsonArray = new JsonArray();
|
||||||
for (WxCpUser.Attr attr : user.getExtAttrs()) {
|
for (WxCpUser.Attr attr : user.getExtAttrs()) {
|
||||||
JsonObject attrJson = new JsonObject();
|
JsonObject attrJson = GsonHelper.buildJsonObject("type", attr.getType(),
|
||||||
|
"name", attr.getName());
|
||||||
attrsJsonArray.add(attrJson);
|
attrsJsonArray.add(attrJson);
|
||||||
|
|
||||||
if (attr.getType() == null) {
|
if (attr.getType() == null) {
|
||||||
@ -290,19 +290,12 @@ public class WxCpUserGsonAdapter implements JsonDeserializer<WxCpUser>, JsonSeri
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch (attr.getType()) {
|
switch (attr.getType()) {
|
||||||
case 0: {
|
case 0:
|
||||||
JsonObject text = new JsonObject();
|
attrJson.add("text", GsonHelper.buildJsonObject("value", attr.getTextValue()));
|
||||||
text.addProperty("value", attr.getTextValue());
|
|
||||||
attrJson.add("text", text);
|
|
||||||
break;
|
break;
|
||||||
}
|
case 1:
|
||||||
case 1: {
|
attrJson.add("web", GsonHelper.buildJsonObject("url", attr.getWebUrl(), "title", attr.getWebTitle()));
|
||||||
JsonObject web = new JsonObject();
|
|
||||||
web.addProperty("url", attr.getWebUrl());
|
|
||||||
web.addProperty("title", attr.getWebTitle());
|
|
||||||
attrJson.add("web", web);
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
default: //ignored
|
default: //ignored
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -322,12 +315,11 @@ public class WxCpUserGsonAdapter implements JsonDeserializer<WxCpUser>, JsonSeri
|
|||||||
attrsJson.addProperty(EXTERNAL_CORP_NAME, user.getExternalCorpName());
|
attrsJson.addProperty(EXTERNAL_CORP_NAME, user.getExternalCorpName());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (user.getExternalAttrs().size() > 0) {
|
if (!user.getExternalAttrs().isEmpty()) {
|
||||||
JsonArray attrsJsonArray = new JsonArray();
|
JsonArray attrsJsonArray = new JsonArray();
|
||||||
for (WxCpUser.ExternalAttribute attr : user.getExternalAttrs()) {
|
for (WxCpUser.ExternalAttribute attr : user.getExternalAttrs()) {
|
||||||
JsonObject attrJson = new JsonObject();
|
JsonObject attrJson = GsonHelper.buildJsonObject("type", attr.getType(),
|
||||||
attrJson.addProperty("type", attr.getType());
|
"name", attr.getName());
|
||||||
attrJson.addProperty("name", attr.getName());
|
|
||||||
|
|
||||||
attrsJsonArray.add(attrJson);
|
attrsJsonArray.add(attrJson);
|
||||||
|
|
||||||
@ -336,27 +328,16 @@ public class WxCpUserGsonAdapter implements JsonDeserializer<WxCpUser>, JsonSeri
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch (attr.getType()) {
|
switch (attr.getType()) {
|
||||||
case 0: {
|
case 0:
|
||||||
JsonObject text = new JsonObject();
|
attrJson.add("text", GsonHelper.buildJsonObject("value", attr.getValue()));
|
||||||
text.addProperty("value", attr.getValue());
|
|
||||||
attrJson.add("text", text);
|
|
||||||
break;
|
break;
|
||||||
}
|
case 1:
|
||||||
case 1: {
|
attrJson.add("web", GsonHelper.buildJsonObject("url", attr.getUrl(), "title", attr.getTitle()));
|
||||||
JsonObject web = new JsonObject();
|
|
||||||
web.addProperty("url", attr.getUrl());
|
|
||||||
web.addProperty("title", attr.getTitle());
|
|
||||||
attrJson.add("web", web);
|
|
||||||
break;
|
break;
|
||||||
}
|
case 2:
|
||||||
case 2: {
|
attrJson.add("miniprogram", GsonHelper.buildJsonObject("appid", attr.getAppid(),
|
||||||
JsonObject miniprogram = new JsonObject();
|
"pagepath", attr.getPagePath(), "title", attr.getTitle()));
|
||||||
miniprogram.addProperty("appid", attr.getAppid());
|
|
||||||
miniprogram.addProperty("pagepath", attr.getPagePath());
|
|
||||||
miniprogram.addProperty("title", attr.getTitle());
|
|
||||||
attrJson.add("miniprogram", miniprogram);
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
default://忽略
|
default://忽略
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -91,7 +91,7 @@ public class WxCpUserGsonAdapterTest {
|
|||||||
assertThat(user).isNotNull();
|
assertThat(user).isNotNull();
|
||||||
|
|
||||||
assertThat(user.getOrders()).isNotEmpty();
|
assertThat(user.getOrders()).isNotEmpty();
|
||||||
assertThat(user.getOrders().length).isEqualTo(2);
|
assertThat(user.getOrders()).hasSize(2);
|
||||||
assertThat(user.getOrders()[0]).isEqualTo(1);
|
assertThat(user.getOrders()[0]).isEqualTo(1);
|
||||||
assertThat(user.getOrders()[1]).isEqualTo(2);
|
assertThat(user.getOrders()[1]).isEqualTo(2);
|
||||||
|
|
||||||
@ -140,6 +140,12 @@ public class WxCpUserGsonAdapterTest {
|
|||||||
public void testSerialize() {
|
public void testSerialize() {
|
||||||
WxCpUser user = new WxCpUser();
|
WxCpUser user = new WxCpUser();
|
||||||
user.setOrders(new Integer[]{1, 2});
|
user.setOrders(new Integer[]{1, 2});
|
||||||
|
user.addExtAttr(WxCpUser.Attr.builder()
|
||||||
|
.type(0)
|
||||||
|
.name("文本名称")
|
||||||
|
.textValue("文本")
|
||||||
|
.build());
|
||||||
|
|
||||||
user.addExternalAttr(WxCpUser.ExternalAttribute.builder()
|
user.addExternalAttr(WxCpUser.ExternalAttribute.builder()
|
||||||
.type(0)
|
.type(0)
|
||||||
.name("文本名称")
|
.name("文本名称")
|
||||||
@ -159,7 +165,9 @@ public class WxCpUserGsonAdapterTest {
|
|||||||
.title("my miniprogram")
|
.title("my miniprogram")
|
||||||
.build());
|
.build());
|
||||||
|
|
||||||
assertThat(user.toJson()).isEqualTo("{\"order\":[1,2],\"external_profile\":{\"external_attr\":" +
|
assertThat(user.toJson()).isEqualTo("{\"order\":[1,2]," +
|
||||||
|
"\"extattr\":{\"attrs\":[{\"type\":0,\"name\":\"文本名称\",\"text\":{\"value\":\"文本\"}}]}," +
|
||||||
|
"\"external_profile\":{\"external_attr\":" +
|
||||||
"[{\"type\":0,\"name\":\"文本名称\",\"text\":{\"value\":\"文本\"}}," +
|
"[{\"type\":0,\"name\":\"文本名称\",\"text\":{\"value\":\"文本\"}}," +
|
||||||
"{\"type\":1,\"name\":\"网页名称\",\"web\":{\"url\":\"http://www.test.com\",\"title\":\"标题\"}}," +
|
"{\"type\":1,\"name\":\"网页名称\",\"web\":{\"url\":\"http://www.test.com\",\"title\":\"标题\"}}," +
|
||||||
"{\"type\":2,\"name\":\"测试app\"," +
|
"{\"type\":2,\"name\":\"测试app\"," +
|
||||||
|
Loading…
Reference in New Issue
Block a user