|
@@ -2032,80 +2032,132 @@ public class WeiXinCorpController {
|
|
|
|
|
|
@RequestMapping("/syncMembByCardTime")
|
|
@RequestMapping("/syncMembByCardTime")
|
|
public HttpRespMsg syncMembByCardTime(Integer companyId) {
|
|
public HttpRespMsg syncMembByCardTime(Integer companyId) {
|
|
- Company company = companyMapper.selectById(companyId);
|
|
|
|
WxCorpInfo wxCorpInfo = wxCorpInfoMapper.selectOne(new QueryWrapper<WxCorpInfo>().eq("company_id", companyId));
|
|
WxCorpInfo wxCorpInfo = wxCorpInfoMapper.selectOne(new QueryWrapper<WxCorpInfo>().eq("company_id", companyId));
|
|
|
|
+ Company company = companyMapper.selectOne(new QueryWrapper<Company>().eq("id", companyId));
|
|
|
|
+ Integer employeeCnt = userMapper.selectCount(new QueryWrapper<User>().eq("company_id", companyId).eq("is_active",1));
|
|
|
|
+ if (employeeCnt > company.getStaffCountMax()){
|
|
|
|
+ HttpRespMsg msg = new HttpRespMsg();
|
|
|
|
+ //公司人员已达上限,请联系客服提高人数上限。
|
|
|
|
+ msg.setError(MessageUtils.message("wx.employeeFull"));
|
|
|
|
+ return msg;
|
|
|
|
+ }
|
|
|
|
+ if (wxCorpInfo == null) {
|
|
|
|
+ HttpRespMsg msg = new HttpRespMsg();
|
|
|
|
+ //msg.setError("仅企业微信用户支持该操作");
|
|
|
|
+ msg.setError(MessageUtils.message("wx.noWXUser"));
|
|
|
|
+ return msg;
|
|
|
|
+ }
|
|
String curCorpAccessToken = null;
|
|
String curCorpAccessToken = null;
|
|
try {
|
|
try {
|
|
curCorpAccessToken = getCorpAccessToken(wxCorpInfo);
|
|
curCorpAccessToken = getCorpAccessToken(wxCorpInfo);
|
|
} catch (Exception exception) {
|
|
} catch (Exception exception) {
|
|
exception.printStackTrace();
|
|
exception.printStackTrace();
|
|
}
|
|
}
|
|
- //获取公司根部门人员,也就是没有分配部门的人员
|
|
|
|
- int companyRootDeptId = 1;
|
|
|
|
- JSONArray unAssignedUserList = getDeptUserSimple(curCorpAccessToken, companyRootDeptId);
|
|
|
|
- SysRole defaultRole = sysRoleMapper.selectOne(
|
|
|
|
- new QueryWrapper<SysRole>().eq("company_id", companyId).eq("is_default", 1));
|
|
|
|
-
|
|
|
|
- for (int m=0;m<unAssignedUserList.size(); m++) {
|
|
|
|
- JSONObject userJson = unAssignedUserList.getJSONObject(m);
|
|
|
|
- String curUserid = userJson.getString("userid");
|
|
|
|
- System.out.println("userid="+curUserid+", name=" + userJson.getString("name")+", mobile="+userJson.getString("mobile"));
|
|
|
|
- //不存在的人员, 进行插入
|
|
|
|
- User user = new User();
|
|
|
|
-
|
|
|
|
- user.setId(SnowFlake.nextId()+"")
|
|
|
|
- .setRoleId(defaultRole.getId())//默认普通员工
|
|
|
|
- .setRoleName(defaultRole.getRolename())
|
|
|
|
- .setCompanyId(companyId)
|
|
|
|
- .setName(userJson.getString("name"))
|
|
|
|
- .setCorpwxUserid(curUserid)
|
|
|
|
- .setColor(ColorUtil.randomColor())
|
|
|
|
- .setInductionDate(LocalDate.now());
|
|
|
|
-
|
|
|
|
- //检查用户是否已经存在
|
|
|
|
- if (userMapper.selectCount(new QueryWrapper<User>().eq("corpwx_userid", curUserid)) == 0) {
|
|
|
|
- userMapper.insert(user);
|
|
|
|
|
|
+ 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();
|
|
}
|
|
}
|
|
- }
|
|
|
|
-
|
|
|
|
- //获取部门
|
|
|
|
- JSONObject deptObj = getAllDepartments(curCorpAccessToken);
|
|
|
|
- JSONArray deptObjJSONArray = deptObj.getJSONArray("department");
|
|
|
|
-
|
|
|
|
- for (int i=0;i<deptObjJSONArray.size(); i++) {
|
|
|
|
- int deptId = deptObjJSONArray.getJSONObject(i).getIntValue("id");
|
|
|
|
-
|
|
|
|
- JSONArray userList = getDeptUserSimple(curCorpAccessToken, deptId);
|
|
|
|
- System.out.println("该部门下用户数量:"+userList.size());
|
|
|
|
- for (int m=0;m<userList.size(); m++) {
|
|
|
|
- JSONObject userJson = userList.getJSONObject(m);
|
|
|
|
|
|
+ //管理员授权模式下,获取部门人员
|
|
|
|
+ SysRole defaultRole = sysRoleMapper.selectOne(
|
|
|
|
+ new QueryWrapper<SysRole>().eq("company_id", companyId).eq("is_default", 1));
|
|
|
|
+ //单独授权的人员,获取人员详情进行处理
|
|
|
|
+ for (int i=0;i<allowUserList.size(); i++) {
|
|
|
|
+ String allowUserId = allowUserList.getString(i);
|
|
|
|
+ JSONObject userJson = getUserInfo(curCorpAccessToken, allowUserId);
|
|
String curUserid = userJson.getString("userid");
|
|
String curUserid = userJson.getString("userid");
|
|
- System.out.println("userid="+curUserid+", name=" + userJson.getString("name")+", mobile="+userJson.getString("mobile"));
|
|
|
|
|
|
+ String openUserid = userJson.getString("open_userid");
|
|
|
|
+ //跳过非激活状态的员工
|
|
|
|
+ if (userJson.getInteger("status") != 1) continue;
|
|
//不存在的人员, 进行插入
|
|
//不存在的人员, 进行插入
|
|
User user = new User();
|
|
User user = new User();
|
|
-
|
|
|
|
|
|
+ JSONArray userDeptArray = userJson.getJSONArray("department");
|
|
|
|
+ //取最末级的,也就是最大的deptId
|
|
|
|
+ int maxDeptId = 1;
|
|
|
|
+ for (int m=0;m<userDeptArray.size(); m++) {
|
|
|
|
+ int curId = userDeptArray.getInteger(m);
|
|
|
|
+ if (curId > maxDeptId) maxDeptId = curId;
|
|
|
|
+ }
|
|
|
|
+ System.out.println("userid="+curUserid+", name=" + userJson.getString("name")+", mobile="+userJson.getString("mobile"));
|
|
|
|
+ //不存在的人员, 进行插入
|
|
user.setId(SnowFlake.nextId()+"")
|
|
user.setId(SnowFlake.nextId()+"")
|
|
.setRoleId(defaultRole.getId())//默认普通员工
|
|
.setRoleId(defaultRole.getId())//默认普通员工
|
|
.setRoleName(defaultRole.getRolename())
|
|
.setRoleName(defaultRole.getRolename())
|
|
.setCompanyId(companyId)
|
|
.setCompanyId(companyId)
|
|
- .setDepartmentId(0)
|
|
|
|
.setName(userJson.getString("name"))
|
|
.setName(userJson.getString("name"))
|
|
.setCorpwxUserid(curUserid)
|
|
.setCorpwxUserid(curUserid)
|
|
- .setColor(ColorUtil.randomColor());
|
|
|
|
-
|
|
|
|
|
|
+ .setColor(ColorUtil.randomColor())
|
|
|
|
+ .setInductionDate(LocalDate.now());
|
|
//检查用户是否已经存在
|
|
//检查用户是否已经存在
|
|
- if (userMapper.selectCount(new QueryWrapper<User>().eq("corpwx_userid", curUserid).eq("company_id", companyId)) == 0) {
|
|
|
|
|
|
+ if (userMapper.selectCount(new QueryWrapper<User>().eq("corpwx_userid", curUserid)) == 0) {
|
|
userMapper.insert(user);
|
|
userMapper.insert(user);
|
|
- } else {
|
|
|
|
- User curUser = userMapper.selectOne(new QueryWrapper<User>().eq("corpwx_userid", curUserid).eq("company_id", companyId));
|
|
|
|
- System.out.println("找到用户:"+curUser.getName());
|
|
|
|
- if (curUser.getDepartmentId() != null) {
|
|
|
|
- Department dept = departmentMapper.selectById(curUser.getDepartmentId());
|
|
|
|
- if (dept != null) {
|
|
|
|
- System.out.println("部门:"+dept.getDepartmentName());
|
|
|
|
|
|
+ System.out.println("新用户==="+user.getName());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //获取部门
|
|
|
|
+ JSONObject deptObj = getAllDepartments(curCorpAccessToken);
|
|
|
|
+ JSONArray deptObjJSONArray = deptObj.getJSONArray("department");
|
|
|
|
+ for (int i=0;i<deptObjJSONArray.size(); i++) {
|
|
|
|
+ int deptId = deptObjJSONArray.getJSONObject(i).getIntValue("id");
|
|
|
|
+
|
|
|
|
+ JSONArray userList = getDeptUserSimple(curCorpAccessToken, deptId);
|
|
|
|
+ System.out.println("该部门下用户数量:"+userList.size());
|
|
|
|
+ for (int m=0;m<userList.size(); m++) {
|
|
|
|
+ JSONObject userJson = userList.getJSONObject(m);
|
|
|
|
+ String curUserid = userJson.getString("userid");
|
|
|
|
+ System.out.println("userid="+curUserid+", name=" + userJson.getString("name")+", mobile="+userJson.getString("mobile"));
|
|
|
|
+ //不存在的人员, 进行插入
|
|
|
|
+ User user = new User();
|
|
|
|
+
|
|
|
|
+ user.setId(SnowFlake.nextId()+"")
|
|
|
|
+ .setRoleId(defaultRole.getId())//默认普通员工
|
|
|
|
+ .setRoleName(defaultRole.getRolename())
|
|
|
|
+ .setCompanyId(companyId)
|
|
|
|
+ .setDepartmentId(0)
|
|
|
|
+ .setName(userJson.getString("name"))
|
|
|
|
+ .setCorpwxUserid(curUserid)
|
|
|
|
+ .setColor(ColorUtil.randomColor());
|
|
|
|
+
|
|
|
|
+ //检查用户是否已经存在
|
|
|
|
+ if (userMapper.selectCount(new QueryWrapper<User>().eq("corpwx_userid", curUserid).eq("company_id", companyId)) == 0) {
|
|
|
|
+ userMapper.insert(user);
|
|
|
|
+ } else {
|
|
|
|
+ User curUser = userMapper.selectOne(new QueryWrapper<User>().eq("corpwx_userid", curUserid).eq("company_id", companyId));
|
|
|
|
+ System.out.println("找到用户:"+curUser.getName());
|
|
|
|
+ if (curUser.getDepartmentId() != null) {
|
|
|
|
+ Department dept = departmentMapper.selectById(curUser.getDepartmentId());
|
|
|
|
+ if (dept != null) {
|
|
|
|
+ System.out.println("部门:"+dept.getDepartmentName());
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|