|
@@ -3,14 +3,10 @@ package com.management.platform.controller;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
-import com.management.platform.entity.SysConfig;
|
|
|
-import com.management.platform.entity.User;
|
|
|
-import com.management.platform.entity.WxCorpInfo;
|
|
|
-import com.management.platform.mapper.SysConfigMapper;
|
|
|
-import com.management.platform.mapper.UserMapper;
|
|
|
-import com.management.platform.mapper.WxCorpInfoMapper;
|
|
|
-import com.management.platform.util.HttpRespMsg;
|
|
|
-import com.management.platform.util.Sha1Util;
|
|
|
+import com.management.platform.entity.*;
|
|
|
+import com.management.platform.mapper.*;
|
|
|
+import com.management.platform.service.DepartmentService;
|
|
|
+import com.management.platform.util.*;
|
|
|
import com.qq.weixin.mp.aes.AesException;
|
|
|
import com.qq.weixin.mp.aes.WXBizMsgCrypt;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
@@ -39,10 +35,11 @@ public class WeiXinCorpController {
|
|
|
//获取成员详情
|
|
|
public static final String GET_USER_INFO_URL = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&userid=USERID";
|
|
|
//获取部门列表
|
|
|
- public static final String GET_DEPARTMENT_URL = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=ACCESS_TOKEN&id=";
|
|
|
+// public static final String GET_DEPARTMENT_URL = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=ACCESS_TOKEN";
|
|
|
+ public static final String GET_DEPARTMENT_URL = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=ACCESS_TOKEN&id=1";//获取某个部门下的子部门
|
|
|
//获取部门成员详情
|
|
|
- public static final String GET_DEPARTMENT_USER_DETAIL_URL = "https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token=ACCESS_TOKEN&department_id=DEPARTMENT_ID&fetch_child=1";
|
|
|
-
|
|
|
+ public static final String GET_DEPARTMENT_USER_DETAIL_URL = "https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token=ACCESS_TOKEN&department_id=DEPARTMENT_ID&fetch_child=0";
|
|
|
+ public static final String GET_DEPARTMENT_USER_SIMPLE_URL = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=ACCESS_TOKEN&department_id=DEPARTMENT_ID&fetch_child=0";
|
|
|
public static final String AUTH_CALLBACK_URL = "http://ymhh.yunsu.cn/wxcorp/authcallback";
|
|
|
//网页获取企业用户信息
|
|
|
public static final String GET_CORP_USERINFO_URL = "https://qyapi.weixin.qq.com/cgi-bin/service/getuserinfo3rd?suite_access_token=SUITE_ACCESS_TOKEN&code=CODE";
|
|
@@ -62,6 +59,9 @@ public class WeiXinCorpController {
|
|
|
@Autowired
|
|
|
RestTemplate restTemplate;
|
|
|
|
|
|
+ @Resource
|
|
|
+ CompanyMapper companyMapper;
|
|
|
+
|
|
|
public static String SUITE_ACCESS_TOKEN = null;
|
|
|
public static long suiteTokenExpireTime = 0L;
|
|
|
|
|
@@ -80,6 +80,12 @@ public class WeiXinCorpController {
|
|
|
WxCorpInfoMapper wxCorpInfoMapper;
|
|
|
@Resource
|
|
|
UserMapper userMapper;
|
|
|
+ @Resource
|
|
|
+ TimeTypeMapper timeTypeMapper;
|
|
|
+ @Resource
|
|
|
+ DepartmentMapper departmentMapper;
|
|
|
+ @Resource
|
|
|
+ DepartmentService departmentService;
|
|
|
|
|
|
//"获取企业微信jssdk初始化配置参数"
|
|
|
@RequestMapping("/getCorpWXConfig")
|
|
@@ -205,7 +211,7 @@ public class WeiXinCorpController {
|
|
|
log.info("===========corpId=====suitId========" + suitId);
|
|
|
|
|
|
try {
|
|
|
- WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(token, encodingAesKey, suitId);
|
|
|
+ WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(token, encodingAesKey, corpId);
|
|
|
String sMsg = wxcpt.DecryptMsg(sReqMsgSig, sReqTimeStamp, sReqNonce, requestBody);
|
|
|
log.info("解密后===msg: " + sMsg);
|
|
|
|
|
@@ -268,7 +274,7 @@ public class WeiXinCorpController {
|
|
|
if (jsonObject.has("AuthCode")) {
|
|
|
//企业授权通知
|
|
|
String authCode = jsonObject.getString("AuthCode");
|
|
|
- handleCorpAuth(authCode);
|
|
|
+// handleCorpAuth(authCode);
|
|
|
} else if (jsonObject.has("SuiteTicket")) {
|
|
|
//ticket推送
|
|
|
String ticket = jsonObject.getString("SuiteTicket");
|
|
@@ -319,7 +325,7 @@ public class WeiXinCorpController {
|
|
|
public HttpRespMsg authcallback(String auth_code, Integer expires_in, String state) {
|
|
|
HttpRespMsg msg = new HttpRespMsg();
|
|
|
log.info("authcallback收到: auth_code="+auth_code+", expires_in="+ expires_in+",state= "+state);
|
|
|
- handleCorpAuth(auth_code);
|
|
|
+// handleCorpAuth(auth_code);
|
|
|
return msg;
|
|
|
}
|
|
|
|
|
@@ -353,6 +359,7 @@ public class WeiXinCorpController {
|
|
|
time = time.plusSeconds(obj.getLong("expires_in"));
|
|
|
data.setAccessToken(curCorpAccessToken);
|
|
|
data.setCorpFullName(corpInfo.getString("corp_full_name"));
|
|
|
+ String corpName = corpInfo.getString("corp_name");
|
|
|
data.setCorpIndustry(corpInfo.getString("corp_industry"));
|
|
|
data.setCorpName(corpInfo.getString("corp_name"));
|
|
|
data.setCorpScale(corpInfo.getString("corp_scale"));
|
|
@@ -364,50 +371,154 @@ public class WeiXinCorpController {
|
|
|
JSONObject authUserInfo = obj.getJSONObject("auth_user_info");
|
|
|
data.setAuthUsername(authUserInfo.getString("name"));
|
|
|
String userId = authUserInfo.getString("userid");//授权人的userid
|
|
|
- JSONObject userDetail = getUserInfo(curCorpAccessToken, userId);
|
|
|
- //按企业微信userid匹配
|
|
|
- log.info("===userDetail==" + userDetail.toJSONString());
|
|
|
- User user = userMapper.selectOne(new QueryWrapper<User>().eq("account", userDetail.getString("mobile")).eq("username", userDetail.getString("name")));
|
|
|
- if (user != null) {
|
|
|
- //找到了匹配的企业
|
|
|
- data.setCompanyId(user.getCompanyId());
|
|
|
- }
|
|
|
- wxCorpInfoMapper.insert(data);
|
|
|
|
|
|
+ //先生成company
|
|
|
+ //检查是否已经存在
|
|
|
+ WxCorpInfo oldD = wxCorpInfoMapper.selectById(corpId);
|
|
|
+ Company company = null;
|
|
|
+ if (oldD != null) {
|
|
|
+ company = companyMapper.selectById(oldD.getCompanyId());
|
|
|
+ if (!company.getExpirationDate().isAfter(LocalDateTime.now())) {
|
|
|
+ //超期了,不处理
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (company != null) {
|
|
|
+ if (!company.getCompanyName().equals(corpName)) {
|
|
|
+ company.setCompanyName(corpName);
|
|
|
+ companyMapper.updateById(company);
|
|
|
+ }
|
|
|
+ //之前的部门和人员先删除
|
|
|
+ departmentMapper.delete(new QueryWrapper<Department>().eq("company_id", company.getId()));
|
|
|
+ userMapper.delete(new QueryWrapper<User>().eq("company_id", company.getId()));
|
|
|
+ } else {
|
|
|
+ //首先生成一个新公司,增加会员的试用一个月
|
|
|
+ company = new Company().setCompanyName(corpName)
|
|
|
+ .setExpirationDate(LocalDateTime.now().plusMonths(1));
|
|
|
+ companyMapper.insert(company);
|
|
|
+
|
|
|
+ //生成工作时长
|
|
|
+ TimeType timeType = new TimeType();
|
|
|
+ timeType.setCompanyId(company.getId());
|
|
|
+ timeTypeMapper.insert(timeType);
|
|
|
+
|
|
|
+ JSONObject userDetail = getUserInfo(curCorpAccessToken, userId);
|
|
|
+ //创建企业负责人账号
|
|
|
+ log.info("===userDetail==" + userDetail.toJSONString());
|
|
|
+ Long id = SnowFlake.nextId();
|
|
|
+ User user = new User()
|
|
|
+ .setId(id.toString())
|
|
|
+ .setRole(1)
|
|
|
+ .setName(data.getAuthUsername())
|
|
|
+ .setCorpwxUserid(userId)
|
|
|
+ .setColor(ColorUtil.randomColor())
|
|
|
+ .setCompanyId(company.getId());
|
|
|
+ userMapper.insert(user);
|
|
|
+ }
|
|
|
+ if (oldD == null) {
|
|
|
+ data.setCompanyId(company.getId());
|
|
|
+ wxCorpInfoMapper.insert(data);
|
|
|
+ }
|
|
|
+ int companyId = company.getId();
|
|
|
+ //获取公司根部门人员,也就是没有分配部门的人员
|
|
|
+ int companyRootDeptId = 1;
|
|
|
+ JSONArray unAssignedUserList = getDeptUserSimple(curCorpAccessToken, companyRootDeptId);
|
|
|
+ for (int m=0;m<unAssignedUserList.size(); m++) {
|
|
|
+ JSONObject userJson = unAssignedUserList.getJSONObject(m);
|
|
|
+ String curUserid = userJson.getString("userid");
|
|
|
+ log.info("userid="+curUserid+", name=" + userJson.getString("name")+", mobile="+userJson.getString("mobile"));
|
|
|
+ //不存在的人员, 进行插入
|
|
|
+ User user = new User();
|
|
|
+
|
|
|
+ user.setId(SnowFlake.nextId()+"")
|
|
|
+ .setRole(0)//默认普通员工
|
|
|
+ .setCompanyId(companyId)
|
|
|
+ .setName(userJson.getString("name"))
|
|
|
+ .setCorpwxUserid(curUserid)
|
|
|
+ .setColor(ColorUtil.randomColor());
|
|
|
+
|
|
|
+ //检查用户是否已经存在
|
|
|
+ if (userMapper.selectCount(new QueryWrapper<User>().eq("corpwx_userid", curUserid)) == 0) {
|
|
|
+ userMapper.insert(user);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
//获取部门
|
|
|
JSONObject deptObj = getDepartments(curCorpAccessToken);
|
|
|
JSONArray deptObjJSONArray = deptObj.getJSONArray("department");
|
|
|
|
|
|
+ List<Department> sysDeptList = new ArrayList<>();
|
|
|
for (int i=0;i<deptObjJSONArray.size(); i++) {
|
|
|
int deptId = deptObjJSONArray.getJSONObject(i).getIntValue("id");
|
|
|
- JSONArray userList = getDeptUserDetail(curCorpAccessToken, deptId);
|
|
|
+ Department department = new Department();
|
|
|
+ department.setDepartmentName(deptObjJSONArray.getJSONObject(i).getString("name"));
|
|
|
+ department.setCompanyId(companyId);
|
|
|
+ departmentMapper.insert(department);
|
|
|
+ sysDeptList.add(department);
|
|
|
+ deptObjJSONArray.getJSONObject(i).put("sys_dept_id", department.getDepartmentId());
|
|
|
+ Integer departmentId = department.getDepartmentId();
|
|
|
+ JSONArray userList = getDeptUserSimple(curCorpAccessToken, deptId);
|
|
|
for (int m=0;m<userList.size(); m++) {
|
|
|
- JSONObject item = userList.getJSONObject(m);
|
|
|
- log.info("userid="+item.getString("userid")+", name=" + item.getString("name")+", mobile="+item.getString("mobile"));
|
|
|
+ JSONObject userJson = userList.getJSONObject(m);
|
|
|
+ String curUserid = userJson.getString("userid");
|
|
|
+ log.info("userid="+curUserid+", name=" + userJson.getString("name")+", mobile="+userJson.getString("mobile"));
|
|
|
//不存在的人员, 进行插入
|
|
|
- User employee = new User();
|
|
|
- employee.setName(item.getString("name"));
|
|
|
- employee.setPhone(item.getString("mobile"));
|
|
|
-// int count = userMapper.selectCount(new QueryWrapper<User>().eq("account", employee.getAccount()).eq("company_id", data.getCompanyId()));
|
|
|
-// if (count == 0) {
|
|
|
-// //手机号不存在的,添加
|
|
|
-// if (data.getCompanyId() != null) {
|
|
|
-// employee.setCompanyId(data.getCompanyId());
|
|
|
-// employee.setRoleName("企业微信导入");
|
|
|
-// userMapper.insert(employee);
|
|
|
-// }
|
|
|
-// }
|
|
|
+ User user = new User();
|
|
|
+
|
|
|
+ user.setId(SnowFlake.nextId()+"")
|
|
|
+ .setRole(0)//默认普通员工
|
|
|
+ .setCompanyId(companyId)
|
|
|
+ .setDepartmentId(departmentId)
|
|
|
+ .setName(userJson.getString("name"))
|
|
|
+ .setCorpwxUserid(curUserid)
|
|
|
+ .setColor(ColorUtil.randomColor());
|
|
|
+
|
|
|
+ //检查用户是否已经存在
|
|
|
+ if (userMapper.selectCount(new QueryWrapper<User>().eq("corpwx_userid", curUserid)) == 0) {
|
|
|
+ userMapper.insert(user);
|
|
|
+ } 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
|
|
|
+ int 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);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private JSONArray getDeptUserDetail(String accessToken, int deptId) {
|
|
|
- String url = GET_DEPARTMENT_USER_DETAIL_URL.replace("ACCESS_TOKEN", accessToken).replace("DEPARTMENT_ID", ""+deptId);
|
|
|
+ private JSONArray getDeptUserSimple(String accessToken, int deptId) {
|
|
|
+ String url = GET_DEPARTMENT_USER_SIMPLE_URL.replace("ACCESS_TOKEN", accessToken).replace("DEPARTMENT_ID", ""+deptId);
|
|
|
String result = restTemplate.getForObject(url, String.class);
|
|
|
+ log.info("部门人员详情:"+result);
|
|
|
JSONObject obj = JSONObject.parseObject(result);
|
|
|
JSONArray userlist = obj.getJSONArray("userlist");
|
|
|
|
|
@@ -417,7 +528,9 @@ public class WeiXinCorpController {
|
|
|
private JSONObject getDepartments(String accessToken) {
|
|
|
String url = GET_DEPARTMENT_URL.replace("ACCESS_TOKEN", accessToken);
|
|
|
String result = restTemplate.getForObject(url, String.class);
|
|
|
+ log.info("部门列表:"+result);
|
|
|
JSONObject obj = JSONObject.parseObject(result);
|
|
|
+
|
|
|
return obj;
|
|
|
}
|
|
|
private JSONObject getUserInfo(String accessToken, String userId) {
|