| | |
| | | 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.ruoyi.common.utils.StringUtils; |
| | | import com.smartor.domain.DingTalkReqVo; |
| | | import com.smartor.service.DingTalkService; |
| | | import com.smartor.service.IntelligenceCenterService; |
| | | import com.taobao.api.ApiException; |
| | | import lombok.extern.log4j.Log4j; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | import java.util.Map; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | /** |
| | | * 智能中心 |
| | | * |
| | |
| | | |
| | | @Override |
| | | public Boolean sendNotification(DingTalkReqVo dingTalkReqVo) { |
| | | String body = null; |
| | | List<String> userIdlist = new ArrayList(); |
| | | Boolean result = false; |
| | | log.info("发送钉钉通知"); |
| | | |
| | | String accessToken = null; |
| | | try { |
| | | accessToken = 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; |
| | | try { |
| | | 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 { |
| | | DingTalkClient client3 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listid"); |
| | | |
| | | OapiUserListidRequest request2 = new OapiUserListidRequest(); |
| | | request2.setDeptId(675909059L); // 设置要获取的部门 ID,这里以根部门为例 |
| | | request2.setDeptId(dingTalkReqVo.getDeptId()); // 设置要获取的部门 ID,这里以根部门为例675909059L |
| | | // 设置部门 ID,如果获取所有员工手机号,可以设置为根部门的 ID |
| | | OapiUserListidResponse response2 = null; |
| | | try { |
| | |
| | | } catch (ApiException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | String body = response2.getBody(); |
| | | body = response2.getBody(); |
| | | // 解析为JSONObject |
| | | JSONObject jsonObject = JSONObject.parseObject(body); |
| | | // 提取出JSONArray |
| | | JSONArray jsonArray = new JSONArray(jsonObject.getJSONObject("result").getJSONArray("userid_list")); |
| | | // 将JSONArray转为List列表 |
| | | String str = JSONObject.toJSONString(jsonArray); |
| | | List<String> list = JSONObject.parseObject(str, List.class); |
| | | userIdlist = JSONObject.parseObject(str, List.class); |
| | | } |
| | | |
| | | |
| | | //userid数组 |
| | | for (String urid : list) { |
| | | 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(1779052814L); |
| | | request.setToAllUser(false); |
| | | |
| | | List<String> contents = dingTalkReqVo.getContents(); |
| | | //发送内容处理 |
| | | List<ConcurrentHashMap<String, String>> contents = dingTalkReqVo.getContents(); |
| | | ArrayList<OapiMessageCorpconversationAsyncsendV2Request.Form> objects = new ArrayList<>(); |
| | | for (int i = 0; i < contents.size(); i++) { |
| | | ConcurrentHashMap<String, String> map = contents.get(i); |
| | | for (Map.Entry<String, String> entry : map.entrySet()) { |
| | | OapiMessageCorpconversationAsyncsendV2Request.Form formSeverityDesc = new OapiMessageCorpconversationAsyncsendV2Request.Form(); |
| | | formSeverityDesc.setKey("问题" + i); |
| | | formSeverityDesc.setValue(contents.get(i).toString()); |
| | | formSeverityDesc.setKey(entry.getKey()); |
| | | formSeverityDesc.setValue(entry.getValue()); |
| | | objects.add(formSeverityDesc); |
| | | } |
| | | } |
| | | |
| | | OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg(); |
| | | msg.setOa(new OapiMessageCorpconversationAsyncsendV2Request.OA()); |
| | | msg.getOa().setMessageUrl("https://www.baidu.com"); |
| | | msg.getOa().setPcMessageUrl("https://www.baidu.com"); |
| | | msg.getOa().setMessageUrl(dingTalkReqVo.getUrl()); |
| | | msg.getOa().setPcMessageUrl(dingTalkReqVo.getUrl()); |
| | | msg.getOa().setHead(new OapiMessageCorpconversationAsyncsendV2Request.Head()); |
| | | msg.getOa().getHead().setText("待办事宜"); |
| | | // msg.getOa().getHead().setText("你在说啥呢绒顶替苛夺阿斯蒂芬阿斯蒂芬"); |
| | | msg.getOa().getHead().setBgcolor("00409eff"); |
| | | msg.getOa().setBody(new OapiMessageCorpconversationAsyncsendV2Request.Body()); |
| | | msg.getOa().getBody().setTitle("尊敬的 " + "AAA" + "(项目经理)"); |
| | | msg.getOa().getBody().setContent("待审批:" + "123" + "条"); |
| | | msg.getOa().getBody().setTitle(dingTalkReqVo.getTitle()); |
| | | // msg.getOa().getBody().setContent("待处理:" + "123" + "条"); |
| | | |
| | | msg.getOa().getBody().setForm(objects); |
| | | msg.setMsgtype("oa"); |