Skip to content

Commit bdbd92b

Browse files
committed
binarywang#583 企业微信通讯录管理增加邀请成员接口
1 parent 01b1be3 commit bdbd92b

File tree

3 files changed

+97
-19
lines changed

3 files changed

+97
-19
lines changed

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpUserService.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package me.chanjar.weixin.cp.api;
22

33
import me.chanjar.weixin.common.exception.WxErrorException;
4+
import me.chanjar.weixin.cp.bean.WxCpInviteResult;
45
import me.chanjar.weixin.cp.bean.WxCpUser;
56

67
import java.util.List;
@@ -84,18 +85,18 @@ public interface WxCpUserService {
8485

8586
/**
8687
* <pre>
87-
* 邀请成员关注
88-
* http://qydev.weixin.qq.com/wiki/index.php?title=管理成员#.E9.82.80.E8.AF.B7.E6.88.90.E5.91.98.E5.85.B3.E6.B3.A8
88+
* 邀请成员
89+
* 企业可通过接口批量邀请成员使用企业微信,邀请后将通过短信或邮件下发通知。
90+
* 请求方式:POST(HTTPS)
91+
* 请求地址: https://qyapi.weixin.qq.com/cgi-bin/batch/invite?access_token=ACCESS_TOKEN
92+
* 文档地址:https://work.weixin.qq.com/api/doc#12543
8993
* </pre>
9094
*
91-
* @param userId 用户的userid
92-
* @param inviteTips 推送到微信上的提示语(只有认证号可以使用)。当使用微信推送时,该字段默认为“请关注XXX企业号”,邮件邀请时,该字段无效。
93-
* @return 1:微信邀请 2.邮件邀请
94-
* @deprecated api obsoleted. 邀请关注的功能微信企业号已经下线了,
95-
* 详细请参考该链接点击查看 https://qy.weixin.qq.com/cgi-bin/homepagenotify?action=get&id=46
95+
* @param userIds 成员ID列表, 最多支持1000个。
96+
* @param partyIds 部门ID列表,最多支持100个。
97+
* @param tagIds 标签ID列表,最多支持100个。
9698
*/
97-
@Deprecated
98-
int invite(String userId, String inviteTips) throws WxErrorException;
99+
WxCpInviteResult invite(List<String> userIds, List<String> partyIds, List<String> tagIds) throws WxErrorException;
99100

100101
/**
101102
* <pre>

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpUserServiceImpl.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
import me.chanjar.weixin.common.exception.WxErrorException;
77
import me.chanjar.weixin.cp.api.WxCpService;
88
import me.chanjar.weixin.cp.api.WxCpUserService;
9+
import me.chanjar.weixin.cp.bean.WxCpInviteResult;
910
import me.chanjar.weixin.cp.bean.WxCpUser;
1011
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
11-
import org.apache.commons.lang3.StringUtils;
1212

1313
import java.util.List;
1414
import java.util.Map;
@@ -116,17 +116,34 @@ public List<WxCpUser> listSimpleByDepartment(Integer departId, Boolean fetchChil
116116
}
117117

118118
@Override
119-
@Deprecated
120-
public int invite(String userId, String inviteTips) throws WxErrorException {
121-
String url = "https://qyapi.weixin.qq.com/cgi-bin/invite/send";
119+
public WxCpInviteResult invite(List<String> userIds, List<String> partyIds, List<String> tagIds) throws WxErrorException {
120+
String url = "https://qyapi.weixin.qq.com/cgi-bin/batch/invite";
122121
JsonObject jsonObject = new JsonObject();
123-
jsonObject.addProperty("userid", userId);
124-
if (StringUtils.isNotEmpty(inviteTips)) {
125-
jsonObject.addProperty("invite_tips", inviteTips);
122+
if (userIds != null) {
123+
JsonArray jsonArray = new JsonArray();
124+
for (String userId : userIds) {
125+
jsonArray.add(new JsonPrimitive(userId));
126+
}
127+
jsonObject.add("user", jsonArray);
126128
}
127-
String responseContent = this.mainService.post(url, jsonObject.toString());
128-
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
129-
return tmpJsonElement.getAsJsonObject().get("type").getAsInt();
129+
130+
if (partyIds != null) {
131+
JsonArray jsonArray = new JsonArray();
132+
for (String userId : partyIds) {
133+
jsonArray.add(new JsonPrimitive(userId));
134+
}
135+
jsonObject.add("party", jsonArray);
136+
}
137+
138+
if (tagIds != null) {
139+
JsonArray jsonArray = new JsonArray();
140+
for (String tagId : tagIds) {
141+
jsonArray.add(new JsonPrimitive(tagId));
142+
}
143+
jsonObject.add("tag", jsonArray);
144+
}
145+
146+
return WxCpInviteResult.fromJson(this.mainService.post(url, jsonObject.toString()));
130147
}
131148

132149
@Override
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package me.chanjar.weixin.cp.bean;
2+
3+
import com.google.common.base.Splitter;
4+
import com.google.gson.annotations.SerializedName;
5+
import lombok.Data;
6+
import me.chanjar.weixin.common.util.ToStringUtils;
7+
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
8+
import org.apache.commons.lang3.StringUtils;
9+
10+
import java.io.Serializable;
11+
import java.util.Collections;
12+
import java.util.List;
13+
14+
/**
15+
* 邀请成员的结果对象类.
16+
* Created by Binary Wang on 2018-5-13.
17+
*
18+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
19+
*/
20+
@Data
21+
public class WxCpInviteResult implements Serializable {
22+
private static final long serialVersionUID = 1420065684270213578L;
23+
24+
@Override
25+
public String toString() {
26+
return ToStringUtils.toSimpleString(this);
27+
}
28+
29+
public static WxCpInviteResult fromJson(String json) {
30+
return WxCpGsonBuilder.INSTANCE.create().fromJson(json, WxCpInviteResult.class);
31+
}
32+
33+
@SerializedName("errcode")
34+
private Integer errCode;
35+
36+
@SerializedName("errmsg")
37+
private String errMsg;
38+
39+
@SerializedName("invaliduser")
40+
private String invalidUsers;
41+
42+
@SerializedName("invalidparty")
43+
private String[] invalidParties;
44+
45+
@SerializedName("invalidtag")
46+
private String[] invalidTags;
47+
48+
public List<String> getInvalidUserList() {
49+
return this.content2List(this.invalidUsers);
50+
}
51+
52+
private List<String> content2List(String content) {
53+
if (StringUtils.isBlank(content)) {
54+
return Collections.emptyList();
55+
}
56+
57+
return Splitter.on("|").splitToList(content);
58+
}
59+
60+
}

0 commit comments

Comments
 (0)