|  |  | 
 |  |  |  | 
 |  |  | import com.alibaba.fastjson2.JSONArray; | 
 |  |  | import com.alibaba.fastjson2.JSONObject; | 
 |  |  | import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; | 
 |  |  | import com.dingtalk.api.DefaultDingTalkClient; | 
 |  |  | import com.dingtalk.api.DingTalkClient; | 
 |  |  | import com.dingtalk.api.request.OapiGettokenRequest; | 
 |  |  | import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request; | 
 |  |  | import com.dingtalk.api.request.OapiUserListidRequest; | 
 |  |  | import com.dingtalk.api.request.OapiV2UserGetbymobileRequest; | 
 |  |  | import com.dingtalk.api.response.OapiGettokenResponse; | 
 |  |  | import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response; | 
 |  |  | import com.dingtalk.api.response.OapiUserListidResponse; | 
 |  |  | import com.dingtalk.api.response.OapiV2UserGetbymobileResponse; | 
 |  |  | import com.dingtalk.api.request.*; | 
 |  |  | import com.dingtalk.api.response.*; | 
 |  |  | import com.fasterxml.jackson.databind.ObjectMapper; | 
 |  |  | import com.ruoyi.common.core.domain.entity.SysUser; | 
 |  |  | import com.ruoyi.common.exception.base.BaseException; | 
 |  |  | import com.ruoyi.common.utils.StringUtils; | 
 |  |  | import com.ruoyi.common.utils.http.HttpUtils; | 
 |  |  | import com.ruoyi.project.domain.vo.DingDetpInfoVo; | 
 |  |  | import com.ruoyi.project.domain.vo.DingTalkReqVo; | 
 |  |  | import com.ruoyi.project.domain.vo.DingUserInfoVo; | 
 |  |  | import com.ruoyi.project.service.DingTalkService; | 
 |  |  | import com.ruoyi.system.mapper.SysUserMapper; | 
 |  |  | import com.taobao.api.ApiException; | 
 |  |  | import lombok.Data; | 
 |  |  | import lombok.extern.log4j.Log4j; | 
 |  |  | import org.apache.commons.collections4.CollectionUtils; | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | import org.springframework.beans.factory.annotation.Value; | 
 |  |  | import org.springframework.stereotype.Service; | 
 |  |  |  | 
 |  |  | 
 |  |  |  | 
 |  |  |     @Value("${agentId}") | 
 |  |  |     private Long agentId; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private SysUserMapper sysUserMapper; | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public Boolean sendNotification(DingTalkReqVo dingTalkReqVo) { | 
 |  |  | 
 |  |  |         return result; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 免登陆接口 | 
 |  |  |      * | 
 |  |  |      * @param authCode | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     public Map<String, Object> noLogin(String authCode) { | 
 |  |  |         try { | 
 |  |  |             String urlString = "https://oapi.dingtalk.com/topapi/v2/user/getuserinfo"; | 
 |  |  |             String params = "access_token=" + getAccessToken() + "&code=" + authCode; | 
 |  |  |             String result = HttpUtils.sendPost(urlString, params); | 
 |  |  |             ObjectMapper objectMapper = new ObjectMapper(); | 
 |  |  |             Map<String, Object> map = objectMapper.readValue(result, Map.class); | 
 |  |  |             return map; | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             log.error(e.getMessage()); | 
 |  |  |             new BaseException("登录出异常了,请联系管理员处理"); | 
 |  |  |         } | 
 |  |  |         return null; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public void deptidList(Long deptID, Integer begin) { | 
 |  |  |         try { | 
 |  |  |             //根据传过来的部门ID,去获取该部门下的用户信息 | 
 |  |  |             if (begin != 1) DingUserInfoList(deptID); | 
 |  |  |             //获取子部门的dept_id | 
 |  |  |             DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub"); | 
 |  |  |             OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest(); | 
 |  |  |             req.setDeptId(deptID); | 
 |  |  |             req.setLanguage("zh_CN"); | 
 |  |  |             OapiV2DepartmentListsubResponse rsp = client.execute(req, getAccessToken()); | 
 |  |  |             ObjectMapper objectMapper = new ObjectMapper(); | 
 |  |  |             Map<String, Object> map = objectMapper.readValue(rsp.getBody(), Map.class); | 
 |  |  |             if (ObjectUtils.isNotEmpty(map)) { | 
 |  |  |                 List<Map<String, Object>> dingDetpInfoList = (List<Map<String, Object>>) map.get("result"); | 
 |  |  |                 if (Integer.valueOf(map.get("errcode").toString()) == 0) { | 
 |  |  |                     for (int i = 0; i < dingDetpInfoList.size(); i++) { | 
 |  |  |                         Map<String, Object> map1 = dingDetpInfoList.get(i); | 
 |  |  |                         deptidList(Long.valueOf(map1.get("dept_id").toString()), 2); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             log.error(e.getMessage()); | 
 |  |  |             new BaseException("登录出异常了,请联系管理员处理"); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public void DingUserInfoList(Long deptId) { | 
 |  |  |         DingTalkClient client = null; | 
 |  |  |         try { | 
 |  |  |             client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/list?access_token=" + getAccessToken()); | 
 |  |  |             OapiV2UserListRequest req = new OapiV2UserListRequest(); | 
 |  |  |             req.setDeptId(deptId); | 
 |  |  |             req.setCursor(0L); | 
 |  |  |             req.setSize(100L); | 
 |  |  |             req.setLanguage("zh_CN"); | 
 |  |  |             OapiV2UserListResponse rsp = client.execute(req, ""); | 
 |  |  |             //获取resp里的用户信息集合 | 
 |  |  |             ObjectMapper objectMapper = new ObjectMapper(); | 
 |  |  |             Map<String, Object> map = objectMapper.readValue(rsp.getBody(), Map.class); | 
 |  |  |             Map<String, Object> mapResult = (Map<String, Object>) map.get("result"); | 
 |  |  |             List<Map<String, Object>> userInfoMapList = (List<Map<String, Object>>) mapResult.get("list"); | 
 |  |  |             if (!CollectionUtils.isEmpty(userInfoMapList)) { | 
 |  |  |                 //根据手机号,将userID写进对应的字段上 | 
 |  |  |                 for (Map<String, Object> objectMap : userInfoMapList) { | 
 |  |  |                     SysUser sysUser = new SysUser(); | 
 |  |  |                     sysUser.setPhonenumber(objectMap.get("mobile").toString()); | 
 |  |  |                     sysUser.setDingUserId(objectMap.get("userid").toString()); | 
 |  |  |                     sysUserMapper.updateUser(sysUser); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             e.printStackTrace(); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 获取 access_token | 
 |  |  | 
 |  |  |         OapiGettokenResponse response = client.execute(request); | 
 |  |  |         return response.getAccessToken(); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | } |