@@ -1,6 +1,7 @@ | |||
package me.chanjar.weixin.cp.api; | |||
import me.chanjar.weixin.common.exception.WxErrorException; | |||
import me.chanjar.weixin.cp.bean.WxCpInviteResult; | |||
import me.chanjar.weixin.cp.bean.WxCpUser; | |||
import java.util.List; | |||
@@ -84,18 +85,18 @@ public interface WxCpUserService { | |||
/** | |||
* <pre> | |||
* 邀请成员关注 | |||
* 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 | |||
* 邀请成员 | |||
* 企业可通过接口批量邀请成员使用企业微信,邀请后将通过短信或邮件下发通知。 | |||
* 请求方式:POST(HTTPS) | |||
* 请求地址: https://qyapi.weixin.qq.com/cgi-bin/batch/invite?access_token=ACCESS_TOKEN | |||
* 文档地址:https://work.weixin.qq.com/api/doc#12543 | |||
* </pre> | |||
* | |||
* @param userId 用户的userid | |||
* @param inviteTips 推送到微信上的提示语(只有认证号可以使用)。当使用微信推送时,该字段默认为“请关注XXX企业号”,邮件邀请时,该字段无效。 | |||
* @return 1:微信邀请 2.邮件邀请 | |||
* @deprecated api obsoleted. 邀请关注的功能微信企业号已经下线了, | |||
* 详细请参考该链接点击查看 https://qy.weixin.qq.com/cgi-bin/homepagenotify?action=get&id=46 | |||
* @param userIds 成员ID列表, 最多支持1000个。 | |||
* @param partyIds 部门ID列表,最多支持100个。 | |||
* @param tagIds 标签ID列表,最多支持100个。 | |||
*/ | |||
@Deprecated | |||
int invite(String userId, String inviteTips) throws WxErrorException; | |||
WxCpInviteResult invite(List<String> userIds, List<String> partyIds, List<String> tagIds) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -6,9 +6,9 @@ import com.google.gson.reflect.TypeToken; | |||
import me.chanjar.weixin.common.exception.WxErrorException; | |||
import me.chanjar.weixin.cp.api.WxCpService; | |||
import me.chanjar.weixin.cp.api.WxCpUserService; | |||
import me.chanjar.weixin.cp.bean.WxCpInviteResult; | |||
import me.chanjar.weixin.cp.bean.WxCpUser; | |||
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; | |||
import org.apache.commons.lang3.StringUtils; | |||
import java.util.List; | |||
import java.util.Map; | |||
@@ -116,17 +116,34 @@ public class WxCpUserServiceImpl implements WxCpUserService { | |||
} | |||
@Override | |||
@Deprecated | |||
public int invite(String userId, String inviteTips) throws WxErrorException { | |||
String url = "https://qyapi.weixin.qq.com/cgi-bin/invite/send"; | |||
public WxCpInviteResult invite(List<String> userIds, List<String> partyIds, List<String> tagIds) throws WxErrorException { | |||
String url = "https://qyapi.weixin.qq.com/cgi-bin/batch/invite"; | |||
JsonObject jsonObject = new JsonObject(); | |||
jsonObject.addProperty("userid", userId); | |||
if (StringUtils.isNotEmpty(inviteTips)) { | |||
jsonObject.addProperty("invite_tips", inviteTips); | |||
if (userIds != null) { | |||
JsonArray jsonArray = new JsonArray(); | |||
for (String userId : userIds) { | |||
jsonArray.add(new JsonPrimitive(userId)); | |||
} | |||
jsonObject.add("user", jsonArray); | |||
} | |||
String responseContent = this.mainService.post(url, jsonObject.toString()); | |||
JsonElement tmpJsonElement = new JsonParser().parse(responseContent); | |||
return tmpJsonElement.getAsJsonObject().get("type").getAsInt(); | |||
if (partyIds != null) { | |||
JsonArray jsonArray = new JsonArray(); | |||
for (String userId : partyIds) { | |||
jsonArray.add(new JsonPrimitive(userId)); | |||
} | |||
jsonObject.add("party", jsonArray); | |||
} | |||
if (tagIds != null) { | |||
JsonArray jsonArray = new JsonArray(); | |||
for (String tagId : tagIds) { | |||
jsonArray.add(new JsonPrimitive(tagId)); | |||
} | |||
jsonObject.add("tag", jsonArray); | |||
} | |||
return WxCpInviteResult.fromJson(this.mainService.post(url, jsonObject.toString())); | |||
} | |||
@Override | |||
@@ -0,0 +1,60 @@ | |||
package me.chanjar.weixin.cp.bean; | |||
import com.google.common.base.Splitter; | |||
import com.google.gson.annotations.SerializedName; | |||
import lombok.Data; | |||
import me.chanjar.weixin.common.util.ToStringUtils; | |||
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; | |||
import org.apache.commons.lang3.StringUtils; | |||
import java.io.Serializable; | |||
import java.util.Collections; | |||
import java.util.List; | |||
/** | |||
* 邀请成员的结果对象类. | |||
* Created by Binary Wang on 2018-5-13. | |||
* | |||
* @author <a href="https://github.com/binarywang">Binary Wang</a> | |||
*/ | |||
@Data | |||
public class WxCpInviteResult implements Serializable { | |||
private static final long serialVersionUID = 1420065684270213578L; | |||
@Override | |||
public String toString() { | |||
return ToStringUtils.toSimpleString(this); | |||
} | |||
public static WxCpInviteResult fromJson(String json) { | |||
return WxCpGsonBuilder.INSTANCE.create().fromJson(json, WxCpInviteResult.class); | |||
} | |||
@SerializedName("errcode") | |||
private Integer errCode; | |||
@SerializedName("errmsg") | |||
private String errMsg; | |||
@SerializedName("invaliduser") | |||
private String invalidUsers; | |||
@SerializedName("invalidparty") | |||
private String[] invalidParties; | |||
@SerializedName("invalidtag") | |||
private String[] invalidTags; | |||
public List<String> getInvalidUserList() { | |||
return this.content2List(this.invalidUsers); | |||
} | |||
private List<String> content2List(String content) { | |||
if (StringUtils.isBlank(content)) { | |||
return Collections.emptyList(); | |||
} | |||
return Splitter.on("|").splitToList(content); | |||
} | |||
} |