diff --git a/weixin-java-enterprise/README.md b/weixin-java-enterprise/README.md index d01c50601..e160e26d5 100644 --- a/weixin-java-enterprise/README.md +++ b/weixin-java-enterprise/README.md @@ -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); ``` diff --git a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/common/bean/WxAccessToken.java b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/common/bean/result/WxAccessToken.java similarity index 92% rename from weixin-java-enterprise/src/main/java/me/chanjar/weixin/common/bean/WxAccessToken.java rename to weixin-java-enterprise/src/main/java/me/chanjar/weixin/common/bean/result/WxAccessToken.java index deacb432e..fe7117b60 100644 --- a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/common/bean/WxAccessToken.java +++ b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/common/bean/result/WxAccessToken.java @@ -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; diff --git a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/result/WxError.java b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/common/bean/result/WxError.java similarity index 88% rename from weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/result/WxError.java rename to weixin-java-enterprise/src/main/java/me/chanjar/weixin/common/bean/result/WxError.java index 677a9c095..00b93b148 100644 --- a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/result/WxError.java +++ b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/common/bean/result/WxError.java @@ -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; diff --git a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/api/WxCpConfigStorage.java b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/api/WxCpConfigStorage.java index 3e63d463b..219727b77 100644 --- a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/api/WxCpConfigStorage.java +++ b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/api/WxCpConfigStorage.java @@ -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; /** * 微信客户端配置存储 diff --git a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/api/WxCpService.java b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/api/WxCpService.java index 39c0013cb..6300abd00 100644 --- a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/api/WxCpService.java +++ b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/api/WxCpService.java @@ -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 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 userGetByDepart(Integer departId, Boolean fetchChild, Integer status) throws WxErrorException; /** * 注入 {@link WxCpConfigStorage} 的实现 diff --git a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/api/WxCpServiceImpl.java b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/api/WxCpServiceImpl.java index c0e8865d7..4d74a71e8 100644 --- a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/api/WxCpServiceImpl.java +++ b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/api/WxCpServiceImpl.java @@ -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 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 userGetByDepartment(String departmentId) throws WxErrorException { - return null; + public List 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>() { }.getType() + ); } /** diff --git a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/api/WxInMemoryCpConfigStorage.java b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/api/WxInMemoryCpConfigStorage.java index 376ed7923..5873b12ad 100644 --- a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/api/WxInMemoryCpConfigStorage.java +++ b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/api/WxInMemoryCpConfigStorage.java @@ -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,在实际生产环境中应该将这些配置持久化 diff --git a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/WxCpUser.java b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/WxCpUser.java new file mode 100644 index 000000000..aa44ea610 --- /dev/null +++ b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/WxCpUser.java @@ -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 extAttrs = new ArrayList(); + + 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 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; + } + + } + +} diff --git a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/result/WxMassSendResult.java b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/result/WxMassSendResult.java deleted file mode 100644 index 7e76f002a..000000000 --- a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/result/WxMassSendResult.java +++ /dev/null @@ -1,55 +0,0 @@ -package me.chanjar.weixin.enterprise.bean.result; - -import me.chanjar.weixin.enterprise.util.json.WxCpGsonBuilder; - -/** - *
- * 群发消息一发送就返回的结果
- * 
- * 真正的群发消息是否发送成功要看
- * 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
- * 
- * 
- * @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 + "]"; - } - -} diff --git a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/result/WxMassUploadResult.java b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/result/WxMassUploadResult.java deleted file mode 100644 index eed94eadf..000000000 --- a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/result/WxMassUploadResult.java +++ /dev/null @@ -1,52 +0,0 @@ -package me.chanjar.weixin.enterprise.bean.result; - -import me.chanjar.weixin.enterprise.util.json.WxCpGsonBuilder; - -/** - *
- * 上传群发用的素材的结果
- * 视频和图文消息需要在群发前上传素材
- * 
- * @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 + "]"; - } - -} diff --git a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/result/WxUser.java b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/result/WxUser.java deleted file mode 100644 index a15955001..000000000 --- a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/result/WxUser.java +++ /dev/null @@ -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); - } - -} diff --git a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/result/WxUserList.java b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/result/WxUserList.java deleted file mode 100644 index e3c286f8c..000000000 --- a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/result/WxUserList.java +++ /dev/null @@ -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 openIds = new ArrayList(); - 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 getOpenIds() { - return openIds; - } - public void setOpenIds(List 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); - } -} diff --git a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/exception/WxErrorException.java b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/exception/WxErrorException.java index 54a51eef8..da5e47c70 100644 --- a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/exception/WxErrorException.java +++ b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/exception/WxErrorException.java @@ -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 { diff --git a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/http/MediaDownloadRequestExecutor.java b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/http/MediaDownloadRequestExecutor.java index bbe58847a..79ee8261a 100644 --- a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/http/MediaDownloadRequestExecutor.java +++ b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/http/MediaDownloadRequestExecutor.java @@ -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; diff --git a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/http/MediaUploadRequestExecutor.java b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/http/MediaUploadRequestExecutor.java index 96ef28c20..477ae1d40 100644 --- a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/http/MediaUploadRequestExecutor.java +++ b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/http/MediaUploadRequestExecutor.java @@ -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; diff --git a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/http/SimpleGetRequestExecutor.java b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/http/SimpleGetRequestExecutor.java index 4463e3c75..c71d26ec4 100644 --- a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/http/SimpleGetRequestExecutor.java +++ b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/http/SimpleGetRequestExecutor.java @@ -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; /** diff --git a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/http/SimplePostRequestExecutor.java b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/http/SimplePostRequestExecutor.java index 6cea13c1b..4268b22fa 100644 --- a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/http/SimplePostRequestExecutor.java +++ b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/http/SimplePostRequestExecutor.java @@ -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; diff --git a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/json/WxCpAccessTokenAdapter.java b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/json/WxCpAccessTokenAdapter.java index 0de5428d4..4578bff05 100644 --- a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/json/WxCpAccessTokenAdapter.java +++ b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/json/WxCpAccessTokenAdapter.java @@ -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; diff --git a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/json/WxCpGsonBuilder.java b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/json/WxCpGsonBuilder.java index a339ead41..5ec386c3c 100644 --- a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/json/WxCpGsonBuilder.java +++ b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/json/WxCpGsonBuilder.java @@ -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()); diff --git a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/json/WxCpUserGsonAdapter.java b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/json/WxCpUserGsonAdapter.java index 5da2e6ab9..e49fa2608 100644 --- a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/json/WxCpUserGsonAdapter.java +++ b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/json/WxCpUserGsonAdapter.java @@ -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 { +public class WxCpUserGsonAdapter implements JsonDeserializer, JsonSerializer { - 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("女"); - } else { - 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); } - return wxUser; + + 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 { + 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 o; } } diff --git a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/json/WxCpUserListGsonAdapter.java b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/json/WxCpUserListGsonAdapter.java deleted file mode 100644 index adcb150fd..000000000 --- a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/json/WxCpUserListGsonAdapter.java +++ /dev/null @@ -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 { - - 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; - } - -} diff --git a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/json/WxErrorAdapter.java b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/json/WxErrorAdapter.java index 2a3536117..8143c1821 100644 --- a/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/json/WxErrorAdapter.java +++ b/weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/json/WxErrorAdapter.java @@ -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; diff --git a/weixin-java-enterprise/src/test/java/me/chanjar/weixin/common/bean/WxAccessTokenTest.java b/weixin-java-enterprise/src/test/java/me/chanjar/weixin/common/bean/WxAccessTokenTest.java index c3b891f71..76bdeeaff 100644 --- a/weixin-java-enterprise/src/test/java/me/chanjar/weixin/common/bean/WxAccessTokenTest.java +++ b/weixin-java-enterprise/src/test/java/me/chanjar/weixin/common/bean/WxAccessTokenTest.java @@ -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; diff --git a/weixin-java-enterprise/src/test/java/me/chanjar/weixin/enterprise/api/WxCpDepartAPITest.java b/weixin-java-enterprise/src/test/java/me/chanjar/weixin/enterprise/api/WxCpDepartAPITest.java index 6e97a37ec..6301b1851 100644 --- a/weixin-java-enterprise/src/test/java/me/chanjar/weixin/enterprise/api/WxCpDepartAPITest.java +++ b/weixin-java-enterprise/src/test/java/me/chanjar/weixin/enterprise/api/WxCpDepartAPITest.java @@ -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 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()); } diff --git a/weixin-java-enterprise/src/test/java/me/chanjar/weixin/enterprise/api/WxCpUserAPITest.java b/weixin-java-enterprise/src/test/java/me/chanjar/weixin/enterprise/api/WxCpUserAPITest.java new file mode 100644 index 000000000..9e4b00207 --- /dev/null +++ b/weixin-java-enterprise/src/test/java/me/chanjar/weixin/enterprise/api/WxCpUserAPITest.java @@ -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 users = wxService.userGetByDepart(1, true, 0); + Assert.assertNotEquals(users.size(), 0); + } + + @Test(dependsOnMethods = "testUserGetByDepart") + public void testUserDelete() throws WxErrorException { + wxService.userDelete("xiaohe.yang"); + } +} diff --git a/weixin-java-enterprise/src/test/java/me/chanjar/weixin/enterprise/bean/WxErrorTest.java b/weixin-java-enterprise/src/test/java/me/chanjar/weixin/enterprise/bean/WxErrorTest.java index 5a6545401..ca5c3d3b4 100644 --- a/weixin-java-enterprise/src/test/java/me/chanjar/weixin/enterprise/bean/WxErrorTest.java +++ b/weixin-java-enterprise/src/test/java/me/chanjar/weixin/enterprise/bean/WxErrorTest.java @@ -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 {