package doumeemes.core.utils.dingding; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.dingtalk.api.DefaultDingTalkClient; import com.dingtalk.api.DingTalkClient; import com.dingtalk.api.request.*; import com.dingtalk.api.response.*; import com.google.gson.JsonObject; import com.taobao.api.ApiException; import doumeemes.core.constants.ResponseStatus; import doumeemes.core.exception.BusinessException; import doumeemes.core.utils.Constants; import doumeemes.core.utils.dingding.bean.CompanyUserDTO; import doumeemes.dao.business.model.Company; import doumeemes.dao.business.model.CompanyUser; import doumeemes.dao.business.model.Department; import doumeemes.dao.ext.vo.CompanyUserExtListVO; import doumeemes.service.ext.CompanyExtService; import io.swagger.util.Json; import org.springframework.beans.factory.annotation.Autowired; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Objects; /** * 钉钉业务数据同步类 * * @Author : Rk * @create 2023/6/16 14:02 */ public class DingDingSyncUtil { //文档地址: //https://open.dingtalk.com/document/orgapp/queries-the-complete-information-of-a-department-user /** * 获取钉钉 用户信息 根据用户ID userid(钉钉方) */ public static CompanyUser getDingDingUserInfo(String userId,String accessToken){ try { CompanyUser companyUser = new CompanyUser(); DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get"); OapiV2UserGetRequest req = new OapiV2UserGetRequest(); req.setUserid(userId); req.setLanguage("zh_CN"); OapiV2UserGetResponse rsp = client.execute(req, accessToken); if(rsp.getErrcode()!=Constants.ZERO){ throw new BusinessException(ResponseStatus.ERR_STATUS.getCode(),"同步异常"); } JSONObject jsonObject = JSONObject.parseObject(rsp.getBody()).getJSONObject("result"); companyUser.setDeleted(Constants.ZERO); companyUser.setCreateTime(new Date()); companyUser.setName(jsonObject.getString("name")); companyUser.setPhone(jsonObject.getString("mobile")); companyUser.setPhoneAuthStatus(Integer.toString(Constants.ZERO)); companyUser.setCode(null); companyUser.setPosition(jsonObject.getString("title")); JSONArray deptIdArray = jsonObject.getJSONArray("dept_id_list"); if(!Objects.isNull(deptIdArray)&&deptIdArray.size()>Constants.ZERO){ companyUser.setDepartmentId((Integer)deptIdArray.get(Constants.ZERO)); } companyUser.setStatus(Constants.ZERO); companyUser.setDdUnionid(jsonObject.getString("unionid")); companyUser.setDdUserid(jsonObject.getString("userid")); System.out.println(jsonObject); return companyUser; } catch (ApiException e) { e.printStackTrace(); throw new BusinessException(ResponseStatus.ERR_STATUS.getCode(),"同步异常"); } } /** * 获取钉钉 部门信息 */ public static Department getDingDingDepartInfo(Company company,Long ddDeptId,String accessToken){ try { DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/get"); OapiV2DepartmentGetRequest req = new OapiV2DepartmentGetRequest(); req.setDeptId(ddDeptId); req.setLanguage("zh_CN"); OapiV2DepartmentGetResponse rsp = client.execute(req, accessToken); //处理返回信息 if(rsp.getErrcode()!=Constants.ZERO){ throw new BusinessException(ResponseStatus.ERR_STATUS.getCode(),"同步异常"); } JSONObject jsonObject = JSONObject.parseObject(rsp.getBody()).getJSONObject("result"); Department department = new Department(); department.setCreateTime(new Date()); department.setDeleted(Constants.ZERO); department.setType(Constants.DEPART_TYPE.depart); department.setDdDeptId(jsonObject.getLong("dept_id")); department.setName(jsonObject.getString("name")); department.setParentId(jsonObject.getInteger("parent_id")); department.setStatus(Constants.ZERO); department.setCompanyId(company.getId()); return department; } catch (ApiException e) { e.printStackTrace(); throw new BusinessException(ResponseStatus.ERR_STATUS.getCode(),"同步异常"); } } /** * 获取钉钉 所有部门信息 */ public static List getDingDingDepartListInfo(CompanyUserExtListVO companyUser, Long departId){ try { List departmentList = new ArrayList<>(); DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub"); OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest(); //如果 departId 为空 则同步最高级数据 if(Objects.isNull(departId)){ req.setDeptId(departId); } req.setLanguage("zh_CN"); //TODO 获取access_token信息 OapiV2DepartmentListsubResponse rsp = client.execute(req, "access_token"); //TODO 处理返回信息 if(rsp.getErrcode().equals(Constants.ZERO)){ JSONArray jsonArray = JSONArray.parseArray(rsp.getBody()); for (int i = 0; i < jsonArray.size(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); Department department = new Department(); department.setCreateTime(new Date()); department.setDeleted(Constants.ZERO); department.setDdDeptId(jsonObject.getLong("dept_id")); department.setName(jsonObject.getString("name")); department.setParentId(jsonObject.getInteger("parent_id")); department.setCompanyId(companyUser.getId()); departmentList.add(department); } } System.out.println(rsp.getBody()); return departmentList; } catch (ApiException e) { e.printStackTrace(); throw new BusinessException(ResponseStatus.ERR_STATUS.getCode(),"同步异常"); } } /** * 获取部门信息下 所有用户ID * @param companyId * @param departId */ public static void getDingDingDepartUserIdListInfo(Integer companyId ,Long departId){ try { DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listid"); OapiUserListidRequest req = new OapiUserListidRequest(); req.setDeptId(departId); //TODO 获取access_token信息 OapiUserListidResponse rsp = client.execute(req, "access_token"); //TODO 处理返回信息 if(rsp.getErrcode().equals(Constants.ZERO)){ JSONObject jsonObject = JSONObject.parseObject(rsp.getBody()); } System.out.println(rsp.getBody()); } catch (ApiException e) { e.printStackTrace(); } } /** * 获取部门下 用户信息 * @param department 部门信息 * @param cursor 起始序号 */ public static CompanyUserDTO getDingDingDepartUserListInfo(Company company,Department department,Long cursor){ try { CompanyUserDTO companyUserDTO = new CompanyUserDTO(); DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/list"); OapiV2UserListRequest req = new OapiV2UserListRequest(); req.setDeptId(Long.valueOf(department.getId())); //分页查询的游标,最开始传0,后续传返回参数中的next_cursor值 req.setCursor(cursor); //分页大小 req.setSize(10L); req.setContainAccessLimit(false); req.setLanguage("zh_CN"); //TODO 获取access_token信息 OapiV2UserListResponse rsp = client.execute(req, ""); //TODO 处理返回信息 if(rsp.getErrcode().equals(Constants.ZERO)){ List companyUserList = new ArrayList<>(); JSONObject jsonObject = JSONObject.parseObject(rsp.getBody()); Boolean hasMore = jsonObject.getBoolean("has_more"); companyUserDTO.setHasMore(hasMore); Long nextCursor = jsonObject.getLong("next_cursor"); companyUserDTO.setNextCursor(nextCursor); JSONArray jsonArray = jsonObject.getJSONArray("list"); for (int i = 0; i < jsonArray.size(); i++) { JSONObject jsonObjectChild = jsonArray.getJSONObject(i); CompanyUser companyUser = new CompanyUser(); companyUser.setDeleted(Constants.ZERO); companyUser.setCreateTime(new Date()); companyUser.setName(jsonObjectChild.getString("name")); companyUser.setPhone(jsonObjectChild.getString("mobile")); companyUser.setPosition(jsonObjectChild.getString("title")); companyUser.setPhoneAuthStatus(Integer.toString(Constants.ZERO)); //TODO 员工二维码 companyUser.setCode(null); companyUser.setDepartmentId(department.getId()); companyUser.setStatus(Constants.ZERO); companyUser.setDdUnionid(jsonObjectChild.getString("unionid")); companyUser.setDdUserid(jsonObjectChild.getString("userid")); companyUserList.add(companyUser); } companyUserDTO.setCompanyUserList(companyUserList); } System.out.println(rsp.getBody()); return companyUserDTO; } catch (ApiException e) { e.printStackTrace(); throw new BusinessException(ResponseStatus.ERR_STATUS.getCode(),"同步异常"); } } }