Browse Source

用户管理支持

master
Daniel Qian 10 years ago
parent
commit
3db7c1789d
26 changed files with 373 additions and 363 deletions
  1. +2
    -2
      weixin-java-enterprise/README.md
  2. +1
    -1
      weixin-java-enterprise/src/main/java/me/chanjar/weixin/common/bean/result/WxAccessToken.java
  3. +1
    -4
      weixin-java-enterprise/src/main/java/me/chanjar/weixin/common/bean/result/WxError.java
  4. +1
    -1
      weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/api/WxCpConfigStorage.java
  5. +13
    -5
      weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/api/WxCpService.java
  6. +35
    -14
      weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/api/WxCpServiceImpl.java
  7. +1
    -1
      weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/api/WxInMemoryCpConfigStorage.java
  8. +143
    -0
      weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/WxCpUser.java
  9. +0
    -55
      weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/result/WxMassSendResult.java
  10. +0
    -52
      weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/result/WxMassUploadResult.java
  11. +0
    -95
      weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/result/WxUser.java
  12. +0
    -47
      weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/result/WxUserList.java
  13. +1
    -1
      weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/exception/WxErrorException.java
  14. +1
    -1
      weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/http/MediaDownloadRequestExecutor.java
  15. +1
    -1
      weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/http/MediaUploadRequestExecutor.java
  16. +1
    -1
      weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/http/SimpleGetRequestExecutor.java
  17. +1
    -1
      weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/http/SimplePostRequestExecutor.java
  18. +1
    -1
      weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/json/WxCpAccessTokenAdapter.java
  19. +3
    -3
      weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/json/WxCpGsonBuilder.java
  20. +91
    -29
      weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/json/WxCpUserGsonAdapter.java
  21. +0
    -43
      weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/json/WxCpUserListGsonAdapter.java
  22. +1
    -1
      weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/json/WxErrorAdapter.java
  23. +1
    -1
      weixin-java-enterprise/src/test/java/me/chanjar/weixin/common/bean/WxAccessTokenTest.java
  24. +7
    -2
      weixin-java-enterprise/src/test/java/me/chanjar/weixin/enterprise/api/WxCpDepartAPITest.java
  25. +66
    -0
      weixin-java-enterprise/src/test/java/me/chanjar/weixin/enterprise/api/WxCpUserAPITest.java
  26. +1
    -1
      weixin-java-enterprise/src/test/java/me/chanjar/weixin/enterprise/bean/WxErrorTest.java

+ 2
- 2
weixin-java-enterprise/README.md View File

@@ -50,7 +50,7 @@ wxService.setWxConfigStorage(config);

// 用户的openid在下面地址获得
// https://mp.weixin.qq.com/debug/cgi-bin/apiinfo?t=index&type=用户管理&form=获取关注者列表接口%20/user/get
String openId = ...;
WxCustomMessage message = WxCustomMessage.TEXT().toUser(openId).content("Hello World").build();
String userId = ...;
WxCustomMessage message = WxCustomMessage.TEXT().toUser(userId).content("Hello World").build();
wxService.customMessageSend(message);
```

weixin-java-enterprise/src/main/java/me/chanjar/weixin/common/bean/WxAccessToken.java → weixin-java-enterprise/src/main/java/me/chanjar/weixin/common/bean/result/WxAccessToken.java View File

@@ -1,4 +1,4 @@
package me.chanjar.weixin.common.bean;
package me.chanjar.weixin.common.bean.result;

import me.chanjar.weixin.enterprise.util.json.WxCpGsonBuilder;


weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/result/WxError.java → weixin-java-enterprise/src/main/java/me/chanjar/weixin/common/bean/result/WxError.java View File

@@ -1,7 +1,4 @@
package me.chanjar.weixin.enterprise.bean.result;

import java.util.HashMap;
import java.util.Map;
package me.chanjar.weixin.common.bean.result;

import me.chanjar.weixin.enterprise.util.json.WxCpGsonBuilder;


+ 1
- 1
weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/api/WxCpConfigStorage.java View File

@@ -1,6 +1,6 @@
package me.chanjar.weixin.enterprise.api;

import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.bean.result.WxAccessToken;

/**
* 微信客户端配置存储


+ 13
- 5
weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/api/WxCpService.java View File

@@ -8,7 +8,7 @@ import java.util.List;
import me.chanjar.weixin.enterprise.bean.*;
import me.chanjar.weixin.enterprise.bean.WxCpDepart;
import me.chanjar.weixin.enterprise.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.enterprise.bean.result.WxUser;
import me.chanjar.weixin.enterprise.bean.WxCpUser;
import me.chanjar.weixin.enterprise.exception.WxErrorException;

/**
@@ -175,15 +175,23 @@ public interface WxCpService {
*/
public void departDelete(Integer departId) throws WxErrorException;

public void userCreate(WxUser user) throws WxErrorException;
public void userCreate(WxCpUser user) throws WxErrorException;

public void userUpdate(WxUser user) throws WxErrorException;
public void userUpdate(WxCpUser user) throws WxErrorException;

public void userDelete(String userid) throws WxErrorException;

public WxUser userGet(String userid) throws WxErrorException;
public WxCpUser userGet(String userid) throws WxErrorException;

public List<WxUser> userGetByDepartment(String departmentId) throws WxErrorException;
/**
* http://qydev.weixin.qq.com/wiki/index.php?title=管理成员#.E8.8E.B7.E5.8F.96.E9.83.A8.E9.97.A8.E6.88.90.E5.91.98
* @param departId 必填。部门id
* @param fetchChild 非必填。1/0:是否递归获取子部门下面的成员
* @param status 非必填。0获取全部员工,1获取已关注成员列表,2获取禁用成员列表,4获取未关注成员列表。status可叠加
* @return
* @throws WxErrorException
*/
public List<WxCpUser> userGetByDepart(Integer departId, Boolean fetchChild, Integer status) throws WxErrorException;

/**
* 注入 {@link WxCpConfigStorage} 的实现


+ 35
- 14
weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/api/WxCpServiceImpl.java View File

@@ -8,7 +8,7 @@ import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;

import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.bean.result.WxAccessToken;
import me.chanjar.weixin.common.util.GsonHelper;
import me.chanjar.weixin.enterprise.bean.*;
import me.chanjar.weixin.enterprise.util.http.SimpleGetRequestExecutor;
@@ -23,9 +23,9 @@ import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import me.chanjar.weixin.enterprise.bean.WxCpDepart;
import me.chanjar.weixin.enterprise.bean.result.WxError;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.enterprise.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.enterprise.bean.result.WxUser;
import me.chanjar.weixin.enterprise.bean.WxCpUser;
import me.chanjar.weixin.enterprise.exception.WxErrorException;
import me.chanjar.weixin.common.util.FileUtils;
import me.chanjar.weixin.enterprise.util.http.MediaDownloadRequestExecutor;
@@ -34,7 +34,6 @@ import me.chanjar.weixin.enterprise.util.http.RequestExecutor;
import me.chanjar.weixin.enterprise.util.http.SimplePostRequestExecutor;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.internal.Streams;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
@@ -165,7 +164,7 @@ public class WxCpServiceImpl implements WxCpService {

public void departDelete(Integer departId) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/department/delete?id=" + departId;
String responseContent = execute(new SimpleGetRequestExecutor(), url, null);
execute(new SimpleGetRequestExecutor(), url, null);
}

public List<WxCpDepart> departGet() throws WxErrorException {
@@ -184,28 +183,50 @@ public class WxCpServiceImpl implements WxCpService {
}

@Override
public void userCreate(WxUser user) throws WxErrorException {
// TODO
public void userCreate(WxCpUser user) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/create";
execute(new SimplePostRequestExecutor(), url, user.toJson());
}

@Override
public void userUpdate(WxUser user) throws WxErrorException {
// TODO
public void userUpdate(WxCpUser user) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/update";
execute(new SimplePostRequestExecutor(), url, user.toJson());
}

@Override
public void userDelete(String userid) throws WxErrorException {
// TODO
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/delete?userid=" + userid;
execute(new SimpleGetRequestExecutor(), url, null);
}

@Override
public WxUser userGet(String userid) throws WxErrorException {
return null;
public WxCpUser userGet(String userid) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/get?userid=" + userid;
String responseContent = execute(new SimpleGetRequestExecutor(), url, null);
return WxCpUser.fromJson(responseContent);
}

@Override
public List<WxUser> userGetByDepartment(String departmentId) throws WxErrorException {
return null;
public List<WxCpUser> userGetByDepart(Integer departId, Boolean fetchChild, Integer status) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?department_id=" + departId;
String params = "";
if (fetchChild != null) {
params += "&fetch_child=" + (fetchChild ? "1" : "0");
}
if (status != null) {
params += "&status=" + status;
} else {
params += "&status=0";
}

String responseContent = execute(new SimpleGetRequestExecutor(), url, params);
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
return WxCpGsonBuilder.INSTANCE.create()
.fromJson(
tmpJsonElement.getAsJsonObject().get("userlist"),
new TypeToken<List<WxCpUser>>() { }.getType()
);
}

/**


+ 1
- 1
weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/api/WxInMemoryCpConfigStorage.java View File

@@ -1,6 +1,6 @@
package me.chanjar.weixin.enterprise.api;

import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.bean.result.WxAccessToken;

/**
* 基于内存的微信配置provider,在实际生产环境中应该将这些配置持久化


+ 143
- 0
weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/WxCpUser.java View File

@@ -0,0 +1,143 @@
package me.chanjar.weixin.enterprise.bean;

import me.chanjar.weixin.enterprise.util.json.WxCpGsonBuilder;

import java.sql.PseudoColumnUsage;
import java.util.ArrayList;
import java.util.List;

/**
* 微信用户信息
*
* @author Daniel Qian
*/
public class WxCpUser {

private String userId;
private String name;
private Integer[] departIds;
private String position;
private String mobile;
private String gender;
private String tel;
private String email;
private String weiXinId;
private final List<Attr> extAttrs = new ArrayList<Attr>();

public String getUserId() {
return userId;
}

public void setUserId(String userId) {
this.userId = userId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer[] getDepartIds() {
return departIds;
}

public void setDepartIds(Integer[] departIds) {
this.departIds = departIds;
}

public String getGender() {
return gender;
}

public void setGender(String gender) {
this.gender = gender;
}

public String getPosition() {
return position;
}

public void setPosition(String position) {
this.position = position;
}

public String getMobile() {
return mobile;
}

public void setMobile(String mobile) {
this.mobile = mobile;
}

public String getTel() {
return tel;
}

public void setTel(String tel) {
this.tel = tel;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getWeiXinId() {
return weiXinId;
}

public void setWeiXinId(String weiXinId) {
this.weiXinId = weiXinId;
}

public String toJson() {
return WxCpGsonBuilder.INSTANCE.create().toJson(this);
}

public static WxCpUser fromJson(String json) {
return WxCpGsonBuilder.INSTANCE.create().fromJson(json, WxCpUser.class);
}

public void addExtAttr(String name, String value) {
this.extAttrs.add(new Attr(name, value));
}

public List<Attr> getExtAttrs() {
return extAttrs;
}

public static class Attr {

private String name;
private String value;

public Attr(String name, String value) {
this.name = name;
this.value = value;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

}

}

+ 0
- 55
weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/result/WxMassSendResult.java View File

@@ -1,55 +0,0 @@
package me.chanjar.weixin.enterprise.bean.result;

import me.chanjar.weixin.enterprise.util.json.WxCpGsonBuilder;

/**
* <pre>
* 群发消息一发送就返回的结果
*
* 真正的群发消息是否发送成功要看
* http://mp.weixin.qq.com/wiki/index.php?title=高级群发接口#.E4.BA.8B.E4.BB.B6.E6.8E.A8.E9.80.81.E7.BE.A4.E5.8F.91.E7.BB.93.E6.9E.9C
*
* </pre>
* @author Daniel Qian
*
*/
public class WxMassSendResult {

private String errorCode;
private String errorMsg;
private String msgId;

public String getErrorCode() {
return errorCode;
}

public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}

public String getErrorMsg() {
return errorMsg;
}

public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}

public String getMsgId() {
return msgId;
}

public void setMsgId(String msgId) {
this.msgId = msgId;
}
public static WxMassSendResult fromJson(String json) {
return WxCpGsonBuilder.create().fromJson(json, WxMassSendResult.class);
}

@Override
public String toString() {
return "WxMassSendResult [errcode=" + errorCode + ", errmsg=" + errorMsg + ", msg_id=" + msgId + "]";
}
}

+ 0
- 52
weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/result/WxMassUploadResult.java View File

@@ -1,52 +0,0 @@
package me.chanjar.weixin.enterprise.bean.result;

import me.chanjar.weixin.enterprise.util.json.WxCpGsonBuilder;

/**
* <pre>
* 上传群发用的素材的结果
* 视频和图文消息需要在群发前上传素材
* </pre>
* @author Daniel Qian
*
*/
public class WxMassUploadResult {

private String type;
private String mediaId;
private long createdAt;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getMediaId() {
return mediaId;
}

public void setMediaId(String mediaId) {
this.mediaId = mediaId;
}

public long getCreatedAt() {
return createdAt;
}

public void setCreatedAt(long createdAt) {
this.createdAt = createdAt;
}

public static WxMassUploadResult fromJson(String json) {
return WxCpGsonBuilder.create().fromJson(json, WxMassUploadResult.class);
}

@Override
public String toString() {
return "WxUploadResult [type=" + type + ", media_id=" + mediaId + ", created_at=" + createdAt + "]";
}

}

+ 0
- 95
weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/result/WxUser.java View File

@@ -1,95 +0,0 @@
package me.chanjar.weixin.enterprise.bean.result;

import me.chanjar.weixin.enterprise.util.json.WxCpGsonBuilder;

/**
* 微信用户信息
* @author Daniel Qian
*
*/
public class WxUser {

protected boolean subscribe;
protected String openId;
protected String nickname;
protected String sex;
protected String language;
protected String city;
protected String province;
protected String country;
protected String headImgUrl;
protected long subscribeTime;
protected String unionId;
public boolean isSubscribe() {
return subscribe;
}
public void setSubscribe(boolean subscribe) {
this.subscribe = subscribe;
}
public String getOpenId() {
return openId;
}
public void setOpenId(String openId) {
this.openId = openId;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getHeadImgUrl() {
return headImgUrl;
}
public void setHeadImgUrl(String headImgUrl) {
this.headImgUrl = headImgUrl;
}
public long getSubscribeTime() {
return subscribeTime;
}
public void setSubscribeTime(long subscribeTime) {
this.subscribeTime = subscribeTime;
}
public String getUnionId() {
return unionId;
}
public void setUnionId(String unionId) {
this.unionId = unionId;
}
public static WxUser fromJson(String json) {
return WxCpGsonBuilder.INSTANCE.create().fromJson(json, WxUser.class);
}
}

+ 0
- 47
weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/result/WxUserList.java View File

@@ -1,47 +0,0 @@
package me.chanjar.weixin.enterprise.bean.result;

import java.util.ArrayList;
import java.util.List;

import me.chanjar.weixin.enterprise.util.json.WxCpGsonBuilder;

/**
* 关注者列表
* @author Daniel Qian
*
*/
public class WxUserList {

protected int total = -1;
protected int count = -1;
protected List<String> openIds = new ArrayList<String>();
protected String nextOpenId;
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public List<String> getOpenIds() {
return openIds;
}
public void setOpenIds(List<String> openIds) {
this.openIds = openIds;
}
public String getNextOpenId() {
return nextOpenId;
}
public void setNextOpenId(String nextOpenId) {
this.nextOpenId = nextOpenId;
}
public static WxUserList fromJson(String json) {
return WxCpGsonBuilder.INSTANCE.create().fromJson(json, WxUserList.class);
}
}

+ 1
- 1
weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/exception/WxErrorException.java View File

@@ -1,6 +1,6 @@
package me.chanjar.weixin.enterprise.exception;

import me.chanjar.weixin.enterprise.bean.result.WxError;
import me.chanjar.weixin.common.bean.result.WxError;

public class WxErrorException extends Exception {



+ 1
- 1
weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/http/MediaDownloadRequestExecutor.java View File

@@ -13,7 +13,7 @@ import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.ContentType;

import me.chanjar.weixin.enterprise.bean.result.WxError;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.enterprise.exception.WxErrorException;
import me.chanjar.weixin.common.util.FileUtils;



+ 1
- 1
weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/http/MediaUploadRequestExecutor.java View File

@@ -10,7 +10,7 @@ import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;

import me.chanjar.weixin.enterprise.bean.result.WxError;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.enterprise.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.enterprise.exception.WxErrorException;



+ 1
- 1
weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/http/SimpleGetRequestExecutor.java View File

@@ -6,7 +6,7 @@ import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;

import me.chanjar.weixin.enterprise.bean.result.WxError;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.enterprise.exception.WxErrorException;

/**


+ 1
- 1
weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/http/SimplePostRequestExecutor.java View File

@@ -2,7 +2,7 @@ package me.chanjar.weixin.enterprise.util.http;

import java.io.IOException;

import me.chanjar.weixin.enterprise.bean.result.WxError;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.enterprise.exception.WxErrorException;
import org.apache.http.Consts;
import org.apache.http.client.ClientProtocolException;


+ 1
- 1
weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/json/WxCpAccessTokenAdapter.java View File

@@ -10,7 +10,7 @@ package me.chanjar.weixin.enterprise.util.json;

import com.google.gson.*;
import me.chanjar.weixin.common.util.GsonHelper;
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.bean.result.WxAccessToken;

import java.lang.reflect.Type;



+ 3
- 3
weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/json/WxCpGsonBuilder.java View File

@@ -3,7 +3,8 @@ package me.chanjar.weixin.enterprise.util.json;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.bean.result.WxAccessToken;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.enterprise.bean.*;
import me.chanjar.weixin.enterprise.bean.result.*;

@@ -16,8 +17,7 @@ public class WxCpGsonBuilder {
INSTANCE.registerTypeAdapter(WxCpMessage.class, new WxCpMessageGsonAdapter());
INSTANCE.registerTypeAdapter(WxCpMenu.class, new WxCpMenuGsonAdapter());
INSTANCE.registerTypeAdapter(WxCpDepart.class, new WxCpDepartGsonAdapter());
INSTANCE.registerTypeAdapter(WxUser.class, new WxCpUserGsonAdapter());
INSTANCE.registerTypeAdapter(WxUserList.class, new WxCpUserListGsonAdapter());
INSTANCE.registerTypeAdapter(WxCpUser.class, new WxCpUserGsonAdapter());
INSTANCE.registerTypeAdapter(WxAccessToken.class, new WxCpAccessTokenAdapter());
INSTANCE.registerTypeAdapter(WxError.class, new WxErrorAdapter());
INSTANCE.registerTypeAdapter(WxMediaUploadResult.class, new WxCpMediaUploadResultAdapter());


+ 91
- 29
weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/json/WxCpUserGsonAdapter.java View File

@@ -10,44 +10,106 @@ package me.chanjar.weixin.enterprise.util.json;

import java.lang.reflect.Type;

import com.google.gson.*;
import me.chanjar.weixin.common.util.GsonHelper;
import me.chanjar.weixin.enterprise.bean.result.WxUser;

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 me.chanjar.weixin.enterprise.bean.WxCpUser;

/**
*
* @author Daniel Qian
*
*/
public class WxCpUserGsonAdapter implements JsonDeserializer<WxUser> {
public class WxCpUserGsonAdapter implements JsonDeserializer<WxCpUser>, JsonSerializer<WxCpUser> {

public WxUser deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
public WxCpUser deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
JsonObject o = json.getAsJsonObject();
WxUser wxUser = new WxUser();
wxUser.setSubscribe(new Integer(0).equals(GsonHelper.getInteger(o, "subscribe")) ? false : true);
wxUser.setCity(GsonHelper.getString(o, "city"));
wxUser.setCountry(GsonHelper.getString(o, "country"));
wxUser.setHeadImgUrl(GsonHelper.getString(o, "headimgurl"));
wxUser.setLanguage(GsonHelper.getString(o, "language"));
wxUser.setNickname(GsonHelper.getString(o, "nickname"));
wxUser.setOpenId(GsonHelper.getString(o, "openid"));
wxUser.setProvince(GsonHelper.getString(o, "province"));
wxUser.setSubscribeTime(GsonHelper.getLong(o, "subscribe_time"));
wxUser.setUnionId(GsonHelper.getString(o, "unionid"));
Integer sex = GsonHelper.getInteger(o, "sex");
if(new Integer(1).equals(sex)) {
wxUser.setSex("男");
} else if (new Integer(2).equals(sex)) {
wxUser.setSex("女");
WxCpUser user = new WxCpUser();
user.setUserId(GsonHelper.getString(o, "userid"));
user.setName(GsonHelper.getString(o, "name"));

if(o.get("department") != null) {
JsonArray departJsonArray = o.get("department").getAsJsonArray();
Integer[] departIds = new Integer[departJsonArray.size()];
int i = 0;
for (JsonElement jsonElement : departJsonArray) {
departIds[i++] = jsonElement.getAsInt();
}
user.setDepartIds(departIds);
}

user.setPosition(GsonHelper.getString(o, "position"));
user.setMobile(GsonHelper.getString(o, "mobile"));
Integer gender = GsonHelper.getInteger(o, "gender");
if (new Integer(1).equals(gender)) {
user.setGender("男");
} else if (new Integer(2).equals(gender)) {
user.setGender("女");
} else {
wxUser.setSex("未知");
user.setGender("未知");
}
user.setTel(GsonHelper.getString(o, "tel"));
user.setEmail(GsonHelper.getString(o, "email"));
user.setWeiXinId(GsonHelper.getString(o, "weixinid"));

if (GsonHelper.isNotNull(o.get("extattr"))) {
JsonArray attrJsonElements = o.get("extattr").getAsJsonObject().get("attrs").getAsJsonArray();
for (JsonElement attrJsonElement : attrJsonElements) {
WxCpUser.Attr attr = new WxCpUser.Attr(
GsonHelper.getString(attrJsonElement.getAsJsonObject(), "name"),
GsonHelper.getString(attrJsonElement.getAsJsonObject(), "value")
);
user.getExtAttrs().add(attr);
}
}
return user;
}

@Override
public JsonElement serialize(WxCpUser user, Type typeOfSrc, JsonSerializationContext context) {
JsonObject o = new JsonObject();
if (user.getUserId() != null) {
o.addProperty("userid", user.getUserId());
}
if (user.getName() != null) {
o.addProperty("name", user.getName());
}
if (user.getDepartIds() != null) {
JsonArray jsonArray = new JsonArray();
for (Integer departId : user.getDepartIds()) {
jsonArray.add(new JsonPrimitive(departId));
}
o.add("department", jsonArray);
}
if (user.getPosition() != null) {
o.addProperty("position", user.getPosition());
}
if (user.getMobile() != null) {
o.addProperty("mobile", user.getMobile());
}
if (user.getGender() != null) {
o.addProperty("gender", user.getGender().equals("男") ? 0 : 1);
}
if (user.getTel() != null) {
o.addProperty("tel", user.getTel());
}
if (user.getEmail() != null) {
o.addProperty("email", user.getEmail());
}
if (user.getWeiXinId() != null) {
o.addProperty("weixinid", user.getWeiXinId());
}
if (user.getExtAttrs().size() > 0) {
JsonArray attrsJsonArray = new JsonArray();
for (WxCpUser.Attr attr : user.getExtAttrs()) {
JsonObject attrJson = new JsonObject();
attrJson.addProperty("name", attr.getName());
attrJson.addProperty("value", attr.getValue());
attrsJsonArray.add(attrJson);
}
JsonObject attrsJson = new JsonObject();
attrsJson.add("attrs", attrsJsonArray);
o.add("extattr", attrsJson);
}
return wxUser;
return o;
}

}

+ 0
- 43
weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/json/WxCpUserListGsonAdapter.java View File

@@ -1,43 +0,0 @@
/*
* KINGSTAR MEDIA SOLUTIONS Co.,LTD. Copyright c 2005-2013. All rights reserved.
*
* This source code is the property of KINGSTAR MEDIA SOLUTIONS LTD. It is intended
* only for the use of KINGSTAR MEDIA application development. Reengineering, reproduction
* arose from modification of the original source, or other redistribution of this source
* is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD.
*/
package me.chanjar.weixin.enterprise.util.json;

import java.lang.reflect.Type;

import me.chanjar.weixin.common.util.GsonHelper;
import me.chanjar.weixin.enterprise.bean.result.WxUserList;

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;

/**
*
* @author Daniel Qian
*
*/
public class WxCpUserListGsonAdapter implements JsonDeserializer<WxUserList> {

public WxUserList deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject o = json.getAsJsonObject();
WxUserList wxUserList = new WxUserList();
wxUserList.setTotal(GsonHelper.getInteger(o, "total"));
wxUserList.setCount(GsonHelper.getInteger(o, "count"));
wxUserList.setNextOpenId(GsonHelper.getString(o, "next_openid"));
JsonArray data = o.get("data").getAsJsonObject().get("openid").getAsJsonArray();
for (int i = 0; i < data.size(); i++) {
wxUserList.getOpenIds().add(GsonHelper.getAsString(data.get(i)));
}
return wxUserList;
}

}

+ 1
- 1
weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/json/WxErrorAdapter.java View File

@@ -10,7 +10,7 @@ package me.chanjar.weixin.enterprise.util.json;

import com.google.gson.*;
import me.chanjar.weixin.common.util.GsonHelper;
import me.chanjar.weixin.enterprise.bean.result.WxError;
import me.chanjar.weixin.common.bean.result.WxError;

import java.lang.reflect.Type;



+ 1
- 1
weixin-java-enterprise/src/test/java/me/chanjar/weixin/common/bean/WxAccessTokenTest.java View File

@@ -1,6 +1,6 @@
package me.chanjar.weixin.common.bean;

import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.bean.result.WxAccessToken;
import org.testng.Assert;
import org.testng.annotations.Test;



+ 7
- 2
weixin-java-enterprise/src/test/java/me/chanjar/weixin/enterprise/api/WxCpDepartAPITest.java View File

@@ -27,7 +27,7 @@ public class WxCpDepartAPITest {

public void testDepartCreate() throws WxErrorException {
WxCpDepart depart = new WxCpDepart();
depart.setName("测试部门");
depart.setName("子部门" + System.currentTimeMillis());
depart.setParentId(1);
depart.setOrder(1);
Integer departId = wxService.departCreate(depart);
@@ -35,23 +35,28 @@ public class WxCpDepartAPITest {

@Test(dependsOnMethods = "testDepartCreate")
public void testDepartGet() throws WxErrorException {
System.out.println("=================获取部门");
List<WxCpDepart> departList = wxService.departGet();
Assert.assertNotNull(departList);
Assert.assertTrue(departList.size() > 0);
for (WxCpDepart g : departList) {
depart = g;
System.out.println(depart.getId() + ":" + depart.getName());
Assert.assertNotNull(g.getName());
}
}

@Test(dependsOnMethods = { "testDepartGet", "testDepartCreate" })
public void testDepartUpdate() throws WxErrorException {
depart.setName("部门改名");
System.out.println("=================更新部门");
depart.setName("子部门改名" + System.currentTimeMillis());
wxService.departUpdate(depart);
}

@Test(dependsOnMethods = "testDepartUpdate")
public void testDepartDelete() throws WxErrorException {
System.out.println("=================删除部门");
System.out.println(depart.getId() + ":" + depart.getName());
wxService.departDelete(depart.getId());
}



+ 66
- 0
weixin-java-enterprise/src/test/java/me/chanjar/weixin/enterprise/api/WxCpUserAPITest.java View File

@@ -0,0 +1,66 @@
package me.chanjar.weixin.enterprise.api;

import com.google.inject.Inject;
import me.chanjar.weixin.enterprise.bean.WxCpDepart;
import me.chanjar.weixin.enterprise.bean.WxCpUser;
import me.chanjar.weixin.enterprise.exception.WxErrorException;
import org.testng.Assert;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;

import java.util.List;

/**
* 测试用户接口
*
* @author Daniel Qian
*/
@Test(groups = "userAPI", dependsOnGroups = "baseAPI")
@Guice(modules = ApiTestModule.class)
public class WxCpUserAPITest {

@Inject
protected WxCpServiceImpl wxService;

protected WxCpDepart depart;

public void testUserCreate() throws WxErrorException {
WxCpUser user = new WxCpUser();
user.setUserId("xiaohe.yang");
user.setName("杨宝");
user.setDepartIds(new Integer[] { 9, 8 });
user.setEmail("yangxiaohe@ddd.com");
user.setGender("女");
user.setMobile("13564684979");
user.setPosition("老婆");
user.setTel("3300393");
user.addExtAttr("爱好", "老公");
wxService.userCreate(user);
}

@Test(dependsOnMethods = "testUserCreate")
public void testUserUpdate() throws WxErrorException {
WxCpUser user = new WxCpUser();
user.setUserId("xiaohe.yang");
user.setName("杨宝");
user.addExtAttr("爱好", "老公2");
wxService.userUpdate(user);
}

@Test(dependsOnMethods = "testUserUpdate")
public void testUserGet() throws WxErrorException {
WxCpUser user = wxService.userGet("xiaohe.yang");
Assert.assertNotNull(user);
}

@Test(dependsOnMethods = "testUserGet")
public void testUserGetByDepart() throws WxErrorException {
List<WxCpUser> users = wxService.userGetByDepart(1, true, 0);
Assert.assertNotEquals(users.size(), 0);
}

@Test(dependsOnMethods = "testUserGetByDepart")
public void testUserDelete() throws WxErrorException {
wxService.userDelete("xiaohe.yang");
}
}

+ 1
- 1
weixin-java-enterprise/src/test/java/me/chanjar/weixin/enterprise/bean/WxErrorTest.java View File

@@ -3,7 +3,7 @@ package me.chanjar.weixin.enterprise.bean;
import org.testng.Assert;
import org.testng.annotations.Test;

import me.chanjar.weixin.enterprise.bean.result.WxError;
import me.chanjar.weixin.common.bean.result.WxError;

@Test
public class WxErrorTest {


Loading…
Cancel
Save