Selaa lähdekoodia

解决公司单独勾选部门下的人员进行企业微信授权的时候,无法同步过来的问题

seyason 2 vuotta sitten
vanhempi
commit
b62e502a50

+ 37 - 8
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/controller/WeiXinCorpController.java

@@ -55,7 +55,7 @@ public class WeiXinCorpController {
     //获取企业永久授权码
     public static final String GET_CORP_PERMANENT_CODE_URL = "https://qyapi.weixin.qq.com/cgi-bin/service/get_permanent_code?suite_access_token=SUITE_ACCESS_TOKEN";
     public static final String GET_CORP_ACCESSTOKEN_URL = "https://qyapi.weixin.qq.com/cgi-bin/service/get_corp_token?suite_access_token=SUITE_ACCESS_TOKEN";
-
+    public static final String GET_AUTH_INFO = "https://qyapi.weixin.qq.com/cgi-bin/service/get_auth_info?suite_access_token=SUITE_ACCESS_TOKEN";
     //获取成员详情
     public static final String GET_USER_INFO_URL = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&userid=USERID";
     //获取部门列表
@@ -2048,16 +2048,45 @@ public class WeiXinCorpController {
             exception.printStackTrace();
         }
         if (wxCorpInfo.getAuthMode() == 0) {
+            //修正: 获取企业的人员授权范围
+            JSONArray allowUserList = null;
+            JSONArray allowPartyList = null;
+            try {
+                String url = GET_AUTH_INFO.replaceAll("SUITE_ACCESS_TOKEN", getSuiteAccessToken());
+                JSONObject map = new JSONObject();
+                map.put("auth_corpid",wxCorpInfo.getCorpid());
+                map.put("permanent_code",wxCorpInfo.getPermanentCode());
+                HttpHeaders headers = new HttpHeaders();
+                headers.setContentType(MediaType.APPLICATION_JSON);
+                HttpEntity<JSONObject> detailEntity = new HttpEntity<>(map, headers);
+                ResponseEntity<String> detailResponseEntity = restTemplate.postForEntity(url, detailEntity, String.class);
+                JSONObject json = JSONObject.parseObject(detailResponseEntity.getBody());
+                System.out.println("获取企业授权返回:"+json.toJSONString());
+                if (json.containsKey("auth_corp_info")){
+                    JSONArray jsonArray = json.getJSONObject("auth_info").getJSONArray("agent");
+                    for (int i=0;i<jsonArray.size(); i++) {
+                        JSONObject jsonObject = jsonArray.getJSONObject(i);
+                        if (jsonObject.getString("name").equals("工时管家")) {
+                            System.out.println(jsonObject);
+                            JSONObject privilege = jsonObject.getJSONObject("privilege");
+                            allowUserList = privilege.getJSONArray("allow_user");
+                            allowPartyList = privilege.getJSONArray("allow_party");
+                            break;
+                        }
+                    }
+                }
+            } catch (Exception exception) {
+                exception.printStackTrace();
+            }
             //管理员授权模式下,获取部门人员
             int companyRootDeptId = 1;
-            JSONArray usersUnderRootArray = getDeptUserInfo(curCorpAccessToken, companyRootDeptId);
-            System.out.println("获取到公司下的员工数量="+usersUnderRootArray.size());
             SysRole defaultRole = sysRoleMapper.selectOne(
                     new QueryWrapper<SysRole>().eq("company_id", companyId).eq("is_default", 1));
             List<User> allCorpWxUserList = new ArrayList<>();
-            for (int m=0;m<usersUnderRootArray.size(); m++) {
-                JSONObject userJson = usersUnderRootArray.getJSONObject(m);
-                System.out.println(userJson.toString());
+            //单独授权的人员,获取人员详情进行处理
+            for (int i=0;i<allowUserList.size(); i++) {
+                String allowUserId = allowUserList.getString(i);
+                JSONObject userJson = getUserInfo(curCorpAccessToken, allowUserId);
                 String curUserid = userJson.getString("userid");
                 String openUserid = userJson.getString("open_userid");
                 //跳过非激活状态的员工
@@ -2067,8 +2096,8 @@ public class WeiXinCorpController {
                 JSONArray userDeptArray = userJson.getJSONArray("department");
                 //取最末级的,也就是最大的deptId
                 int maxDeptId = 1;
-                for (int i=0;i<userDeptArray.size(); i++) {
-                    int curId = userDeptArray.getInteger(i);
+                for (int m=0;m<userDeptArray.size(); m++) {
+                    int curId = userDeptArray.getInteger(m);
                     if (curId > maxDeptId) maxDeptId = curId;
                 }
                 user.setId(SnowFlake.nextId()+"")