Min před 2 roky
rodič
revize
06da49c69c

+ 53 - 40
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/controller/FeishuInfoController.java

@@ -228,9 +228,11 @@ public class FeishuInfoController {
                     Optional<DepartmentFeishu> dpFs = departmentFeishuList.stream().filter(dl -> dl.getFeishuOpenDeptid().equals(max)).findFirst();
                     if(dpFs.isPresent()){
                         Optional<Department> dp = departmentList.stream().filter(dl -> dl.getDepartmentId().equals(dpFs.get().getSysDeptid())).findFirst();
-                        user.setDepartmentName(dp.get().getDepartmentName());
-                        user.setDepartmentId(dp.get().getDepartmentId());
-                        user.setDepartmentCascade(convertDepartmentIdToCascade(dp.get().getDepartmentId(),departmentList));
+                        if(dp.isPresent()){
+                            user.setDepartmentName(dp.get().getDepartmentName());
+                            user.setDepartmentId(dp.get().getDepartmentId());
+                            user.setDepartmentCascade(convertDepartmentIdToCascade(dp.get().getDepartmentId(),departmentList));
+                        }
                     }
                     boolean b = userList.stream().anyMatch(ul ->ul.getFeishuUserid()!=null&&ul.getFeishuUserid().equals(user.getFeishuUserid()));
                     if(!b){
@@ -284,7 +286,7 @@ public class FeishuInfoController {
             for (int i = 0; i < subDepartmentList.size(); i++) {
                 JSONObject ob = subDepartmentList.getJSONObject(i);
                 if(ob.getString("open_department_id")!=null){
-                    JSONArray array = getSubDepartmentList(feishuInfo, ob.getString("open_department_id"));
+                    JSONArray array = getSubDepartmentList(feishuInfo,ob.getString("open_department_id"));
                     result.addAll(array);
                 }
             }
@@ -593,22 +595,27 @@ public class FeishuInfoController {
     //"企业微信数据回调"
     @RequestMapping(value = "/dataCallback", method = RequestMethod.POST)
     @ResponseBody
-    public void dataCallback(@RequestBody JSONObject jsonParam, HttpServletResponse response,String schema,String header,String event) throws IOException {
+    public void dataCallback(@RequestBody JSONObject jsonParam, HttpServletResponse response) throws IOException {
+        //用于验证飞书的部分
         PrintWriter out = null;
         JSONObject jsonObject=new JSONObject();
-        jsonObject.put("challenge",jsonParam.getString("challenge"));
-        //设定类容为json的格式
-        response.setContentType("application/json;charset=UTF-8");
-        out = response.getWriter();
-        //写到客户端
-        out.write(jsonObject.toJSONString());
-        out.flush();
+        if(jsonParam.getString("challenge")!=null){
+            jsonObject.put("challenge",jsonParam.getString("challenge"));
+            //设定类容为json的格式
+            response.setContentType("application/json;charset=UTF-8");
+            out = response.getWriter();
+            //写到客户端
+            out.write(jsonObject.toJSONString());
+            out.flush();
+            return;
+        }
+        log.info("============回调参数==========="+jsonParam.toJSONString());
         JSONObject headerOb =jsonParam.getJSONObject("header");
-        if(headerOb==null){
+        if(headerOb==null||headerOb.size()<=0){
             return;
         }
         JSONObject eventOb = jsonParam.getJSONObject("event");
-        log.info("========schema========="+schema);
+        log.info("=========event======"+eventOb.toJSONString());
         String appId = headerOb.getString("app_id");
         log.info("========app_id========"+appId);
         String tenantKey = headerOb.getString("tenant_key");
@@ -622,7 +629,7 @@ public class FeishuInfoController {
             SysRole role = sysRoleMapper.selectOne(new QueryWrapper<SysRole>().eq("company_id",feishuInfo.getCompanyId()).eq("rolename","普通员工"));
             //通讯录权限范围变更
             //todo:新增的
-            if(eventOb!=null&&eventOb.getJSONObject("added")!=null){
+            if(eventOb!=null&&eventOb.getJSONObject("added").size()>0){
                 JSONObject addedOb = eventOb.getJSONObject("added");
                 //todo:处理部门
                 JSONArray departmentArray = addedOb.getJSONArray("departments");
@@ -635,6 +642,8 @@ public class FeishuInfoController {
                         allNeedTodo.addAll(jsonArray);
                     }
                 }
+                List<Department> departmentList=new ArrayList<>();
+                List<DepartmentFeishu> departmentFeishuList=new ArrayList<>();
                 for (int j = 0; j < allNeedTodo.size(); j++) {
                     JSONObject ob = allNeedTodo.getJSONObject(j);
                     String departmentName = String.valueOf(ob.get("name"));
@@ -658,7 +667,7 @@ public class FeishuInfoController {
                         department.setFeishuDeptid(openDepartmentId);
                         departmentMapper.insert(department);
                         List<DepartmentOtherManager> odList=new ArrayList<>();
-                        if(leaders.size()>0){
+                        if(leaders!=null&&leaders.size()>0){
                             //todo:处理主要负责人
                             Optional<Object> first = leaders.stream().filter(l -> ((JSONObject) l).getIntValue("leaderType") == 1).findFirst();
                             if(first.isPresent()){
@@ -683,18 +692,18 @@ public class FeishuInfoController {
                             }
                         }
                         departmentMapper.updateById(department);
+                        departmentList.add(department);
                         departmentFeishu.setSysDeptid(department.getDepartmentId());
                         departmentFeishuMapper.insert(departmentFeishu);
+                        departmentFeishuList.add(departmentFeishu);
                         departmentOtherManagerService.saveBatch(odList);
                     }
                 }
-                List<Department> departmentList = departmentMapper.selectList(new QueryWrapper<Department>().eq("company_id",feishuInfo.getCompanyId()));
-                List<DepartmentFeishu> departmentFeishuList = departmentFeishuMapper.selectList(new QueryWrapper<DepartmentFeishu>().eq("corpid", feishuInfo.getCorpid()));
                 List<User> newUserList=new ArrayList<>();
                 for (DepartmentFeishu departmentFeishu : departmentFeishuList) {
                     Optional<Department> first = departmentList.stream().filter(dl -> dl.getDepartmentId().equals(departmentFeishu.getSysDeptid())).findFirst();
                     //获取到当前部门的上级部门
-                    Optional<DepartmentFeishu> wx = departmentFeishuList.stream().filter(dl -> dl.getFeishuDeptid().equals(departmentFeishu.getFeishuParentid())).findFirst();
+                    Optional<DepartmentFeishu> wx = departmentFeishuList.stream().filter(dl -> dl.getFeishuOpenDeptid().equals(departmentFeishu.getFeishuParentid())).findFirst();
                     if(first.isPresent()){
                         if(wx.isPresent()){
                             Optional<Department> dp = departmentList.stream().filter(dl -> dl.getDepartmentId().equals(wx.get().getSysDeptid())).findFirst();
@@ -734,9 +743,11 @@ public class FeishuInfoController {
                             Optional<DepartmentFeishu> dpFs = departmentFeishuList.stream().filter(dl -> dl.getFeishuOpenDeptid().equals(max)).findFirst();
                             if(dpFs.isPresent()){
                                 Optional<Department> dp = departmentList.stream().filter(dl -> dl.getDepartmentId().equals(dpFs.get().getSysDeptid())).findFirst();
-                                user.setDepartmentName(dp.get().getDepartmentName());
-                                user.setDepartmentId(dp.get().getDepartmentId());
-                                user.setDepartmentCascade(convertDepartmentIdToCascade(dp.get().getDepartmentId(),departmentList));
+                                if(dp.isPresent()){
+                                    user.setDepartmentName(dp.get().getDepartmentName());
+                                    user.setDepartmentId(dp.get().getDepartmentId());
+                                    user.setDepartmentCascade(convertDepartmentIdToCascade(dp.get().getDepartmentId(),departmentList));
+                                }
                             }
                             boolean c = newUserList.stream().anyMatch(ul ->ul.getFeishuUserid()!=null&&ul.getFeishuUserid().equals(user.getFeishuUserid()));
                             if(!c){
@@ -747,24 +758,26 @@ public class FeishuInfoController {
                 }
                 //todo:处理人员
                 JSONArray userArrays = addedOb.getJSONArray("users");
-                for (int i = 0; i < userArrays.size(); i++) {
-                    JSONObject userOb = userArrays.getJSONObject(i);
-                    boolean b = allUserList.stream().anyMatch(al -> al.getFeishuUserid() != null && al.getFeishuUserid().equals(userOb.getString("open_id")));
-                    if(!b){
-                        //不存在的人员, 进行插入
-                        User user = new User();
-                        user.setId(SnowFlake.nextId()+"")
-                                .setRoleId(role.getId())//默认普通员工
-                                .setRoleName(role.getRolename())
-                                .setCompanyId(feishuInfo.getCompanyId())
-                                .setName(userOb.getString("name"))
-                                .setFeishuUserid(userOb.getString("open_id"))
-                                .setColor(ColorUtil.randomColor())
-                                .setJobNumber(userOb.getString("user_id"))
-                                .setPassword(MD5Util.getPassword("000000"));
-                        boolean c = newUserList.stream().anyMatch(ul ->ul.getFeishuUserid()!=null&&ul.getFeishuUserid().equals(user.getFeishuUserid()));
-                        if(!c){
-                            newUserList.add(user);
+                if(userArrays!=null&&userArrays.size()>0){
+                    for (int i = 0; i < userArrays.size(); i++) {
+                        JSONObject userOb = userArrays.getJSONObject(i);
+                        boolean b = allUserList.stream().anyMatch(al -> al.getFeishuUserid() != null && al.getFeishuUserid().equals(userOb.getString("open_id")));
+                        if(!b){
+                            //不存在的人员, 进行插入
+                            User user = new User();
+                            user.setId(SnowFlake.nextId()+"")
+                                    .setRoleId(role.getId())//默认普通员工
+                                    .setRoleName(role.getRolename())
+                                    .setCompanyId(feishuInfo.getCompanyId())
+                                    .setName(userOb.getString("name"))
+                                    .setFeishuUserid(userOb.getString("open_id"))
+                                    .setColor(ColorUtil.randomColor())
+                                    .setJobNumber(userOb.getString("user_id"))
+                                    .setPassword(MD5Util.getPassword("000000"));
+                            boolean c = newUserList.stream().anyMatch(ul ->ul.getFeishuUserid()!=null&&ul.getFeishuUserid().equals(user.getFeishuUserid()));
+                            if(!c){
+                                newUserList.add(user);
+                            }
                         }
                     }
                 }

+ 24 - 33
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/service/impl/FeishuInfoServiceImpl.java

@@ -102,47 +102,42 @@ public class FeishuInfoServiceImpl extends ServiceImpl<FeishuInfoMapper, FeishuI
                 String result="";
                 FeishuInfo feishuInfo = getOne(new QueryWrapper<FeishuInfo>().eq("app_id", appId));
                 if(feishuInfo!=null){
-                        if(feishuInfo.getExpireTime().isBefore(LocalDateTime.now())){
-                                String url = GET_TENANT_ACCESS_TOKEN;
-                                HttpHeaders headers = new HttpHeaders();
-                                RestTemplate restTemplate = new RestTemplate();
-                                MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
-                                headers.setContentType(type);
-                                JSONObject requestMap = new JSONObject();
-                                requestMap.put("app_id",feishuInfo.getAppId());
-                                requestMap.put("app_secret",feishuInfo.getAppSecret());
-                                HttpEntity<JSONObject> entity = new HttpEntity<>(requestMap, headers);
-                                ResponseEntity<String> ResponseEntity = restTemplate.postForEntity(url, entity, String.class);
-                                if (ResponseEntity.getStatusCode() == HttpStatus.OK) {
-                                        String resp = ResponseEntity.getBody();
-                                        JSONObject respJson = JSONObject.parseObject(resp);
-                                        if (respJson.getInteger("code")==0){
-                                                result=respJson.getString("tenant_access_token");
-                                                feishuInfo.setAccessToken(result);
-                                                LocalDateTime now = LocalDateTime.now();
-                                                now = now.plusSeconds(7200);
-                                                feishuInfo.setExpireTime(now);
-                                                feishuInfoMapper.updateById(feishuInfo);
-                                        }
+                        String url = GET_TENANT_ACCESS_TOKEN;
+                        HttpHeaders headers = new HttpHeaders();
+                        RestTemplate restTemplate = new RestTemplate();
+                        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
+                        headers.setContentType(type);
+                        JSONObject requestMap = new JSONObject();
+                        requestMap.put("app_id",feishuInfo.getAppId());
+                        requestMap.put("app_secret",feishuInfo.getAppSecret());
+                        HttpEntity<JSONObject> entity = new HttpEntity<>(requestMap, headers);
+                        ResponseEntity<String> ResponseEntity = restTemplate.postForEntity(url, entity, String.class);
+                        if (ResponseEntity.getStatusCode() == HttpStatus.OK) {
+                                String resp = ResponseEntity.getBody();
+                                JSONObject respJson = JSONObject.parseObject(resp);
+                                if (respJson.getInteger("code")==0){
+                                        result=respJson.getString("tenant_access_token");
+                                        feishuInfo.setAccessToken(result);
+                                        LocalDateTime now = LocalDateTime.now();
+                                        now = now.plusSeconds(7200);
+                                        feishuInfo.setExpireTime(now);
+                                        feishuInfoMapper.updateById(feishuInfo);
                                 }
                         }
+
                 }
                 return result;
         }
 
         @Override
         public JSONObject getCorpInfo(FeishuInfo feishuInfo){
-                if(feishuInfo.getExpireTime().isBefore(LocalDateTime.now())){
-                        String tenantAccessToken = getTenantAccessToken(feishuInfo.getAppId());
-                        feishuInfo.setAccessToken(tenantAccessToken);
-                }
                 JSONObject result=new JSONObject();
                 String url = GET_CORP_INFO;
                 HttpHeaders headers = new HttpHeaders();
                 RestTemplate restTemplate = new RestTemplate();
                 MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                 headers.setContentType(type);
-                headers.add("Authorization","Bearer "+feishuInfo.getAccessToken());
+                headers.add("Authorization","Bearer "+getTenantAccessToken(feishuInfo.getAppId()));
                 HttpEntity<JSONObject> httpEntity = new HttpEntity<>(null, headers);
                 ResponseEntity<String> ResponseEntity = restTemplate.exchange(url,HttpMethod.GET,httpEntity,String.class);
                 if (ResponseEntity.getStatusCode() == HttpStatus.OK) {
@@ -158,17 +153,13 @@ public class FeishuInfoServiceImpl extends ServiceImpl<FeishuInfoMapper, FeishuI
 
         @Override
         public JSONObject getUserInfo(FeishuInfo feishuInfo, String feishuUserId){
-                if(feishuInfo.getExpireTime().isBefore(LocalDateTime.now())){
-                        String tenantAccessToken = getTenantAccessToken(feishuInfo.getAppId());
-                        feishuInfo.setAccessToken(tenantAccessToken);
-                }
                 JSONObject result=new JSONObject();
                 String url = GET_USER_INFO.replace(":user_id",feishuUserId);
                 HttpHeaders headers = new HttpHeaders();
                 RestTemplate restTemplate = new RestTemplate();
                 MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                 headers.setContentType(type);
-                headers.add("Authorization","Bearer "+feishuInfo.getAccessToken());
+                headers.add("Authorization","Bearer "+getTenantAccessToken(feishuInfo.getAppId()));
                 HttpEntity<JSONObject> httpEntity = new HttpEntity<>(null, headers);
                 ResponseEntity<String> ResponseEntity = restTemplate.exchange(url,HttpMethod.GET,httpEntity,String.class);
                 if (ResponseEntity.getStatusCode() == HttpStatus.OK) {
@@ -239,7 +230,7 @@ public class FeishuInfoServiceImpl extends ServiceImpl<FeishuInfoMapper, FeishuI
                 RestTemplate restTemplate = new RestTemplate();
                 MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                 headers.setContentType(type);
-                headers.add("Authorization","Bearer "+feishuInfo.getAccessToken());
+                headers.add("Authorization","Bearer "+getTenantAccessToken(feishuInfo.getAppId()));
                 HttpEntity<JSONObject> httpEntity = new HttpEntity<>(null, headers);
                 Map<String,Object> map=new HashMap<>();
                 map.put("fetch_child",0);