From 517ecf9879052b2f3fbad25c0469935cc45553a7 Mon Sep 17 00:00:00 2001
From: liusheng <337615773@qq.com>
Date: 星期四, 25 七月 2024 18:24:24 +0800
Subject: [PATCH] 变更

---
 ruoyi-project/src/main/java/com/ruoyi/project/service/impl/DingTalkServiceImpl.java |  142 +++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 123 insertions(+), 19 deletions(-)

diff --git a/ruoyi-project/src/main/java/com/ruoyi/project/service/impl/DingTalkServiceImpl.java b/ruoyi-project/src/main/java/com/ruoyi/project/service/impl/DingTalkServiceImpl.java
index 51d48ef..99fb158 100644
--- a/ruoyi-project/src/main/java/com/ruoyi/project/service/impl/DingTalkServiceImpl.java
+++ b/ruoyi-project/src/main/java/com/ruoyi/project/service/impl/DingTalkServiceImpl.java
@@ -2,25 +2,31 @@
 
 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.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;
 
+import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
@@ -41,6 +47,12 @@
 
     @Value("${dingAppSecret}")
     private String dingAppSecret;
+
+    @Value("${agentId}")
+    private Long agentId;
+
+    @Autowired
+    private SysUserMapper sysUserMapper;
 
     @Override
     public Boolean sendNotification(DingTalkReqVo dingTalkReqVo) {
@@ -84,11 +96,13 @@
             body = response2.getBody();
             // 瑙f瀽涓篔SONObject
             JSONObject jsonObject = JSONObject.parseObject(body);
-            // 鎻愬彇鍑篔SONArray
-            JSONArray jsonArray = new JSONArray(jsonObject.getJSONObject("result").getJSONArray("userid_list"));
-            // 灏咼SONArray杞负List鍒楄〃
-            String str = JSONObject.toJSONString(jsonArray);
-            userIdlist = JSONObject.parseObject(str, List.class);
+            if (jsonObject != null) {
+                // 鎻愬彇鍑篔SONArray
+                JSONArray jsonArray = new JSONArray(jsonObject.getJSONObject("result").getJSONArray("userid_list"));
+                // 灏咼SONArray杞负List鍒楄〃
+                String str = JSONObject.toJSONString(jsonArray);
+                userIdlist = JSONObject.parseObject(str, List.class);
+            }
         }
 
 
@@ -97,18 +111,27 @@
             DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
             OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
             request.setUseridList(urid);
-            request.setAgentId(1779052814L);
+            request.setAgentId(agentId);
             request.setToAllUser(false);
 
             //鍙戦�佸唴瀹瑰鐞�
-            List<ConcurrentHashMap<String, String>> contents = dingTalkReqVo.getContents();
+            List<ConcurrentHashMap<String, Object>> 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()) {
+                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());
-                    formSeverityDesc.setValue(entry.getValue());
+                    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);
+                    } else {
+                        if (entry.getValue() != null) {
+                            formSeverityDesc.setValue(entry.getValue().toString());
+                        }
+                    }
                     objects.add(formSeverityDesc);
                 }
             }
@@ -132,7 +155,7 @@
             try {
                 response = client.execute(request, accessToken);
             } catch (ApiException e) {
-                e.printStackTrace();
+                e.getErrMsg();
             }
 
             //  log.info("娑堟伅浠诲姟ID"+response.getTaskId());
@@ -141,6 +164,86 @@
             result = response.isSuccess();
         }
         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)) {
+                //鏍规嵁鎵嬫満鍙凤紝灏唘serID鍐欒繘瀵瑰簲鐨勫瓧娈典笂
+                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();
+        }
+
     }
 
 
@@ -159,4 +262,5 @@
         OapiGettokenResponse response = client.execute(request);
         return response.getAccessToken();
     }
+
 }

--
Gitblit v1.9.3