|
@@ -184,25 +184,17 @@ public class WeiXinCorpController {
|
|
|
|
|
|
//获取企业通讯录的accessToken
|
|
//获取企业通讯录的accessToken
|
|
private String getCorpConcactAccessToken(WxCorpInfo corpInfo) throws Exception {
|
|
private String getCorpConcactAccessToken(WxCorpInfo corpInfo) throws Exception {
|
|
- if (corpInfo.getExpireTime().isBefore(LocalDateTime.now())) {
|
|
|
|
- String url = GET_CORP_TOKEN.replace("ID", corpInfo.getCorpid()).replace("SECRET", concactSecret);
|
|
|
|
-// HttpHeaders headers = new HttpHeaders();
|
|
|
|
-// headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
-// JSONObject reqParam = new JSONObject();
|
|
|
|
-// reqParam.put("auth_corpid", corpInfo.getCorpid());
|
|
|
|
-// reqParam.put("permanent_code", corpInfo.getPermanentCode());
|
|
|
|
-//// HttpEntity<String> requestEntity = new HttpEntity<String>(reqParam.toJSONString(), headers);
|
|
|
|
- ResponseEntity<String> responseEntity = this.restTemplate.exchange(url,
|
|
|
|
- HttpMethod.GET, null, String.class);
|
|
|
|
- if (responseEntity.getStatusCode() == HttpStatus.OK) {
|
|
|
|
- String resp = responseEntity.getBody();
|
|
|
|
- JSONObject json = JSONObject.parseObject(resp);
|
|
|
|
- if (json.getIntValue("errcode") == 0) {
|
|
|
|
- String access_token = json.getString("access_token");
|
|
|
|
- corpInfo.setAccessToken(access_token);
|
|
|
|
- } else {
|
|
|
|
- throw new Exception(json.toJSONString());
|
|
|
|
- }
|
|
|
|
|
|
+ String url = GET_CORP_TOKEN.replace("ID", corpInfo.getCorpid()).replace("SECRET", corpInfo.getContactSecret());
|
|
|
|
+ ResponseEntity<String> responseEntity = this.restTemplate.exchange(url,
|
|
|
|
+ HttpMethod.GET, null, String.class);
|
|
|
|
+ if (responseEntity.getStatusCode() == HttpStatus.OK) {
|
|
|
|
+ String resp = responseEntity.getBody();
|
|
|
|
+ JSONObject json = JSONObject.parseObject(resp);
|
|
|
|
+ if (json.getIntValue("errcode") == 0) {
|
|
|
|
+ String access_token = json.getString("access_token");
|
|
|
|
+ corpInfo.setAccessToken(access_token);
|
|
|
|
+ } else {
|
|
|
|
+ throw new Exception(json.toJSONString());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return corpInfo.getAccessToken();
|
|
return corpInfo.getAccessToken();
|
|
@@ -975,9 +967,12 @@ public class WeiXinCorpController {
|
|
|
|
|
|
@RequestMapping("/getCorpMembs")
|
|
@RequestMapping("/getCorpMembs")
|
|
public HttpRespMsg getCorpMembs(String corpId) {
|
|
public HttpRespMsg getCorpMembs(String corpId) {
|
|
-
|
|
|
|
WxCorpInfo wxCorpInfo = wxCorpInfoMapper.selectById(corpId);
|
|
WxCorpInfo wxCorpInfo = wxCorpInfoMapper.selectById(corpId);
|
|
-
|
|
|
|
|
|
+ if (wxCorpInfo.getContactSecret() == null) {
|
|
|
|
+ HttpRespMsg msg = new HttpRespMsg();
|
|
|
|
+ msg.setError("请先设置企业通讯录同步Secret");
|
|
|
|
+ return msg;
|
|
|
|
+ }
|
|
Company company = companyMapper.selectById(wxCorpInfo.getCompanyId());
|
|
Company company = companyMapper.selectById(wxCorpInfo.getCompanyId());
|
|
String curCorpAccessToken = null;
|
|
String curCorpAccessToken = null;
|
|
try {
|
|
try {
|
|
@@ -1009,41 +1004,170 @@ public class WeiXinCorpController {
|
|
|
|
|
|
//检查用户是否已经存在
|
|
//检查用户是否已经存在
|
|
if (userMapper.selectCount(new QueryWrapper<User>().eq("corpwx_userid", curUserid)) == 0) {
|
|
if (userMapper.selectCount(new QueryWrapper<User>().eq("corpwx_userid", curUserid)) == 0) {
|
|
- userMapper.insert(user);
|
|
|
|
|
|
+ //检查姓名,该公司下面这个员工的姓名是否已经存在
|
|
|
|
+ List<User> userList = userMapper.selectList(new QueryWrapper<User>().eq("company_id", companyId).eq("name", user.getName()));
|
|
|
|
+ if (userList.size() == 0) {
|
|
|
|
+ //姓名也没有存在,就插入,检查人员是否已经超过上限
|
|
|
|
+ userMapper.insert(user);
|
|
|
|
+ } else {
|
|
|
|
+ //姓名已经存在,更新wxcorpid
|
|
|
|
+ List<User> updateUser = new ArrayList<User>();
|
|
|
|
+ userList.forEach(u-> {
|
|
|
|
+ if (u.getCorpwxUserid() == null) {
|
|
|
|
+ u.setCorpwxUserid(user.getCorpwxUserid());
|
|
|
|
+ updateUser.add(u);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ if (updateUser.size() > 0) {
|
|
|
|
+ userService.updateBatchById(updateUser);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
//获取部门
|
|
//获取部门
|
|
- JSONObject deptObj = getAllDepartments(curCorpAccessToken);
|
|
|
|
|
|
+ JSONObject deptObj = getDepartments(curCorpAccessToken);
|
|
JSONArray deptObjJSONArray = deptObj.getJSONArray("department");
|
|
JSONArray deptObjJSONArray = deptObj.getJSONArray("department");
|
|
|
|
|
|
|
|
+ List<Department> sysDeptList = new ArrayList<>();
|
|
for (int i=0;i<deptObjJSONArray.size(); i++) {
|
|
for (int i=0;i<deptObjJSONArray.size(); i++) {
|
|
int deptId = deptObjJSONArray.getJSONObject(i).getIntValue("id");
|
|
int deptId = deptObjJSONArray.getJSONObject(i).getIntValue("id");
|
|
|
|
+ Department department = new Department();
|
|
|
|
+ department.setDepartmentName(deptObjJSONArray.getJSONObject(i).getString("name"));
|
|
|
|
+ department.setCompanyId(companyId);
|
|
|
|
+ //检查是否已经有了
|
|
|
|
+ Department oldDept = departmentMapper.selectOne(new QueryWrapper<Department>().eq("company_id", companyId).eq("department_name", department.getDepartmentName()).last("limit 1"));
|
|
|
|
+ if (oldDept == null) {
|
|
|
|
+ if (deptId != 1) {
|
|
|
|
+ //忽略根,根是公司名称
|
|
|
|
+ departmentMapper.insert(department);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ department = oldDept;
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ sysDeptList.add(department);
|
|
|
|
+ deptObjJSONArray.getJSONObject(i).put("sys_dept_id", department.getDepartmentId());
|
|
|
|
+ Integer departmentId = department.getDepartmentId();
|
|
JSONArray userList = getDeptUserSimple(curCorpAccessToken, deptId);
|
|
JSONArray userList = getDeptUserSimple(curCorpAccessToken, deptId);
|
|
for (int m=0;m<userList.size(); m++) {
|
|
for (int m=0;m<userList.size(); m++) {
|
|
JSONObject userJson = userList.getJSONObject(m);
|
|
JSONObject userJson = userList.getJSONObject(m);
|
|
String curUserid = userJson.getString("userid");
|
|
String curUserid = userJson.getString("userid");
|
|
- System.out.println("userid="+curUserid+", name=" + userJson.getString("name")+", mobile="+userJson.getString("mobile"));
|
|
|
|
|
|
+ log.info("userid="+curUserid+", name=" + userJson.getString("name")+", mobile="+userJson.getString("mobile"));
|
|
//不存在的人员, 进行插入
|
|
//不存在的人员, 进行插入
|
|
User user = new User();
|
|
User user = new User();
|
|
|
|
|
|
user.setId(SnowFlake.nextId()+"")
|
|
user.setId(SnowFlake.nextId()+"")
|
|
- .setRoleId(defaultRole.getId())//默认普通员工
|
|
|
|
- .setRoleName(defaultRole.getRolename())
|
|
|
|
|
|
+ .setRole(0)//默认普通员工
|
|
.setCompanyId(companyId)
|
|
.setCompanyId(companyId)
|
|
- .setDepartmentId(0)
|
|
|
|
|
|
+ .setDepartmentId(departmentId)
|
|
.setName(userJson.getString("name"))
|
|
.setName(userJson.getString("name"))
|
|
.setCorpwxUserid(curUserid)
|
|
.setCorpwxUserid(curUserid)
|
|
.setColor(ColorUtil.randomColor());
|
|
.setColor(ColorUtil.randomColor());
|
|
|
|
|
|
//检查用户是否已经存在
|
|
//检查用户是否已经存在
|
|
if (userMapper.selectCount(new QueryWrapper<User>().eq("corpwx_userid", curUserid)) == 0) {
|
|
if (userMapper.selectCount(new QueryWrapper<User>().eq("corpwx_userid", curUserid)) == 0) {
|
|
- userMapper.insert(user);
|
|
|
|
|
|
+ //检查姓名,该公司下面这个员工的姓名是否已经存在
|
|
|
|
+ List<User> userList2 = userMapper.selectList(new QueryWrapper<User>().eq("company_id", companyId).eq("name", user.getName()));
|
|
|
|
+ if (userList2.size() == 0) {
|
|
|
|
+ //姓名也没有存在,就插入,检查人员是否已经超过上限
|
|
|
|
+ userMapper.insert(user);
|
|
|
|
+ } else {
|
|
|
|
+ //姓名已经存在,更新wxcorpid
|
|
|
|
+ List<User> updateUser = new ArrayList<User>();
|
|
|
|
+ userList2.forEach(u-> {
|
|
|
|
+ if (u.getCorpwxUserid() == null) {
|
|
|
|
+ u.setCorpwxUserid(user.getCorpwxUserid());
|
|
|
|
+ updateUser.add(u);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ if (updateUser.size() > 0) {
|
|
|
|
+ userService.updateBatchById(updateUser);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+// //更新信息
|
|
|
|
+// User oldUser = userMapper.selectList(new QueryWrapper<User>().eq("corpwx_userid", curUserid).eq("company_id", companyId).orderByDesc("create_time")).get(0);
|
|
|
|
+// oldUser.setName(userJson.getString("name"));
|
|
|
|
+// oldUser.setDepartmentId(departmentId);
|
|
|
|
+// userMapper.updateById(oldUser);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ //再来更新部门的层级关系
|
|
|
|
+ List<Department> needUpdateDepts = new ArrayList<>();
|
|
|
|
+ for (int i=0;i<deptObjJSONArray.size(); i++) {
|
|
|
|
+ JSONObject deptJson = deptObjJSONArray.getJSONObject(i);
|
|
|
|
+ int pid = deptJson.getInteger("parentid");
|
|
|
|
+ if (pid != 1) {
|
|
|
|
+ //根部门Id = 1
|
|
|
|
+ Integer sysDeptId = deptJson.getInteger("sys_dept_id");
|
|
|
|
+ Department department = sysDeptList.stream().filter(d -> d.getDepartmentId().equals(sysDeptId)).findFirst().get();
|
|
|
|
+ //从deptjson数组中寻找parent item
|
|
|
|
+ for (int m=0;m<deptObjJSONArray.size(); m++) {
|
|
|
|
+ JSONObject item = deptObjJSONArray.getJSONObject(m);
|
|
|
|
+ if (item.getInteger("id").equals(pid)) {
|
|
|
|
+ department.setSuperiorId(item.getInteger("sys_dept_id"));
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ needUpdateDepts.add(department);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (needUpdateDepts.size() > 0) {
|
|
|
|
+ departmentService.updateBatchById(needUpdateDepts);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+// //获取部门
|
|
|
|
+// 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);
|
|
|
|
+// for (int m=0;m<userList.size(); m++) {
|
|
|
|
+// JSONObject userJson = userList.getJSONObject(m);
|
|
|
|
+// System.out.println(userJson);
|
|
|
|
+// 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)) == 0) {
|
|
|
|
+// //检查姓名,该公司下面这个员工的姓名是否已经存在
|
|
|
|
+// List<User> userList2 = userMapper.selectList(new QueryWrapper<User>().eq("company_id", companyId).eq("name", user.getName()));
|
|
|
|
+// if (userList2.size() == 0) {
|
|
|
|
+// //姓名也没有存在,就插入,检查人员是否已经超过上限
|
|
|
|
+// userMapper.insert(user);
|
|
|
|
+// } else {
|
|
|
|
+// //姓名已经存在,更新wxcorpid
|
|
|
|
+// List<User> updateUser = new ArrayList<User>();
|
|
|
|
+// userList2.forEach(u-> {
|
|
|
|
+// if (u.getCorpwxUserid() == null) {
|
|
|
|
+// u.setCorpwxUserid(user.getCorpwxUserid());
|
|
|
|
+// updateUser.add(u);
|
|
|
|
+// }
|
|
|
|
+// });
|
|
|
|
+// if (updateUser.size() > 0) {
|
|
|
|
+// userService.updateBatchById(updateUser);
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+
|
|
return new HttpRespMsg();
|
|
return new HttpRespMsg();
|
|
}
|
|
}
|
|
|
|
|