|
@@ -0,0 +1,115 @@
|
|
|
+package com.jeeplus.test.dingding.controller;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.dingtalk.api.DefaultDingTalkClient;
|
|
|
+import com.dingtalk.api.DingTalkClient;
|
|
|
+import com.dingtalk.api.request.OapiGettokenRequest;
|
|
|
+import com.dingtalk.api.request.OapiV2DepartmentListsubRequest;
|
|
|
+import com.dingtalk.api.request.OapiV2UserListRequest;
|
|
|
+import com.dingtalk.api.response.OapiGettokenResponse;
|
|
|
+import com.dingtalk.api.response.OapiV2DepartmentListsubResponse;
|
|
|
+import com.dingtalk.api.response.OapiV2UserListResponse;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.jeeplus.logging.annotation.ApiLog;
|
|
|
+import com.jeeplus.test.grid.service.dto.TestContinentDTO;
|
|
|
+import com.taobao.api.ApiException;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@RestController
|
|
|
+@RequestMapping(value = "/test/addressBook")
|
|
|
+public class DingdingAddressBookController {
|
|
|
+
|
|
|
+ @Value("dingtk0peibzdlxdmojz")
|
|
|
+ private String appKey;
|
|
|
+
|
|
|
+ @Value("NWFAu2fcfKlW3GxlvrGEwMjvJ7jyCPq0Q70XzptUsxE5PYs9cx5wTBWL0wjxKBtf")
|
|
|
+ private String appSecret;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取token
|
|
|
+ */
|
|
|
+// @ApiLog("获取token")
|
|
|
+// @ApiOperation(value = "获取token")
|
|
|
+// @GetMapping("getToken")
|
|
|
+ public String getToken() throws ApiException {
|
|
|
+ // 创建钉钉客户端
|
|
|
+ DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
|
|
|
+ // 配置请求参数
|
|
|
+ OapiGettokenRequest req = new OapiGettokenRequest();
|
|
|
+ req.setAppkey(appKey); // 确保 appKey 已正确初始化
|
|
|
+ req.setAppsecret(appSecret); // 确保 appSecret 已正确初始化
|
|
|
+ req.setHttpMethod("GET");
|
|
|
+ // 执行请求,获取响应
|
|
|
+ OapiGettokenResponse rsp = client.execute(req);
|
|
|
+ String accessToken = rsp.getAccessToken();
|
|
|
+ return accessToken;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取用户列表
|
|
|
+ */
|
|
|
+ @ApiLog("获取用户列表")
|
|
|
+ @ApiOperation(value = "获取用户列表")
|
|
|
+ @GetMapping("list")
|
|
|
+ public ResponseEntity list(Long deptId) {
|
|
|
+ try {
|
|
|
+ String token = getToken();
|
|
|
+ DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/list");
|
|
|
+ OapiV2UserListRequest req = new OapiV2UserListRequest();
|
|
|
+ // 设置部门 ID(传递具体部门 ID 值)
|
|
|
+ req.setDeptId(deptId); // 替换为你需要的部门 ID
|
|
|
+ // 设置分页参数(可选)
|
|
|
+ req.setCursor(0L); // 起始点
|
|
|
+ req.setSize(50L); // 每页返回数量,建议不要超过 50
|
|
|
+ OapiV2UserListResponse rsp = client.execute(req, token);
|
|
|
+ // 返回 JSON 响应
|
|
|
+ return ResponseEntity.ok(rsp);
|
|
|
+ } catch (ApiException e) {
|
|
|
+ e.printStackTrace(); // 打印错误日志(开发调试用)
|
|
|
+ return ResponseEntity.badRequest ( ).body ( "接口异常,请重试!" );
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace(); // 打印错误日志(开发调试用)
|
|
|
+ return ResponseEntity.badRequest ( ).body ( "接口异常,请重试!" );
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取部门列表
|
|
|
+ */
|
|
|
+ @ApiLog("获取部门列表")
|
|
|
+ @ApiOperation(value = "获取部门列表")
|
|
|
+ @GetMapping("deptList")
|
|
|
+ public ResponseEntity deptList() {
|
|
|
+ try {
|
|
|
+ String token = getToken();
|
|
|
+ DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
|
|
|
+ OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest();
|
|
|
+ OapiV2DepartmentListsubResponse rsp = client.execute(req, token);
|
|
|
+ // 返回 JSON 响应
|
|
|
+ return ResponseEntity.ok(rsp);
|
|
|
+ } catch (ApiException e) {
|
|
|
+ e.printStackTrace(); // 打印错误日志(开发调试用)
|
|
|
+ return ResponseEntity.badRequest ( ).body ( "接口异常,请重试!" );
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace(); // 打印错误日志(开发调试用)
|
|
|
+ return ResponseEntity.badRequest ( ).body ( "接口异常,请重试!" );
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public <T> T jsonToObject(String json, Class<T> clazz) {
|
|
|
+ try {
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+ return objectMapper.readValue(json, clazz);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("Error converting JSON to Object: " + e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|