| | |
| | | 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.*; |
| | | 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.project.utils.DingTalkProxyClient; |
| | | import com.ruoyi.system.mapper.SysUserMapper; |
| | | import com.taobao.api.ApiException; |
| | | import lombok.extern.log4j.Log4j; |
| | |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | |
| | | @Autowired |
| | | private SysUserMapper sysUserMapper; |
| | | |
| | | @Autowired |
| | | private DingTalkProxyClient dingTalkProxyClient; |
| | | |
| | | @Override |
| | | public Boolean sendNotification(DingTalkReqVo dingTalkReqVo) { |
| | | String body = null; |
| | |
| | | |
| | | String accessToken = null; |
| | | try { |
| | | accessToken = getAccessToken(); |
| | | accessToken = dingTalkProxyClient.getAccessToken(); |
| | | } catch (ApiException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | if (StringUtils.isNotEmpty(dingTalkReqVo.getNumber())) { |
| | | DingTalkClient clientT = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getbymobile"); |
| | | OapiV2UserGetbymobileRequest req = new OapiV2UserGetbymobileRequest(); |
| | | req.setMobile(dingTalkReqVo.getNumber()); |
| | | OapiV2UserGetbymobileResponse rsp = null; |
| | | // 使用代理客户端调用获取用户ID接口 |
| | | Map<String, String> params = new HashMap<>(); |
| | | params.put("access_token", accessToken); |
| | | params.put("mobile", dingTalkReqVo.getNumber()); |
| | | try { |
| | | rsp = clientT.execute(req, accessToken); |
| | | body = rsp.getBody(); |
| | | body = dingTalkProxyClient.executeGet("/topapi/v2/user/getbymobile", params); |
| | | String jsonObject = JSONObject.parseObject(body).getJSONObject("result").get("userid").toString(); |
| | | userIdlist.add(jsonObject); |
| | | |
| | | } catch (ApiException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } else { |
| | | DingTalkClient client3 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listid"); |
| | | |
| | | OapiUserListidRequest request2 = new OapiUserListidRequest(); |
| | | request2.setDeptId(dingTalkReqVo.getDeptId()); // 设置要获取的部门 ID,这里以根部门为例675909059L |
| | | // 设置部门 ID,如果获取所有员工手机号,可以设置为根部门的 ID |
| | | OapiUserListidResponse response2 = null; |
| | | // 使用代理客户端调用获取部门用户ID列表接口 |
| | | Map<String, String> params = new HashMap<>(); |
| | | params.put("access_token", accessToken); |
| | | params.put("dept_id", String.valueOf(dingTalkReqVo.getDeptId())); |
| | | try { |
| | | response2 = client3.execute(request2, accessToken); |
| | | body = dingTalkProxyClient.executeGet("/topapi/user/listid", params); |
| | | } catch (ApiException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | body = response2.getBody(); |
| | | // 解析为JSONObject |
| | | JSONObject jsonObject = JSONObject.parseObject(body); |
| | | if (jsonObject != null) { |
| | |
| | | |
| | | //userid数组 |
| | | for (String urid : userIdlist) { |
| | | DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2"); |
| | | OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request(); |
| | | request.setUseridList(urid); |
| | | request.setAgentId(agentId); |
| | | request.setToAllUser(false); |
| | | // 构建发送消息的完整JSON对象,符合钉钉API格式 |
| | | Map<String, Object> messageData = new HashMap<>(); |
| | | messageData.put("userid_list", urid); |
| | | messageData.put("agent_id", agentId); |
| | | messageData.put("to_all_user", false); |
| | | |
| | | //发送内容处理 |
| | | List<ConcurrentHashMap<String, Object>> contents = dingTalkReqVo.getContents(); |
| | | ArrayList<OapiMessageCorpconversationAsyncsendV2Request.Form> objects = new ArrayList<>(); |
| | | List<Map<String, Object>> objects = new ArrayList<>(); |
| | | for (int i = 0; i < contents.size(); i++) { |
| | | ConcurrentHashMap<String, Object> map = contents.get(i); |
| | | for (Map.Entry<String, Object> entry : map.entrySet()) { |
| | | OapiMessageCorpconversationAsyncsendV2Request.Form formSeverityDesc = new OapiMessageCorpconversationAsyncsendV2Request.Form(); |
| | | formSeverityDesc.setKey(entry.getKey()); |
| | | Map<String, Object> formItem = new HashMap<>(); |
| | | formItem.put("key", entry.getKey()); |
| | | if (entry.getKey().contains("审批时间")) { |
| | | Date date = (Date) entry.getValue(); |
| | | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); |
| | | String format = simpleDateFormat.format(date); |
| | | formSeverityDesc.setValue(format); |
| | | formItem.put("value", format); |
| | | } else { |
| | | if (entry.getValue() != null) { |
| | | formSeverityDesc.setValue(entry.getValue().toString()); |
| | | formItem.put("value", entry.getValue().toString()); |
| | | } else { |
| | | formItem.put("value", ""); |
| | | } |
| | | } |
| | | objects.add(formSeverityDesc); |
| | | objects.add(formItem); |
| | | } |
| | | } |
| | | |
| | | OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg(); |
| | | msg.setOa(new OapiMessageCorpconversationAsyncsendV2Request.OA()); |
| | | msg.getOa().setMessageUrl(dingTalkReqVo.getUrl()); |
| | | msg.getOa().setPcMessageUrl(dingTalkReqVo.getUrl()); |
| | | msg.getOa().setHead(new OapiMessageCorpconversationAsyncsendV2Request.Head()); |
| | | // msg.getOa().getHead().setText("你在说啥呢绒顶替苛夺阿斯蒂芬阿斯蒂芬"); |
| | | msg.getOa().getHead().setBgcolor("00409eff"); |
| | | msg.getOa().setBody(new OapiMessageCorpconversationAsyncsendV2Request.Body()); |
| | | msg.getOa().getBody().setTitle(dingTalkReqVo.getTitle()); |
| | | // msg.getOa().getBody().setContent("待处理:" + "123" + "条"); |
| | | Map<String, Object> msg = new HashMap<>(); |
| | | Map<String, Object> oa = new HashMap<>(); |
| | | oa.put("message_url", dingTalkReqVo.getUrl()); |
| | | oa.put("pc_message_url", dingTalkReqVo.getUrl()); |
| | | Map<String, Object> head = new HashMap<>(); |
| | | head.put("bgcolor", "00409eff"); |
| | | oa.put("head", head); |
| | | Map<String, Object> bodyContent = new HashMap<>(); |
| | | bodyContent.put("title", dingTalkReqVo.getTitle()); |
| | | bodyContent.put("form", objects); |
| | | oa.put("body", bodyContent); |
| | | msg.put("oa", oa); |
| | | msg.put("msgtype", "oa"); |
| | | messageData.put("msg", msg); |
| | | |
| | | msg.getOa().getBody().setForm(objects); |
| | | msg.setMsgtype("oa"); |
| | | request.setMsg(msg); |
| | | // log.info("获取发送通知消息体和获取发送通知人完成"); |
| | | OapiMessageCorpconversationAsyncsendV2Response response = null; |
| | | // 将access_token作为URL参数传递 |
| | | String messageJson = JSONObject.toJSONString(messageData); |
| | | try { |
| | | response = client.execute(request, accessToken); |
| | | // 使用代理客户端发送POST请求 |
| | | body = dingTalkProxyClient.executePost("/topapi/message/corpconversation/asyncsend_v2?access_token=" + accessToken, messageJson); |
| | | JSONObject response = JSONObject.parseObject(body); |
| | | result = response.getInteger("errcode") == 0; |
| | | } catch (ApiException e) { |
| | | e.getErrMsg(); |
| | | log.error("发送钉钉消息失败: " + e.getErrMsg()); |
| | | } |
| | | |
| | | // log.info("消息任务ID"+response.getTaskId()); |
| | | System.out.println(response.getTaskId()); |
| | | |
| | | result = response.isSuccess(); |
| | | } |
| | | return result; |
| | | } |
| | |
| | | */ |
| | | 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); |
| | | String params = "access_token=" + dingTalkProxyClient.getAccessToken() + "&code=" + authCode; |
| | | String result = HttpUtils.sendPost("https://oapi.dingtalk.com/topapi/v2/user/getuserinfo", params); |
| | | ObjectMapper objectMapper = new ObjectMapper(); |
| | | Map<String, Object> map = objectMapper.readValue(result, Map.class); |
| | | return map; |
| | |
| | | //根据传过来的部门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()); |
| | | Map<String, String> params = new HashMap<>(); |
| | | params.put("access_token", dingTalkProxyClient.getAccessToken()); |
| | | params.put("dept_id", deptID.toString()); |
| | | params.put("language", "zh_CN"); |
| | | String rspBody = dingTalkProxyClient.executeGet("/topapi/v2/department/listsub", params); |
| | | ObjectMapper objectMapper = new ObjectMapper(); |
| | | Map<String, Object> map = objectMapper.readValue(rsp.getBody(), Map.class); |
| | | Map<String, Object> map = objectMapper.readValue(rspBody, 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) { |
| | |
| | | } |
| | | |
| | | 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, ""); |
| | | Map<String, String> params = new HashMap<>(); |
| | | params.put("access_token", dingTalkProxyClient.getAccessToken()); |
| | | params.put("dept_id", deptId.toString()); |
| | | params.put("cursor", "0"); |
| | | params.put("size", "100"); |
| | | params.put("language", "zh_CN"); |
| | | String rspBody = dingTalkProxyClient.executeGet("/topapi/v2/user/list", params); |
| | | //获取resp里的用户信息集合 |
| | | ObjectMapper objectMapper = new ObjectMapper(); |
| | | Map<String, Object> map = objectMapper.readValue(rsp.getBody(), Map.class); |
| | | Map<String, Object> map = objectMapper.readValue(rspBody, 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)) { |
| | |
| | | * 获取 access_token |
| | | */ |
| | | private String getAccessToken() throws ApiException { |
| | | DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken"); |
| | | OapiGettokenRequest request = new OapiGettokenRequest(); |
| | | //Appkey |
| | | request.setAppkey(dingAppid); |
| | | //Appsecret |
| | | request.setAppsecret(dingAppSecret); |
| | | /*请求方式*/ |
| | | request.setHttpMethod("GET"); |
| | | OapiGettokenResponse response = client.execute(request); |
| | | return response.getAccessToken(); |
| | | return dingTalkProxyClient.getAccessToken(); |
| | | } |
| | | |
| | | } |