From 497e295b8d660fa399996651e86553e56c0a20ce Mon Sep 17 00:00:00 2001 From: wanggeng <450292408@qq.com> Date: Wed, 11 Aug 2021 23:15:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=92=89=E9=92=89=E9=83=A8?= =?UTF-8?q?=E9=97=A8=E5=88=97=E8=A1=A8=E5=92=8C=E7=94=A8=E6=88=B7=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/api/DemoController.java | 5 - .../pojo/bos/DingDingDepartmentBO.java | 57 ++++++ .../dingding/pojo/bos/DingDingUserBO.java | 185 ++++++++++++++++++ .../IDingDingAppDepartmentService.java | 10 +- .../service/IDingDingAppUserService.java | 20 ++ .../DingDingAppDepartmentServiceImpl.java | 26 ++- .../impl/DingDingAppUserServiceImpl.java | 41 ++++ 7 files changed, 334 insertions(+), 10 deletions(-) create mode 100644 cloud-common-dingding/src/main/java/com/cm/common/dingding/pojo/bos/DingDingDepartmentBO.java create mode 100644 cloud-common-dingding/src/main/java/com/cm/common/dingding/pojo/bos/DingDingUserBO.java diff --git a/cloud-common-dingding/src/main/java/com/cm/common/dingding/controller/api/DemoController.java b/cloud-common-dingding/src/main/java/com/cm/common/dingding/controller/api/DemoController.java index d08c105..836c375 100644 --- a/cloud-common-dingding/src/main/java/com/cm/common/dingding/controller/api/DemoController.java +++ b/cloud-common-dingding/src/main/java/com/cm/common/dingding/controller/api/DemoController.java @@ -38,9 +38,4 @@ public class DemoController { return dingDingMessageService.send(phone, "测试"); } - @GetMapping("list") - public JSONObject list() throws ApiException { - return dingDingAppDepartmentService.list(); - } - } diff --git a/cloud-common-dingding/src/main/java/com/cm/common/dingding/pojo/bos/DingDingDepartmentBO.java b/cloud-common-dingding/src/main/java/com/cm/common/dingding/pojo/bos/DingDingDepartmentBO.java new file mode 100644 index 0000000..da356dd --- /dev/null +++ b/cloud-common-dingding/src/main/java/com/cm/common/dingding/pojo/bos/DingDingDepartmentBO.java @@ -0,0 +1,57 @@ +package com.cm.common.dingding.pojo.bos; + +/** + * @ClassName: DingDingDepartmentBO + * @Description: 钉钉部门 + * @Author: wanggeng + * @Date: 2021/8/4 10:31 下午 + * @Version: 1.0 + */ +public class DingDingDepartmentBO { + + private Long deptId; + private String name; + private String parentId; + private String createDeptGroup; + private String autoAddUser; + + public Long getDeptId() { + return deptId == null ? 0 : deptId; + } + + public void setDeptId(Long deptId) { + this.deptId = deptId; + } + + public String getName() { + return name == null ? "" : name.trim(); + } + + public void setName(String name) { + this.name = name; + } + + public String getParentId() { + return parentId == null ? "" : parentId.trim(); + } + + public void setParentId(String parentId) { + this.parentId = parentId; + } + + public String getCreateDeptGroup() { + return createDeptGroup == null ? "" : createDeptGroup.trim(); + } + + public void setCreateDeptGroup(String createDeptGroup) { + this.createDeptGroup = createDeptGroup; + } + + public String getAutoAddUser() { + return autoAddUser == null ? "" : autoAddUser.trim(); + } + + public void setAutoAddUser(String autoAddUser) { + this.autoAddUser = autoAddUser; + } +} diff --git a/cloud-common-dingding/src/main/java/com/cm/common/dingding/pojo/bos/DingDingUserBO.java b/cloud-common-dingding/src/main/java/com/cm/common/dingding/pojo/bos/DingDingUserBO.java new file mode 100644 index 0000000..59c6033 --- /dev/null +++ b/cloud-common-dingding/src/main/java/com/cm/common/dingding/pojo/bos/DingDingUserBO.java @@ -0,0 +1,185 @@ +package com.cm.common.dingding.pojo.bos; + +import java.util.Date; + +/** + * @ClassName: DingDingUserBO + * @Description: 钉钉用户 + * @Author: wanggeng + * @Date: 2021/8/5 10:27 上午 + * @Version: 1.0 + */ +public class DingDingUserBO { + + private String userid; + private Long order; + private String unionid; + private String mobile; + private String tel; + private String workPlace; + private String remark; + private Boolean isAdmin; + private Boolean isBoss; + private Boolean isHide; + private Boolean isLeader; + private String name; + private Boolean isActive; + private Long[] department; + private String position; + private String email; + private String avatar; + private String jobnumber; + private Date hiredDate; + + public String getUserid() { + return userid == null ? "" : userid.trim(); + } + + public void setUserid(String userid) { + this.userid = userid; + } + + public Long getOrder() { + return order == null ? 0 : order; + } + + public void setOrder(Long order) { + this.order = order; + } + + public String getUnionid() { + return unionid == null ? "" : unionid.trim(); + } + + public void setUnionid(String unionid) { + this.unionid = unionid; + } + + public String getMobile() { + return mobile == null ? "" : mobile.trim(); + } + + public void setMobile(String mobile) { + this.mobile = mobile; + } + + public String getTel() { + return tel == null ? "" : tel.trim(); + } + + public void setTel(String tel) { + this.tel = tel; + } + + public String getWorkPlace() { + return workPlace == null ? "" : workPlace.trim(); + } + + public void setWorkPlace(String workPlace) { + this.workPlace = workPlace; + } + + public String getRemark() { + return remark == null ? "" : remark.trim(); + } + + public void setRemark(String remark) { + this.remark = remark; + } + + public Boolean getAdmin() { + return isAdmin; + } + + public void setAdmin(Boolean admin) { + isAdmin = admin; + } + + public Boolean getBoss() { + return isBoss; + } + + public void setBoss(Boolean boss) { + isBoss = boss; + } + + public Boolean getHide() { + return isHide; + } + + public void setHide(Boolean hide) { + isHide = hide; + } + + public Boolean getLeader() { + return isLeader; + } + + public void setLeader(Boolean leader) { + isLeader = leader; + } + + public String getName() { + return name == null ? "" : name.trim(); + } + + public void setName(String name) { + this.name = name; + } + + public Boolean getActive() { + return isActive; + } + + public void setActive(Boolean active) { + isActive = active; + } + + public Long[] getDepartment() { + return department; + } + + public void setDepartment(Long[] department) { + this.department = department; + } + + public String getPosition() { + return position == null ? "" : position.trim(); + } + + public void setPosition(String position) { + this.position = position; + } + + public String getEmail() { + return email == null ? "" : email.trim(); + } + + public void setEmail(String email) { + this.email = email; + } + + public String getAvatar() { + return avatar == null ? "" : avatar.trim(); + } + + public void setAvatar(String avatar) { + this.avatar = avatar; + } + + public String getJobnumber() { + return jobnumber == null ? "" : jobnumber.trim(); + } + + public void setJobnumber(String jobnumber) { + this.jobnumber = jobnumber; + } + + public Date getHiredDate() { + return hiredDate; + } + + public void setHiredDate(Date hiredDate) { + this.hiredDate = hiredDate; + } +} diff --git a/cloud-common-dingding/src/main/java/com/cm/common/dingding/service/IDingDingAppDepartmentService.java b/cloud-common-dingding/src/main/java/com/cm/common/dingding/service/IDingDingAppDepartmentService.java index c7700a2..ab7dbfe 100644 --- a/cloud-common-dingding/src/main/java/com/cm/common/dingding/service/IDingDingAppDepartmentService.java +++ b/cloud-common-dingding/src/main/java/com/cm/common/dingding/service/IDingDingAppDepartmentService.java @@ -1,8 +1,11 @@ package com.cm.common.dingding.service; import com.alibaba.fastjson.JSONObject; +import com.dingtalk.api.response.OapiV2DepartmentListsubResponse; import com.taobao.api.ApiException; +import java.util.List; + /** * @ClassName: IDingDingAppDepartmentService * @Description: 钉钉部门 @@ -12,6 +15,11 @@ import com.taobao.api.ApiException; */ public interface IDingDingAppDepartmentService { - JSONObject list() throws ApiException; + /** + * 部门全部列表,无层级 + * + * @return + */ + List listAll() throws ApiException; } diff --git a/cloud-common-dingding/src/main/java/com/cm/common/dingding/service/IDingDingAppUserService.java b/cloud-common-dingding/src/main/java/com/cm/common/dingding/service/IDingDingAppUserService.java index b1e6379..3ff9151 100644 --- a/cloud-common-dingding/src/main/java/com/cm/common/dingding/service/IDingDingAppUserService.java +++ b/cloud-common-dingding/src/main/java/com/cm/common/dingding/service/IDingDingAppUserService.java @@ -2,6 +2,10 @@ package com.cm.common.dingding.service; import com.cm.common.exception.SearchException; import com.dingtalk.api.response.OapiUserGetResponse; +import com.dingtalk.api.response.OapiV2UserListResponse; +import com.taobao.api.ApiException; + +import java.util.List; /** * When you feel like quitting. Think about why you started @@ -41,4 +45,20 @@ public interface IDingDingAppUserService { */ String getUserIdByPhone(String phone); + /** + * 部门用户列表 + * + * @param deptId + * @return + */ + List listByDeptId(Long deptId) throws ApiException; + + /** + * 部门用户列表 + * + * @param deptIds + * @return + */ + List listByDeptIds(List deptIds) throws ApiException; + } diff --git a/cloud-common-dingding/src/main/java/com/cm/common/dingding/service/impl/DingDingAppDepartmentServiceImpl.java b/cloud-common-dingding/src/main/java/com/cm/common/dingding/service/impl/DingDingAppDepartmentServiceImpl.java index 4247f81..6b3589b 100644 --- a/cloud-common-dingding/src/main/java/com/cm/common/dingding/service/impl/DingDingAppDepartmentServiceImpl.java +++ b/cloud-common-dingding/src/main/java/com/cm/common/dingding/service/impl/DingDingAppDepartmentServiceImpl.java @@ -7,11 +7,16 @@ import com.cm.common.dingding.manager.app.DingDingAppManager; import com.cm.common.dingding.service.IDingDingAppDepartmentService; import com.dingtalk.api.DefaultDingTalkClient; import com.dingtalk.api.DingTalkClient; +import com.dingtalk.api.request.OapiUserSimplelistRequest; import com.dingtalk.api.request.OapiV2DepartmentListsubRequest; +import com.dingtalk.api.response.OapiUserSimplelistResponse; import com.dingtalk.api.response.OapiV2DepartmentListsubResponse; import com.taobao.api.ApiException; import org.springframework.stereotype.Service; +import java.util.ArrayList; +import java.util.List; + /** * @ClassName: DingDingAppDepartmentServiceImpl * @Description: 钉钉部门管理,需要开放钉钉应用:权限管理 -> 通讯录管理 -> 通讯录部门信息读权限 @@ -21,14 +26,27 @@ import org.springframework.stereotype.Service; */ @Service public class DingDingAppDepartmentServiceImpl extends AbstractService implements IDingDingAppDepartmentService { + @Override - public JSONObject list() throws ApiException { + public List listAll() throws ApiException { + List deptBaseResponses = new ArrayList<>(); + buildDeptList(1L, deptBaseResponses); + return deptBaseResponses; + } + + private void buildDeptList(Long parentDeptId, List deptBaseResponses) throws ApiException { DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub"); OapiV2DepartmentListsubRequest oapiV2DepartmentListsubRequest = new OapiV2DepartmentListsubRequest(); - oapiV2DepartmentListsubRequest.setDeptId(1L); + oapiV2DepartmentListsubRequest.setDeptId(parentDeptId); oapiV2DepartmentListsubRequest.setLanguage("zh_CN"); OapiV2DepartmentListsubResponse rsp = client.execute(oapiV2DepartmentListsubRequest, DingDingAppManager.getInstance().getAccessToken()); - System.out.println(rsp.getBody()); - return JSON.parseObject(rsp.getBody()); + List subDepts = rsp.getResult(); + if (!subDepts.isEmpty()) { + for (OapiV2DepartmentListsubResponse.DeptBaseResponse subDept : subDepts) { + deptBaseResponses.add(subDept); + buildDeptList(subDept.getDeptId(), deptBaseResponses); + } + } } + } diff --git a/cloud-common-dingding/src/main/java/com/cm/common/dingding/service/impl/DingDingAppUserServiceImpl.java b/cloud-common-dingding/src/main/java/com/cm/common/dingding/service/impl/DingDingAppUserServiceImpl.java index 77c8a35..b526d2c 100644 --- a/cloud-common-dingding/src/main/java/com/cm/common/dingding/service/impl/DingDingAppUserServiceImpl.java +++ b/cloud-common-dingding/src/main/java/com/cm/common/dingding/service/impl/DingDingAppUserServiceImpl.java @@ -10,13 +10,18 @@ import com.dingtalk.api.DingTalkClient; import com.dingtalk.api.request.OapiUserGetByMobileRequest; import com.dingtalk.api.request.OapiUserGetRequest; import com.dingtalk.api.request.OapiUserGetUseridByUnionidRequest; +import com.dingtalk.api.request.OapiV2UserListRequest; import com.dingtalk.api.response.OapiUserGetByMobileResponse; import com.dingtalk.api.response.OapiUserGetResponse; import com.dingtalk.api.response.OapiUserGetUseridByUnionidResponse; +import com.dingtalk.api.response.OapiV2UserListResponse; import com.taobao.api.ApiException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.ArrayList; +import java.util.List; + /** * When you feel like quitting. Think about why you started * 当你想要放弃的时候,想想当初你为何开始 @@ -86,4 +91,40 @@ public class DingDingAppUserServiceImpl extends AbstractService implements IDing } } + @Override + public List listByDeptId(Long deptId) throws ApiException { + List userResponses = new ArrayList<>(); + buildUserList(deptId, 0L, userResponses); + return userResponses; + } + + private void buildUserList(Long deptId, Long cursor, List userResponses) throws ApiException { + DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/list"); + OapiV2UserListRequest req = new OapiV2UserListRequest(); + req.setDeptId(deptId); + req.setCursor(cursor); + req.setSize(1000L); + req.setContainAccessLimit(true); + req.setLanguage("zh_CN"); + OapiV2UserListResponse oapiV2UserListResponse = client.execute(req, DingDingAppManager.getInstance().getAccessToken()); + OapiV2UserListResponse.PageResult result = oapiV2UserListResponse.getResult(); + List listUserResponses = result.getList(); + if (result.getHasMore()) { + buildUserList(deptId, result.getNextCursor(), userResponses); + } + userResponses.addAll(listUserResponses); + } + + @Override + public List listByDeptIds(List deptIds) throws ApiException { + if (deptIds.isEmpty()) { + return new ArrayList<>(); + } + List userResponses = new ArrayList<>(); + for (Long deptId : deptIds) { + userResponses.addAll(listByDeptId(deptId)); + } + return userResponses; + } + }