| | |
| | | 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.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.ruoyi.common.core.domain.entity.SysUser; |
| | | import com.ruoyi.common.exception.base.BaseException; |
| | |
| | | |
| | | String accessToken = null; |
| | | try { |
| | | accessToken = dingTalkProxyClient.getAccessToken(); |
| | | accessToken = getAccessToken(); |
| | | } catch (ApiException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | if (StringUtils.isNotEmpty(dingTalkReqVo.getNumber())) { |
| | | // 使用代理客户端调用获取用户ID接口 |
| | | Map<String, String> params = new HashMap<>(); |
| | | params.put("access_token", accessToken); |
| | | params.put("mobile", 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; |
| | | try { |
| | | body = dingTalkProxyClient.executeGet("/topapi/v2/user/getbymobile", params); |
| | | rsp = clientT.execute(req, accessToken); |
| | | body = rsp.getBody(); |
| | | String jsonObject = JSONObject.parseObject(body).getJSONObject("result").get("userid").toString(); |
| | | userIdlist.add(jsonObject); |
| | | |
| | | } catch (ApiException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } else { |
| | | // 使用代理客户端调用获取部门用户ID列表接口 |
| | | Map<String, String> params = new HashMap<>(); |
| | | params.put("access_token", accessToken); |
| | | params.put("dept_id", String.valueOf(dingTalkReqVo.getDeptId())); |
| | | 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; |
| | | try { |
| | | body = dingTalkProxyClient.executeGet("/topapi/user/listid", params); |
| | | response2 = client3.execute(request2, accessToken); |
| | | } catch (ApiException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | body = response2.getBody(); |
| | | // 解析为JSONObject |
| | | JSONObject jsonObject = JSONObject.parseObject(body); |
| | | if (jsonObject != null) { |
| | |
| | | |
| | | //userid数组 |
| | | for (String urid : userIdlist) { |
| | | // 构建发送消息的完整JSON对象,符合钉钉API格式 |
| | | Map<String, Object> messageData = new HashMap<>(); |
| | | messageData.put("userid_list", urid); |
| | | messageData.put("agent_id", agentId); |
| | | messageData.put("to_all_user", false); |
| | | 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); |
| | | |
| | | //发送内容处理 |
| | | List<ConcurrentHashMap<String, Object>> contents = dingTalkReqVo.getContents(); |
| | | List<Map<String, Object>> objects = new ArrayList<>(); |
| | | ArrayList<OapiMessageCorpconversationAsyncsendV2Request.Form> 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()) { |
| | | Map<String, Object> formItem = new HashMap<>(); |
| | | formItem.put("key", entry.getKey()); |
| | | OapiMessageCorpconversationAsyncsendV2Request.Form formSeverityDesc = new OapiMessageCorpconversationAsyncsendV2Request.Form(); |
| | | formSeverityDesc.setKey(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); |
| | | formItem.put("value", format); |
| | | formSeverityDesc.setValue(format); |
| | | } else { |
| | | if (entry.getValue() != null) { |
| | | formItem.put("value", entry.getValue().toString()); |
| | | } else { |
| | | formItem.put("value", ""); |
| | | formSeverityDesc.setValue(entry.getValue().toString()); |
| | | } |
| | | } |
| | | objects.add(formItem); |
| | | objects.add(formSeverityDesc); |
| | | } |
| | | } |
| | | |
| | | 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); |
| | | 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" + "条"); |
| | | |
| | | // 将access_token作为URL参数传递 |
| | | String messageJson = JSONObject.toJSONString(messageData); |
| | | msg.getOa().getBody().setForm(objects); |
| | | msg.setMsgtype("oa"); |
| | | request.setMsg(msg); |
| | | // log.info("获取发送通知消息体和获取发送通知人完成"); |
| | | OapiMessageCorpconversationAsyncsendV2Response response = null; |
| | | try { |
| | | // 使用代理客户端发送POST请求 |
| | | body = dingTalkProxyClient.executePost("/topapi/message/corpconversation/asyncsend_v2?access_token=" + accessToken, messageJson); |
| | | JSONObject response = JSONObject.parseObject(body); |
| | | result = response.getInteger("errcode") == 0; |
| | | response = client.execute(request, accessToken); |
| | | } catch (ApiException e) { |
| | | log.error("发送钉钉消息失败: " + e.getErrMsg()); |
| | | e.getErrMsg(); |
| | | } |
| | | |
| | | // log.info("消息任务ID"+response.getTaskId()); |
| | | System.out.println(response.getTaskId()); |
| | | |
| | | result = response.isSuccess(); |
| | | } |
| | | return result; |
| | | } |
| | |
| | | * 获取 access_token |
| | | */ |
| | | private String getAccessToken() throws ApiException { |
| | | return dingTalkProxyClient.getAccessToken(); |
| | | 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(); |
| | | } |
| | | |
| | | } |