🎨 #1528 优化企业微信获取客户列表的接口,同时增加更多的企业微信错误码枚举值

This commit is contained in:
Binary Wang
2020-04-25 13:31:27 +08:00
parent 619b377326
commit 6da4b4fca1
5 changed files with 369 additions and 62 deletions

View File

@ -85,7 +85,7 @@ public interface WxCpExternalContactService {
* @return List of CpUser id
* @throws WxErrorException .
*/
List<String> listFollowUser() throws WxErrorException;
List<String> listFollowers() throws WxErrorException;
/**
* 企业和第三方可通过此接口,获取所有离职成员的客户列表,并可进一步调用离职成员的外部联系人再分配接口将这些客户重新分配给其他企业成员。

View File

@ -2,23 +2,26 @@ package me.chanjar.weixin.cp.api.impl;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxCpErrorMsgEnum;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpExternalContactService;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.*;
import org.apache.commons.lang3.ArrayUtils;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.ExternalContact.*;
/**
* @author 曹祖鹏 & yuanqixun
*/
@RequiredArgsConstructor
public class WxCpExternalContactServiceImpl implements WxCpExternalContactService {
private WxCpService mainService;
public WxCpExternalContactServiceImpl(WxCpService mainService) {
this.mainService = mainService;
}
private final WxCpService mainService;
@Override
public WxCpUserExternalContactInfo getExternalContact(String userId) throws WxErrorException {
@ -37,22 +40,30 @@ public class WxCpExternalContactServiceImpl implements WxCpExternalContactServic
@Override
public List<String> listExternalContacts(String userId) throws WxErrorException {
final String url = this.mainService.getWxCpConfigStorage().getApiUrl(LIST_EXTERNAL_CONTACT + userId);
String responseContent = this.mainService.get(url, null);
return WxCpUserExternalContactList.fromJson(responseContent).getExternalUserId();
try {
String responseContent = this.mainService.get(url, null);
return WxCpUserExternalContactList.fromJson(responseContent).getExternalUserId();
} catch (WxErrorException e) {
// not external contact,无客户则返回空列表
if (e.getError().getErrorCode() == WxCpErrorMsgEnum.CODE_84061.getCode()) {
return Collections.emptyList();
}
throw e;
}
}
@Override
public List<String> listFollowUser() throws WxErrorException {
public List<String> listFollowers() throws WxErrorException {
final String url = this.mainService.getWxCpConfigStorage().getApiUrl(GET_FOLLOW_USER_LIST);
String responseContent = this.mainService.get(url, null);
return WxCpUserWithExternalPermission.fromJson(responseContent).getFollowUser();
return WxCpUserWithExternalPermission.fromJson(responseContent).getFollowers();
}
@Override
public WxCpUserExternalUnassignList listUnassignedList(Integer pageIndex, Integer pageSize) throws WxErrorException {
JsonObject json = new JsonObject();
json.addProperty("page_id",pageIndex == null ? 0 : pageIndex);
json.addProperty("page_size",pageSize == null ? 100 : pageSize);
json.addProperty("page_id", pageIndex == null ? 0 : pageIndex);
json.addProperty("page_size", pageSize == null ? 100 : pageSize);
final String url = this.mainService.getWxCpConfigStorage().getApiUrl(LIST_UNASSIGNED_CONTACT);
final String result = this.mainService.post(url, json.toString());
return WxCpUserExternalUnassignList.fromJson(result);
@ -72,18 +83,18 @@ public class WxCpExternalContactServiceImpl implements WxCpExternalContactServic
@Override
public WxCpUserExternalGroupChatList listGroupChat(Integer pageIndex, Integer pageSize, int status, String[] userIds, String[] partyIds) throws WxErrorException {
JsonObject json = new JsonObject();
json.addProperty("offset",pageIndex == null ? 0 : pageIndex);
json.addProperty("limit",pageSize == null ? 100 : pageSize);
json.addProperty("status_filter",status);
if(ArrayUtils.isNotEmpty(userIds) || ArrayUtils.isNotEmpty(partyIds)){
json.addProperty("offset", pageIndex == null ? 0 : pageIndex);
json.addProperty("limit", pageSize == null ? 100 : pageSize);
json.addProperty("status_filter", status);
if (ArrayUtils.isNotEmpty(userIds) || ArrayUtils.isNotEmpty(partyIds)) {
JsonObject ownerFilter = new JsonObject();
if(ArrayUtils.isNotEmpty(userIds)){
if (ArrayUtils.isNotEmpty(userIds)) {
json.add("userid", new Gson().toJsonTree(userIds).getAsJsonArray());
}
if(ArrayUtils.isNotEmpty(partyIds)){
if (ArrayUtils.isNotEmpty(partyIds)) {
json.add("partyid", new Gson().toJsonTree(partyIds).getAsJsonArray());
}
json.add("owner_filter",ownerFilter);
json.add("owner_filter", ownerFilter);
}
final String url = this.mainService.getWxCpConfigStorage().getApiUrl(GROUP_CHAT_LIST);
final String result = this.mainService.post(url, json.toString());
@ -102,13 +113,13 @@ public class WxCpExternalContactServiceImpl implements WxCpExternalContactServic
@Override
public WxCpUserExternalUserBehaviorStatistic getUserBehaviorStatistic(Date startTime, Date endTime, String[] userIds, String[] partyIds) throws WxErrorException {
JsonObject json = new JsonObject();
json.addProperty("start_time", startTime.getTime()/1000);
json.addProperty("end_time", endTime.getTime()/1000);
if(ArrayUtils.isNotEmpty(userIds) || ArrayUtils.isNotEmpty(partyIds)){
if(ArrayUtils.isNotEmpty(userIds)){
json.addProperty("start_time", startTime.getTime() / 1000);
json.addProperty("end_time", endTime.getTime() / 1000);
if (ArrayUtils.isNotEmpty(userIds) || ArrayUtils.isNotEmpty(partyIds)) {
if (ArrayUtils.isNotEmpty(userIds)) {
json.add("userid", new Gson().toJsonTree(userIds).getAsJsonArray());
}
if(ArrayUtils.isNotEmpty(partyIds)){
if (ArrayUtils.isNotEmpty(partyIds)) {
json.add("partyid", new Gson().toJsonTree(partyIds).getAsJsonArray());
}
}
@ -120,20 +131,20 @@ public class WxCpExternalContactServiceImpl implements WxCpExternalContactServic
@Override
public WxCpUserExternalGroupChatStatistic getGroupChatStatistic(Date startTime, Integer orderBy, Integer orderAsc, Integer pageIndex, Integer pageSize, String[] userIds, String[] partyIds) throws WxErrorException {
JsonObject json = new JsonObject();
json.addProperty("day_begin_time", startTime.getTime()/1000);
json.addProperty("day_begin_time", startTime.getTime() / 1000);
json.addProperty("order_by", orderBy == null ? 1 : orderBy);
json.addProperty("order_asc",orderAsc == null ? 0 : orderAsc);
json.addProperty("offset",pageIndex == null ? 0 : pageIndex);
json.addProperty("limit",pageSize == null ? 500 : pageSize);
if(ArrayUtils.isNotEmpty(userIds) || ArrayUtils.isNotEmpty(partyIds)){
json.addProperty("order_asc", orderAsc == null ? 0 : orderAsc);
json.addProperty("offset", pageIndex == null ? 0 : pageIndex);
json.addProperty("limit", pageSize == null ? 500 : pageSize);
if (ArrayUtils.isNotEmpty(userIds) || ArrayUtils.isNotEmpty(partyIds)) {
JsonObject ownerFilter = new JsonObject();
if(ArrayUtils.isNotEmpty(userIds)){
if (ArrayUtils.isNotEmpty(userIds)) {
json.add("userid_list", new Gson().toJsonTree(userIds).getAsJsonArray());
}
if(ArrayUtils.isNotEmpty(partyIds)){
if (ArrayUtils.isNotEmpty(partyIds)) {
json.add("userid_list", new Gson().toJsonTree(partyIds).getAsJsonArray());
}
json.add("owner_filter",ownerFilter);
json.add("owner_filter", ownerFilter);
}
final String url = this.mainService.getWxCpConfigStorage().getApiUrl(LIST_GROUP_CHAT_DATA);
final String result = this.mainService.post(url, json.toString());

View File

@ -2,46 +2,26 @@ package me.chanjar.weixin.cp.bean;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import lombok.Data;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
import java.util.List;
/**
* @author 曹祖鹏
*/
@Data
public class WxCpUserWithExternalPermission {
@SerializedName("errcode")
@Expose
private Long errcode;
private Long errCode;
@SerializedName("errmsg")
@Expose
private String errmsg;
private String errMsg;
@SerializedName("follow_user")
@Expose
private List<String> followUser = null;
public Long getErrcode() {
return errcode;
}
public void setErrcode(Long errcode) {
this.errcode = errcode;
}
public String getErrmsg() {
return errmsg;
}
public void setErrmsg(String errmsg) {
this.errmsg = errmsg;
}
public List<String> getFollowUser() {
return followUser;
}
public void setFollowUser(List<String> followUser) {
this.followUser = followUser;
}
private List<String> followers = null;
public static WxCpUserWithExternalPermission fromJson(String json) {
return WxCpGsonBuilder.create().fromJson(json, WxCpUserWithExternalPermission.class);

View File

@ -38,7 +38,7 @@ public class WxCpExternalContactServiceImplTest {
@Test
public void testListExternalWithPermission() throws WxErrorException {
List<String> ret = this.wxCpService.getExternalContactService().listFollowUser();
List<String> ret = this.wxCpService.getExternalContactService().listFollowers();
System.out.println(ret);
assertNotNull(ret);
}