|
@@ -1,31 +1,79 @@
|
|
|
package org.springblade.dingding.service.impl;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.alibaba.nacos.shaded.com.google.gson.JsonObject;
|
|
|
import com.alibaba.nacos.shaded.com.google.gson.JsonParser;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import org.springblade.core.log.exception.ServiceException;
|
|
|
import org.springblade.dingding.service.MeetingService;
|
|
|
import org.springblade.dingding.vo.MeetingVo;
|
|
|
-import org.springframework.http.HttpEntity;
|
|
|
-import org.springframework.http.HttpHeaders;
|
|
|
-import org.springframework.http.MediaType;
|
|
|
-import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.http.*;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.client.HttpClientErrorException;
|
|
|
+import org.springframework.web.client.HttpServerErrorException;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
+import org.springframework.web.util.UriComponentsBuilder;
|
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.LocalTime;
|
|
|
+import java.time.ZoneId;
|
|
|
+import java.time.ZonedDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@Service
|
|
|
public class MeetingServiceImpl implements MeetingService {
|
|
|
|
|
|
private static final String DINGTALK_API_BASE = "https://api.dingtalk.com/v1.0/";
|
|
|
- private static final String APP_KEY = "dingbqbwen1t2qixpj7g";
|
|
|
- private static final String APP_SECRET = "F_56XGS03bSCjMWgkdFcdBWtg3V6xsJva4XHmR8qMFmadS9U_3OK_953Zd409O6L";
|
|
|
+ private static final String APP_KEY = "ding6mvrzcxcbdggel0h";
|
|
|
+ private static final String APP_SECRET = "TctTp-Qmyh9r20bdeHcLWZ8qfxSEP8R29qo53GZH2elWV-yDZMqgqFKEjp5PtlXZ";
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public MeetingVo getMeetingInfo() {
|
|
|
+ List<MeetingVo> vos=new ArrayList<>();
|
|
|
+ String accessToken = getAccessToken();
|
|
|
+ String unionId = getUnionId(accessToken);
|
|
|
+ String meetingsJson = getMeetings(accessToken, unionId);
|
|
|
+ try {
|
|
|
+ // 解析JSON字符串
|
|
|
+ com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(meetingsJson);
|
|
|
+ // 获取result数组
|
|
|
+ JSONArray resultArray = jsonObject.getJSONArray("result");
|
|
|
+ // 遍历数组,提取roomId和roomName
|
|
|
+ for (int i = 0; i < resultArray.size(); i++) {
|
|
|
+ MeetingVo vo=new MeetingVo();
|
|
|
+ JSONObject roomObject = resultArray.getJSONObject(i);
|
|
|
+ String roomId = roomObject.getString("roomId");
|
|
|
+ String roomName = roomObject.getString("roomName");
|
|
|
+ vo.setRoomId(roomId);
|
|
|
+ vo.setFixedData(roomName);
|
|
|
+ vos.add(vo);
|
|
|
+ }
|
|
|
+ LocalDateTime startOfDay = LocalDateTime.now().with(LocalTime.MIDNIGHT);
|
|
|
+ ZonedDateTime startOfDayZoned = startOfDay.atZone(ZoneId.systemDefault());
|
|
|
+ String startTime=startOfDayZoned.withZoneSameInstant(ZoneId.of("UTC"))
|
|
|
+ .format(DateTimeFormatter.ISO_INSTANT);
|
|
|
+ LocalDateTime endOfDay = LocalDateTime.now().with(LocalTime.MAX);
|
|
|
+ ZonedDateTime endOfDayZoned = endOfDay.atZone(ZoneId.systemDefault());
|
|
|
+ String endTime= endOfDayZoned.withZoneSameInstant(ZoneId.of("UTC"))
|
|
|
+ .format(DateTimeFormatter.ISO_INSTANT);
|
|
|
+ for (MeetingVo vo : vos) {
|
|
|
+ String[] roomIds = new String[]{vo.getRoomId()};
|
|
|
+ String meetingHistory = getMeetingHistory(accessToken, unionId,roomIds,startTime,endTime);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
return null;
|
|
|
}
|
|
|
|
|
@@ -71,7 +119,7 @@ public class MeetingServiceImpl implements MeetingService {
|
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
Map<String, String> requestBody = new HashMap<>();
|
|
|
requestBody.put("language", "zh_CN");
|
|
|
- requestBody.put("userid", "11664747591221614");
|
|
|
+ requestBody.put("userid", "01141506681633389467");
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
try {
|
|
|
String jsonString = objectMapper.writeValueAsString(requestBody);
|
|
@@ -83,6 +131,119 @@ public class MeetingServiceImpl implements MeetingService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ //获取会议室列表
|
|
|
+ public String getMeetings(String accessToken,String unionId) {
|
|
|
+ if (accessToken == null || accessToken.isEmpty()) {
|
|
|
+ System.out.println("获取accessToken失败");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // 构建请求URL,包含必要的查询参数
|
|
|
+ String baseUrl = DINGTALK_API_BASE + "rooms/meetingRoomLists";
|
|
|
+ UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl(baseUrl)
|
|
|
+ .queryParam("unionId", unionId);
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ // 设置必要的请求头
|
|
|
+ headers.set("x-acs-dingtalk-access-token", accessToken);
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ HttpEntity<?> entity = new HttpEntity<>(headers);
|
|
|
+ try {
|
|
|
+ // 使用GET方法发送请求
|
|
|
+ ResponseEntity<String> response = restTemplate.exchange(
|
|
|
+ uriBuilder.toUriString(),
|
|
|
+ HttpMethod.GET,
|
|
|
+ entity,
|
|
|
+ String.class
|
|
|
+ );
|
|
|
+ if (response.getStatusCode().is2xxSuccessful()) {
|
|
|
+ return response.getBody();
|
|
|
+ } else {
|
|
|
+ throw new SecurityException("获取会议室列表异常");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new SecurityException("获取会议室列表异常");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取会议室详情
|
|
|
+ public String getMeetingDetail(String accessToken,String roomId,String unionId) {
|
|
|
+ // 构建请求URL(注意是meetingRooms单个资源路径,不是列表接口)
|
|
|
+ String baseUrl = DINGTALK_API_BASE + "rooms/meetingRooms/" + roomId;
|
|
|
+ UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl(baseUrl)
|
|
|
+ .queryParam("unionId", unionId);
|
|
|
+
|
|
|
+ // 创建REST模板并设置请求头
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.set("x-acs-dingtalk-access-token", accessToken);
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ // 添加User-Agent头,模拟浏览器请求
|
|
|
+ headers.set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36");
|
|
|
+ HttpEntity<?> entity = new HttpEntity<>(headers);
|
|
|
+ try {
|
|
|
+ // 发送GET请求
|
|
|
+ ResponseEntity<String> response = restTemplate.exchange(
|
|
|
+ uriBuilder.toUriString(),
|
|
|
+ HttpMethod.GET,
|
|
|
+ entity,
|
|
|
+ String.class
|
|
|
+ );
|
|
|
+ // 处理响应
|
|
|
+ if (response.getStatusCode().is2xxSuccessful()) {
|
|
|
+ return response.getBody();
|
|
|
+ } else {
|
|
|
+ throw new ServiceException("获取会议室详情异常");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new ServiceException("获取会议室详情失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public String getMeetingHistory(String accessToken, String unionId,String[] roomIds, String startTime, String endTime) {
|
|
|
+ // 构建请求 URL,包含 Path 参数 userId
|
|
|
+ String baseUrl = DINGTALK_API_BASE + "calendar/users/{userId}/meetingRooms/schedules/query";
|
|
|
+ Map<String, Object> paramMap = new HashMap<>();
|
|
|
+ paramMap.put("userId", unionId);
|
|
|
+ UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl(baseUrl)
|
|
|
+ .uriVariables(paramMap);
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ // 设置请求头
|
|
|
+ headers.set("x-acs-dingtalk-access-token", accessToken);
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ // 构建请求体参数(Body 参数)
|
|
|
+ Map<String, Object> requestBody = new HashMap<>();
|
|
|
+ // 替换为实际的会议室 roomId 列表,建议不超过 5 个
|
|
|
+ requestBody.put("roomIds", roomIds);
|
|
|
+ // 替换为实际的查询开始时间,需符合 iso8601 格式,如 "2025-08-30T00:00:00Z"
|
|
|
+ requestBody.put("startTime", startTime);
|
|
|
+ // 替换为实际的查询结束时间,需符合 iso8601 格式,如 "2025-08-31T00:00:00Z"
|
|
|
+ requestBody.put("endTime", endTime);
|
|
|
+ HttpEntity<Map<String, Object>> entity = new HttpEntity<>(requestBody, headers);
|
|
|
+ try {
|
|
|
+ // 使用 POST 方法发送请求
|
|
|
+ ResponseEntity<String> response = restTemplate.exchange(
|
|
|
+ uriBuilder.toUriString(),
|
|
|
+ HttpMethod.POST,
|
|
|
+ entity,
|
|
|
+ String.class
|
|
|
+ );
|
|
|
+ if (response.getStatusCode().is2xxSuccessful()) {
|
|
|
+ return response.getBody();
|
|
|
+ } else {
|
|
|
+ throw new ServiceException("获取会议室历史异常");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new ServiceException("获取会议室历史异常");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
|
|
|
|