|
@@ -1,20 +1,26 @@
|
|
package com.management.platform.service.impl;
|
|
package com.management.platform.service.impl;
|
|
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
+import com.google.gson.JsonObject;
|
|
import com.management.platform.entity.FeishuInfo;
|
|
import com.management.platform.entity.FeishuInfo;
|
|
import com.management.platform.mapper.FeishuInfoMapper;
|
|
import com.management.platform.mapper.FeishuInfoMapper;
|
|
import com.management.platform.service.FeishuInfoService;
|
|
import com.management.platform.service.FeishuInfoService;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
+import com.management.platform.util.HttpRespMsg;
|
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
import org.springframework.http.*;
|
|
import org.springframework.http.*;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.util.StringUtils;
|
|
import org.springframework.web.client.RestTemplate;
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
import java.time.LocalDateTime;
|
|
import java.time.LocalDateTime;
|
|
import java.time.ZoneOffset;
|
|
import java.time.ZoneOffset;
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.time.format.DateTimeFormatter;
|
|
-import java.util.ArrayList;
|
|
|
|
-import java.util.Arrays;
|
|
|
|
-import java.util.List;
|
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
/**
|
|
* <p>
|
|
* <p>
|
|
@@ -32,6 +38,20 @@ public class FeishuInfoServiceImpl extends ServiceImpl<FeishuInfoMapper, FeishuI
|
|
public static final String GET_APP_ACCESS_TOKEN= "https://open.feishu.cn/open-apis/auth/v3/app_access_token/internal";
|
|
public static final String GET_APP_ACCESS_TOKEN= "https://open.feishu.cn/open-apis/auth/v3/app_access_token/internal";
|
|
/*自建应用获取 user_access_token*/
|
|
/*自建应用获取 user_access_token*/
|
|
public static final String GET_USER_ACCESS_TOKEN= "https://open.feishu.cn/open-apis/authen/v1/access_token";
|
|
public static final String GET_USER_ACCESS_TOKEN= "https://open.feishu.cn/open-apis/authen/v1/access_token";
|
|
|
|
+ /*获取企业信息*/
|
|
|
|
+ public static final String GET_CORP_INFO="https://open.feishu.cn/open-apis/tenant/v2/tenant/query";
|
|
|
|
+ /*获取通讯录授权范围*/
|
|
|
|
+ public static final String GET_AVAILABLE_RANGE="https://open.feishu.cn/open-apis/contact/v3/scopes";
|
|
|
|
+ /*获取子部门列表*/
|
|
|
|
+ public static final String GET_SUB_DEPARTMENT_LIST="https://open.feishu.cn/open-apis/contact/v3/departments/:department_id/children";
|
|
|
|
+ /*获取部门直属用户列表*/
|
|
|
|
+ public static final String GET_USER_LIST="https://open.feishu.cn/open-apis/contact/v3/users/find_by_department";
|
|
|
|
+ /*获取单个部门信息*/
|
|
|
|
+ public static final String GET_DEPARTMENT_INFO="https://open.feishu.cn/open-apis/contact/v3/departments/:department_id";
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ FeishuInfoMapper feishuInfoMapper;
|
|
|
|
|
|
public String getAppAccessToken(FeishuInfo feishuInfo){
|
|
public String getAppAccessToken(FeishuInfo feishuInfo){
|
|
String result="";
|
|
String result="";
|
|
@@ -40,7 +60,6 @@ public class FeishuInfoServiceImpl extends ServiceImpl<FeishuInfoMapper, FeishuI
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
|
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
|
headers.setContentType(type);
|
|
headers.setContentType(type);
|
|
- headers.add("Accept", MediaType.APPLICATION_JSON.toString());
|
|
|
|
JSONObject requestMap = new JSONObject();
|
|
JSONObject requestMap = new JSONObject();
|
|
requestMap.put("app_id",feishuInfo.getAppId());
|
|
requestMap.put("app_id",feishuInfo.getAppId());
|
|
requestMap.put("app_secret",feishuInfo.getAppSecret());
|
|
requestMap.put("app_secret",feishuInfo.getAppSecret());
|
|
@@ -50,7 +69,202 @@ public class FeishuInfoServiceImpl extends ServiceImpl<FeishuInfoMapper, FeishuI
|
|
String resp = ResponseEntity.getBody();
|
|
String resp = ResponseEntity.getBody();
|
|
JSONObject respJson = JSONObject.parseObject(resp);
|
|
JSONObject respJson = JSONObject.parseObject(resp);
|
|
if (respJson.getInteger("code")==0){
|
|
if (respJson.getInteger("code")==0){
|
|
- result+=respJson.getString("app_access_token");
|
|
|
|
|
|
+ result=respJson.getString("app_access_token");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public String getTenantAccessToken(String appId){
|
|
|
|
+ String result="";
|
|
|
|
+ FeishuInfo feishuInfo = getOne(new QueryWrapper<FeishuInfo>().eq("app_id", appId));
|
|
|
|
+ if(feishuInfo!=null){
|
|
|
|
+ if(feishuInfo.getExpireTime().isBefore(LocalDateTime.now())){
|
|
|
|
+ String url = GET_TENANT_ACCESS_TOKEN;
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
|
+ MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
|
|
|
+ headers.setContentType(type);
|
|
|
|
+ JSONObject requestMap = new JSONObject();
|
|
|
|
+ requestMap.put("app_id",feishuInfo.getAppId());
|
|
|
|
+ requestMap.put("app_secret",feishuInfo.getAppSecret());
|
|
|
|
+ HttpEntity<JSONObject> entity = new HttpEntity<>(requestMap, headers);
|
|
|
|
+ ResponseEntity<String> ResponseEntity = restTemplate.postForEntity(url, entity, String.class);
|
|
|
|
+ if (ResponseEntity.getStatusCode() == HttpStatus.OK) {
|
|
|
|
+ String resp = ResponseEntity.getBody();
|
|
|
|
+ JSONObject respJson = JSONObject.parseObject(resp);
|
|
|
|
+ if (respJson.getInteger("code")==0){
|
|
|
|
+ result=respJson.getString("tenant_access_token");
|
|
|
|
+ feishuInfo.setAccessToken(result);
|
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
|
+ now = now.plusSeconds(7200);
|
|
|
|
+ feishuInfo.setExpireTime(now);
|
|
|
|
+ feishuInfoMapper.updateById(feishuInfo);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public JSONObject getCorpInfo(FeishuInfo feishuInfo){
|
|
|
|
+ JSONObject result=new JSONObject();
|
|
|
|
+ String url = GET_CORP_INFO;
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
|
+ MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
|
|
|
+ headers.setContentType(type);
|
|
|
|
+ headers.add("Authorization","Bearer "+feishuInfo.getAccessToken());
|
|
|
|
+ HttpEntity<JSONObject> httpEntity = new HttpEntity<>(null, headers);
|
|
|
|
+ ResponseEntity<String> ResponseEntity = restTemplate.exchange(url,HttpMethod.GET,httpEntity,String.class);
|
|
|
|
+ if (ResponseEntity.getStatusCode() == HttpStatus.OK) {
|
|
|
|
+ String resp = ResponseEntity.getBody();
|
|
|
|
+ JSONObject respJson = JSONObject.parseObject(resp);
|
|
|
|
+ if (respJson.getInteger("code")==0){
|
|
|
|
+ JSONObject data = respJson.getJSONObject("data");
|
|
|
|
+ result=data.getJSONObject("tenant");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public JSONArray getAvailableRange(FeishuInfo feishuInfo,String pageToken){
|
|
|
|
+ JSONArray result=new JSONArray();
|
|
|
|
+ String url = GET_AVAILABLE_RANGE;
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
|
+ MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
|
|
|
+ headers.setContentType(type);
|
|
|
|
+ headers.add("Authorization","Bearer "+feishuInfo.getAccessToken());
|
|
|
|
+ HttpEntity<JSONObject> httpEntity = new HttpEntity<>(null, headers);
|
|
|
|
+ Map<String,Object> map=new HashMap<>();
|
|
|
|
+ map.put("page_size",50);
|
|
|
|
+ if(pageToken!=null){
|
|
|
|
+ map.put("page_token",pageToken);
|
|
|
|
+ }
|
|
|
|
+ ResponseEntity<String> ResponseEntity = restTemplate.exchange(url,HttpMethod.GET,httpEntity,String.class,map);
|
|
|
|
+ if (ResponseEntity.getStatusCode() == HttpStatus.OK) {
|
|
|
|
+ String resp = ResponseEntity.getBody();
|
|
|
|
+ JSONObject respJson = JSONObject.parseObject(resp);
|
|
|
|
+ if (respJson.getInteger("code")==0){
|
|
|
|
+ JSONObject data = respJson.getJSONObject("data");
|
|
|
|
+ JSONArray departmentIds = data.getJSONArray("department_ids");
|
|
|
|
+ if(departmentIds!=null&&departmentIds.size()>0){
|
|
|
|
+ result.addAll(departmentIds);
|
|
|
|
+ }
|
|
|
|
+ if(data.getBoolean("has_more")){
|
|
|
|
+ JSONArray array = getAvailableRange(feishuInfo, data.getString("page_token"));
|
|
|
|
+ if(array!=null&&array.size()>0){
|
|
|
|
+ result.addAll(array);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public JSONArray getSubDepartmentList(FeishuInfo feishuInfo, String departmentId, String pageToken){
|
|
|
|
+ JSONArray result=new JSONArray();
|
|
|
|
+ String url = GET_SUB_DEPARTMENT_LIST.replace(":department_id",departmentId);
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
|
+ MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
|
|
|
+ headers.setContentType(type);
|
|
|
|
+ headers.add("Authorization","Bearer "+feishuInfo.getAccessToken());
|
|
|
|
+ HttpEntity<JSONObject> httpEntity = new HttpEntity<>(null, headers);
|
|
|
|
+ Map<String,Object> map=new HashMap<>();
|
|
|
|
+ map.put("fetch_child",0);
|
|
|
|
+ map.put("page_size",50);
|
|
|
|
+ if(pageToken!=null){
|
|
|
|
+ map.put("page_token",pageToken);
|
|
|
|
+ }
|
|
|
|
+ ResponseEntity<String> ResponseEntity = restTemplate.exchange(url,HttpMethod.GET,httpEntity,String.class,map);
|
|
|
|
+ if (ResponseEntity.getStatusCode() == HttpStatus.OK) {
|
|
|
|
+ String resp = ResponseEntity.getBody();
|
|
|
|
+ System.out.println(resp);
|
|
|
|
+ JSONObject respJson = JSONObject.parseObject(resp);
|
|
|
|
+ if (respJson.getInteger("code")==0){
|
|
|
|
+ JSONObject data = respJson.getJSONObject("data");
|
|
|
|
+ JSONArray items = data.getJSONArray("items");
|
|
|
|
+ if(items!=null&&items.size()>0){
|
|
|
|
+ result.addAll(items);
|
|
|
|
+ }
|
|
|
|
+ if(data.getBoolean("has_more")){
|
|
|
|
+ JSONArray array = getSubDepartmentList(feishuInfo, departmentId, data.getString("page_token"));
|
|
|
|
+ if(array!=null&&array.size()>0){
|
|
|
|
+ result.addAll(array);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public JSONArray getDepartmentInfo(FeishuInfo feishuInfo, String departmentId){
|
|
|
|
+ JSONArray result=new JSONArray();
|
|
|
|
+ String url = GET_DEPARTMENT_INFO.replace(":department_id",departmentId);
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
|
+ MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
|
|
|
+ headers.setContentType(type);
|
|
|
|
+ headers.add("Authorization","Bearer "+feishuInfo.getAccessToken());
|
|
|
|
+ HttpEntity<JSONObject> httpEntity = new HttpEntity<>(null, headers);
|
|
|
|
+ ResponseEntity<String> ResponseEntity = restTemplate.exchange(url,HttpMethod.GET,httpEntity,String.class);
|
|
|
|
+ if (ResponseEntity.getStatusCode() == HttpStatus.OK) {
|
|
|
|
+ String resp = ResponseEntity.getBody();
|
|
|
|
+ System.out.println(resp);
|
|
|
|
+ JSONObject respJson = JSONObject.parseObject(resp);
|
|
|
|
+ if (respJson.getInteger("code")==0){
|
|
|
|
+ JSONObject data = respJson.getJSONObject("data");
|
|
|
|
+ JSONObject items = data.getJSONObject("department");
|
|
|
|
+ if(items!=null){
|
|
|
|
+ result.add(items);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public JSONArray getUserInfoWithDepartment(FeishuInfo feishuInfo, String feishuDeptid, String pageToken) {
|
|
|
|
+ JSONArray result=new JSONArray();
|
|
|
|
+ String url = GET_USER_LIST;
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
|
+ MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
|
|
|
+ headers.setContentType(type);
|
|
|
|
+ headers.add("Authorization","Bearer "+feishuInfo.getAccessToken());
|
|
|
|
+ HttpEntity<JSONObject> httpEntity = new HttpEntity<>(null, headers);
|
|
|
|
+ Map<String,Object> map=new HashMap<>();
|
|
|
|
+ map.put("page_size",50);
|
|
|
|
+ map.put("department_id",feishuDeptid);
|
|
|
|
+ ResponseEntity<String> ResponseEntity;
|
|
|
|
+ if(pageToken!=null){
|
|
|
|
+ map.put("page_token",pageToken);
|
|
|
|
+ ResponseEntity = restTemplate.exchange(url+"?department_id={department_id}&page_size={page_size}&page_token={page_token}",HttpMethod.GET,httpEntity,String.class,map);
|
|
|
|
+ }
|
|
|
|
+ ResponseEntity = restTemplate.exchange(url+"?department_id={department_id}&page_size={page_size}",HttpMethod.GET,httpEntity,String.class,map);
|
|
|
|
+ if (ResponseEntity.getStatusCode() == HttpStatus.OK) {
|
|
|
|
+ String resp = ResponseEntity.getBody();
|
|
|
|
+ JSONObject respJson = JSONObject.parseObject(resp);
|
|
|
|
+ if (respJson.getInteger("code")==0){
|
|
|
|
+ JSONObject data = respJson.getJSONObject("data");
|
|
|
|
+ JSONArray items = data.getJSONArray("items");
|
|
|
|
+ if(items!=null&&items.size()>0){
|
|
|
|
+ result.addAll(items);
|
|
|
|
+ }
|
|
|
|
+ if(data.getBoolean("has_more")){
|
|
|
|
+ JSONArray array = getUserInfoWithDepartment(feishuInfo, feishuDeptid, data.getString("page_token"));
|
|
|
|
+ if(array!=null&&array.size()>0){
|
|
|
|
+ result.addAll(array);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
return result;
|