|
@@ -90,6 +90,9 @@ public class DingDingServiceImpl implements DingDingService {
|
|
|
//首先生成一个新公司,增加会员的试用一个月
|
|
|
company = new Company().setCompanyName(corpName)
|
|
|
.setExpirationDate(LocalDateTime.now().plusMonths(1));
|
|
|
+ company.setPackageExpense(1);
|
|
|
+ company.setPackageCustomer(1);
|
|
|
+ company.setPackageProject(1);
|
|
|
companyMapper.insert(company);
|
|
|
|
|
|
//生成工作时长
|
|
@@ -140,13 +143,76 @@ public class DingDingServiceImpl implements DingDingService {
|
|
|
//获取部门
|
|
|
if (dingding.getAccessToken() != null) {
|
|
|
String accessToken = dingding.getAccessToken();
|
|
|
- getDepartmentList(company.getId(), corpid, accessToken, 1L, null);
|
|
|
+ System.out.println("======dingding========首次授权, accessToken="+accessToken);
|
|
|
+// getDepartmentList(company.getId(), corpid, accessToken, 1L, null);
|
|
|
+
|
|
|
+ //获取授权的部门
|
|
|
+ getAuthedDeptsAndUsers(dingding, accessToken);
|
|
|
+
|
|
|
+ } else {
|
|
|
+ System.out.println("AccessToken为空,无法获取部门列表");
|
|
|
}
|
|
|
} else {
|
|
|
companyDingdingMapper.updateById(dingding);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public String syncCorpMembs(String corpid) throws ApiException {
|
|
|
+ CompanyDingding dingding = companyDingdingMapper.selectById(corpid);
|
|
|
+ if (dingding.getExpireTime().isBefore(LocalDateTime.now())) {
|
|
|
+ SysConfig config = sysConfigMapper.selectOne(new QueryWrapper<SysConfig>().eq("param_key", "dingding_suite_ticket"));
|
|
|
+ OapiServiceGetCorpTokenResponse result = getAuthCorpAccessToken(corpid, config.getParamValue());
|
|
|
+ if (result != null) {
|
|
|
+ dingding.setAccessToken(result.getAccessToken());
|
|
|
+ LocalDateTime time = LocalDateTime.now();
|
|
|
+ time = time.plusSeconds(result.getExpiresIn());//设置token过期时间
|
|
|
+ dingding.setExpireTime(time);
|
|
|
+ companyDingdingMapper.updateById(dingding);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String accessToken = dingding.getAccessToken();
|
|
|
+ System.out.println("syncCorpMembs 开始获取部门, accessToken="+accessToken);
|
|
|
+
|
|
|
+ //获取授权的部门
|
|
|
+ getAuthedDeptsAndUsers(dingding, accessToken);
|
|
|
+ return "调用成功";
|
|
|
+
|
|
|
+// String rest = getDepartmentList(dingding.getCompanyId(), corpid, accessToken, 1L, null);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void getAuthedDeptsAndUsers(CompanyDingding dingding, String accessToken) {
|
|
|
+ try {
|
|
|
+ DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/auth/scopes");
|
|
|
+ OapiAuthScopesRequest req = new OapiAuthScopesRequest();
|
|
|
+ req.setHttpMethod("GET");
|
|
|
+ OapiAuthScopesResponse rsp = client.execute(req, accessToken);
|
|
|
+ System.out.println("获取通讯录授权范围:"+rsp.getBody());
|
|
|
+ JSONObject json = JSONObject.parseObject(rsp.getBody());
|
|
|
+ if (json.getInteger("errcode") == 0) {
|
|
|
+ JSONArray deptArray = json.getJSONObject("auth_org_scopes").getJSONArray("authed_dept");
|
|
|
+ List<Long> result = new ArrayList<>();
|
|
|
+ for (int i=0;i<deptArray.size(); i++) {
|
|
|
+ long deptId = deptArray.getLongValue(i);
|
|
|
+ getDepartmentDetailAndUserList(dingding.getCompanyId(), dingding.getCorpid(), dingding.getAccessToken(), deptId);
|
|
|
+ }
|
|
|
+
|
|
|
+ //直接授权的人员,没有部门
|
|
|
+ JSONArray userArray = json.getJSONObject("auth_org_scopes").getJSONArray("authed_user");
|
|
|
+ for (int i=0;i<userArray.size(); i++) {
|
|
|
+ String userdingId = userArray.getString(i);
|
|
|
+ //获取人员详情
|
|
|
+ getUserDetail(dingding.getCompanyId(), 0, userdingId, accessToken);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ System.err.println("获取通讯录范围出错:" + json.toJSONString());
|
|
|
+ }
|
|
|
+ } catch (ApiException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private void getUserDetailInfo(User user, String accessToken) throws ApiException {
|
|
|
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
|
|
|
OapiV2UserGetRequest req = new OapiV2UserGetRequest();
|
|
@@ -314,14 +380,53 @@ public class DingDingServiceImpl implements DingDingService {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ //获取部门详情和部门下的人员
|
|
|
+ public void getDepartmentDetailAndUserList(Integer companyId, String corpid, String accessToken, long deptId) {
|
|
|
+ try {
|
|
|
+ DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/get");
|
|
|
+ OapiV2DepartmentGetRequest req = new OapiV2DepartmentGetRequest();
|
|
|
+ req.setDeptId(deptId);
|
|
|
+ OapiV2DepartmentGetResponse rsp = client.execute(req, accessToken);
|
|
|
+ System.out.println(rsp.getBody());
|
|
|
+ JSONObject json = JSONObject.parseObject(rsp.getBody());
|
|
|
+ if (json.getInteger("errcode") == 0) {
|
|
|
+ JSONObject dept = json.getJSONObject("result");
|
|
|
+ DepartmentDingding departmentDingding = new DepartmentDingding();
|
|
|
+ departmentDingding.setCorpid(corpid);
|
|
|
+ departmentDingding.setDdDeptid(dept.getInteger("dept_id"));
|
|
|
+ departmentDingding.setName(dept.getString("name"));
|
|
|
+ departmentDingding.setDdParentid(dept.getInteger("parent_id"));
|
|
|
+ Department department = new Department();
|
|
|
+ department.setDepartmentName(dept.getString("name"));
|
|
|
+ department.setCompanyId(companyId);
|
|
|
+ //检查该部门是否已经创建
|
|
|
+ List<Department> existDept = departmentMapper.selectList(new QueryWrapper<Department>().eq("company_id", companyId).eq("department_name", dept.getString("name")));
|
|
|
+ if (existDept.size() > 0) {
|
|
|
+ department = existDept.get(0);
|
|
|
+ } else {
|
|
|
+ departmentMapper.insert(department);
|
|
|
+ }
|
|
|
+
|
|
|
+ departmentDingding.setSysDeptid(department.getDepartmentId());
|
|
|
+ departmentDingdingMapper.insert(departmentDingding);
|
|
|
+
|
|
|
+ //获取该部门下的人员
|
|
|
+ getDeptUserIdList(companyId, deptId, department.getDepartmentId(), accessToken);
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (ApiException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
//获取该部门下的人员和子部门列表,递归下一级子部门
|
|
|
- public void getDepartmentList(Integer companyId, String corpid, String access_token, long parentDeptId, Integer parentSysDeptId) throws ApiException {
|
|
|
+ public String getDepartmentList(Integer companyId, String corpid, String access_token, long parentDeptId, Integer parentSysDeptId) throws ApiException {
|
|
|
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
|
|
|
OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest();
|
|
|
req.setDeptId(parentDeptId);
|
|
|
req.setLanguage("zh_CN");
|
|
|
OapiV2DepartmentListsubResponse rsp = client.execute(req, access_token);
|
|
|
- System.out.println(rsp.getBody());
|
|
|
+ System.out.println("获取部门返回:"+rsp.getBody());
|
|
|
JSONObject json = JSONObject.parseObject(rsp.getBody());
|
|
|
if (json.getInteger("errcode") == 0) {
|
|
|
//正确返回的情况
|
|
@@ -352,7 +457,10 @@ public class DingDingServiceImpl implements DingDingService {
|
|
|
}
|
|
|
//获取部门下的人员列表
|
|
|
getDeptUserIdList(companyId, parentDeptId, parentSysDeptId, access_token);
|
|
|
+ } else {
|
|
|
+ return json.toJSONString();
|
|
|
}
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
//获取部门下的人员ids
|
|
@@ -361,7 +469,7 @@ public class DingDingServiceImpl implements DingDingService {
|
|
|
OapiUserListidRequest req = new OapiUserListidRequest();
|
|
|
req.setDeptId(ddDeptId);
|
|
|
OapiUserListidResponse rsp = client.execute(req, access_token);
|
|
|
- System.out.println(rsp.getBody());
|
|
|
+ System.out.println("获取人员返回:"+rsp.getBody());
|
|
|
JSONObject resp = JSONObject.parseObject(rsp.getBody());
|
|
|
if (resp.getInteger("errcode") == 0) {
|
|
|
JSONArray jsonArray = resp.getJSONObject("result").getJSONArray("userid_list");
|