|
@@ -0,0 +1,98 @@
|
|
|
+package org.springblade.dingding.service.impl;
|
|
|
+
|
|
|
+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.stereotype.Service;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+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";
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public MeetingVo getMeetingInfo() {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ //解析json字符串
|
|
|
+ private String parseFromJson(String json,String key) {
|
|
|
+ try {
|
|
|
+ JsonObject jsonObject = JsonParser.parseString(json).getAsJsonObject();
|
|
|
+ return jsonObject.get(key).getAsString();
|
|
|
+ } catch (Exception e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取AccessToken
|
|
|
+ public String getAccessToken() {
|
|
|
+ // 实现获取AccessToken逻辑
|
|
|
+ String url = DINGTALK_API_BASE + "oauth2/accessToken";
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ // 构造请求体
|
|
|
+ String requestBody = "{\n" +
|
|
|
+ " \"appKey\": \"" + APP_KEY + "\",\n" +
|
|
|
+ " \"appSecret\": \"" + APP_SECRET + "\"\n" +
|
|
|
+ "}";
|
|
|
+ HttpEntity<String> entity = new HttpEntity<>(requestBody, headers);
|
|
|
+ try {
|
|
|
+ ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class);
|
|
|
+ return parseFromJson(response.getBody(), "accessToken");
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new ServiceException("获取AccessToken失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取unionId 操作人id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String getUnionId(String accessToken){
|
|
|
+ String url = "https://oapi.dingtalk.com/topapi/v2/user/get?access_token="+accessToken;
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ Map<String, String> requestBody = new HashMap<>();
|
|
|
+ requestBody.put("language", "zh_CN");
|
|
|
+ requestBody.put("userid", "11664747591221614");
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+ try {
|
|
|
+ String jsonString = objectMapper.writeValueAsString(requestBody);
|
|
|
+ HttpEntity<String> entity = new HttpEntity<>(jsonString, headers);
|
|
|
+ ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class);
|
|
|
+ return parseFromJson(response.getBody(), "unionId");
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new ServiceException("获取unionId失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|