Skip to content

【企业微信】 家校沟通-增加批量更新家长接口支持 #2742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* OAuth2相关管理接口.
* Created by BinaryWang on 2017/6/24.
* </pre>
* <p>
* 文档1:https://developer.work.weixin.qq.com/document/path/91856
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
Expand Down Expand Up @@ -84,6 +86,19 @@ public interface WxCpOAuth2Service {
*/
WxCpOauth2UserInfo getUserInfo(Integer agentId, String code) throws WxErrorException;

/**
* 获取家校访问用户身份
* 该接口用于根据code获取家长或者学生信息
* <p>
* 请求方式:GET(HTTPS)
* 请求地址:https://qyapi.weixin.qq.com/cgi-bin/school/getuserinfo?access_token=ACCESS_TOKEN&code=CODE
*
* @param code
* @return
* @throws WxErrorException
*/
WxCpOauth2UserInfo getSchoolUserInfo(String code) throws WxErrorException;

/**
* <pre>
* 使用user_ticket获取成员详情.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.NonNull;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.bean.WxCpBaseResp;
import me.chanjar.weixin.cp.bean.WxCpOauth2UserInfo;
import me.chanjar.weixin.cp.bean.school.user.*;

import java.util.List;
Expand All @@ -16,6 +17,32 @@
*/
public interface WxCpSchoolUserService {

/**
* 获取访问用户身份
* 该接口用于根据code获取成员信息
* <p>
* 请求方式:GET(HTTPS)
* 请求地址:https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=ACCESS_TOKEN&code=CODE
*
* @param code
* @return
* @throws WxErrorException
*/
WxCpOauth2UserInfo getUserInfo(@NonNull String code) throws WxErrorException;

/**
* 获取家校访问用户身份
* 该接口用于根据code获取家长或者学生信息
* <p>
* 请求方式:GET(HTTPS)
* 请求地址:https://qyapi.weixin.qq.com/cgi-bin/school/getuserinfo?access_token=ACCESS_TOKEN&code=CODE
*
* @param code
* @return
* @throws WxErrorException
*/
WxCpOauth2UserInfo getSchoolUserInfo(@NonNull String code) throws WxErrorException;

/**
* 创建学生
* 请求方式:POST(HTTPS)
Expand Down Expand Up @@ -98,6 +125,39 @@ public interface WxCpSchoolUserService {
*/
WxCpBaseResp createParent(@NonNull WxCpCreateParentRequest request) throws WxErrorException;

/**
* 批量创建家长
* 请求方式:POST(HTTPS)
* 请求地址:https://qyapi.weixin.qq.com/cgi-bin/school/user/batch_create_parent?access_token=ACCESS_TOKEN
*
* @param request
* @return
* @throws WxErrorException
*/
WxCpBatchResultList batchCreateParent(@NonNull WxCpBatchCreateParentRequest request) throws WxErrorException;

/**
* 批量删除家长
* 请求方式:POST(HTTPS)
* 请求地址:https://qyapi.weixin.qq.com/cgi-bin/school/user/batch_delete_parent?access_token=ACCESS_TOKEN
*
* @param userIdList
* @return
* @throws WxErrorException
*/
WxCpBatchResultList batchDeleteParent(@NonNull String... userIdList) throws WxErrorException;

/**
* 批量更新家长
* 请求方式:POST(HTTPS)
* 请求地址:https://qyapi.weixin.qq.com/cgi-bin/school/user/batch_update_parent?access_token=ACCESS_TOKEN
*
* @param request
* @return
* @throws WxErrorException
*/
WxCpBatchResultList batchUpdateParent(@NonNull WxCpBatchUpdateParentRequest request) throws WxErrorException;

/**
* 更新家长
* 请求方式:POST(HTTPS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ public WxCpOauth2UserInfo getUserInfo(Integer agentId, String code) throws WxErr
.userTicket(GsonHelper.getString(jo, "user_ticket"))
.expiresIn(GsonHelper.getString(jo, "expires_in"))
.externalUserId(GsonHelper.getString(jo, "external_userid"))
.parentUserId(GsonHelper.getString(jo, "parent_userid"))
.studentUserId(GsonHelper.getString(jo, "student_userid"))
.build();
}

@Override
public WxCpOauth2UserInfo getSchoolUserInfo(String code) throws WxErrorException {
String responseText = this.mainService.get(String.format(this.mainService.getWxCpConfigStorage().getApiUrl(GET_SCHOOL_USER_INFO), code), null);
JsonObject jo = GsonParser.parse(responseText);

return WxCpOauth2UserInfo.builder()
.deviceId(GsonHelper.getString(jo, "DeviceId"))
.parentUserId(GsonHelper.getString(jo, "parent_userid"))
.studentUserId(GsonHelper.getString(jo, "student_userid"))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import me.chanjar.weixin.cp.api.WxCpSchoolUserService;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpBaseResp;
import me.chanjar.weixin.cp.bean.WxCpOauth2UserInfo;
import me.chanjar.weixin.cp.bean.school.user.*;
import org.apache.commons.lang3.StringUtils;

Expand All @@ -32,6 +33,16 @@ public class WxCpSchoolUserServiceImpl implements WxCpSchoolUserService {

private final WxCpService cpService;

@Override
public WxCpOauth2UserInfo getUserInfo(@NonNull String code) throws WxErrorException {
return cpService.getOauth2Service().getUserInfo(code);
}

@Override
public WxCpOauth2UserInfo getSchoolUserInfo(@NonNull String code) throws WxErrorException {
return cpService.getOauth2Service().getSchoolUserInfo(code);
}

@Override
public WxCpBaseResp createStudent(@NonNull String studentUserId, @NonNull String name, @NonNull List<Integer> departments) throws WxErrorException {
String apiUrl = this.cpService.getWxCpConfigStorage().getApiUrl(CREATE_STUDENT);
Expand Down Expand Up @@ -104,6 +115,33 @@ public WxCpBaseResp createParent(@NonNull WxCpCreateParentRequest request) throw
return WxCpBaseResp.fromJson(responseContent);
}

@Override
public WxCpBatchResultList batchCreateParent(@NonNull WxCpBatchCreateParentRequest request) throws WxErrorException {
String apiUrl = this.cpService.getWxCpConfigStorage().getApiUrl(BATCH_CREATE_PARENT);
String responseContent = this.cpService.post(apiUrl, request.toJson());
return WxCpBatchResultList.fromJson(responseContent);
}

@Override
public WxCpBatchResultList batchDeleteParent(@NonNull String... userIdList) throws WxErrorException {
String apiUrl = this.cpService.getWxCpConfigStorage().getApiUrl(BATCH_DELETE_PARENT);
JsonObject jsonObject = new JsonObject();
JsonArray jsonArray = new JsonArray();
for (String userId : userIdList) {
jsonArray.add(new JsonPrimitive(userId));
}
jsonObject.add("useridlist", jsonArray);
String responseContent = this.cpService.post(apiUrl, jsonObject.toString());
return WxCpBatchResultList.fromJson(responseContent);
}

@Override
public WxCpBatchResultList batchUpdateParent(@NonNull WxCpBatchUpdateParentRequest request) throws WxErrorException {
String apiUrl = this.cpService.getWxCpConfigStorage().getApiUrl(BATCH_UPDATE_PARENT);
String responseContent = this.cpService.post(apiUrl, request.toJson());
return WxCpBatchResultList.fromJson(responseContent);
}

@Override
public WxCpBaseResp updateParent(@NonNull WxCpUpdateParentRequest request) throws WxErrorException {
String apiUrl = this.cpService.getWxCpConfigStorage().getApiUrl(UPDATE_PARENT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* Created by BinaryWang on 2019/5/26.
* </pre>
*
* 文档1:https://developer.work.weixin.qq.com/document/path/91707
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
@Data
Expand All @@ -30,4 +32,7 @@ public class WxCpOauth2UserInfo implements Serializable {
private String userTicket;
private String expiresIn;
private String externalUserId;
private String parentUserId;
private String studentUserId;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package me.chanjar.weixin.cp.bean.school.user;

import com.google.gson.annotations.SerializedName;
import lombok.*;
import lombok.experimental.Accessors;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;

import java.io.Serializable;
import java.util.List;

/**
* 批量创建家长.
*
* @author Wang_Wong
* @date 2022-07-11
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class WxCpBatchCreateParentRequest implements Serializable {
private static final long serialVersionUID = -4960239393895754138L;

@SerializedName("parents")
private List<Parent> parents;

@Setter
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class Parent implements Serializable {

@SerializedName("parent_userid")
private String parentUserId;

@SerializedName("mobile")
private String mobile;

@SerializedName("to_invite")
private Boolean toInvite;

@SerializedName("children")
private List<Children> children;

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

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

}

@Setter
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class Children implements Serializable {

@SerializedName("student_userid")
private String studentUserId;

@SerializedName("relation")
private String relation;

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

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

}

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

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package me.chanjar.weixin.cp.bean.school.user;

import com.google.gson.annotations.SerializedName;
import lombok.*;
import lombok.experimental.Accessors;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;

import java.io.Serializable;
import java.util.List;

/**
* 批量更新家长.
*
* @author Wang_Wong
* @date 2022-07-11
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class WxCpBatchUpdateParentRequest implements Serializable {
private static final long serialVersionUID = -4960239393895754138L;

@SerializedName("parents")
private List<Parent> parents;

@Setter
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class Parent implements Serializable {

@SerializedName("parent_userid")
private String parentUserId;

@SerializedName("new_parent_userid")
private String newParentUserId;

@SerializedName("mobile")
private String mobile;

@SerializedName("children")
private List<Children> children;

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

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

}

@Setter
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class Children implements Serializable {

@SerializedName("student_userid")
private String studentUserId;

@SerializedName("relation")
private String relation;

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

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

}

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

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public class WxCpCreateParentRequest implements Serializable {

@Setter
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class Children implements Serializable {

@SerializedName("student_userid")
Expand Down
Loading