package com.project.ding.utils; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.github.tingyugetc520.ali.dingtalk.api.DtService; import com.github.tingyugetc520.ali.dingtalk.bean.department.DtDepart; import com.google.gson.JsonObject; import com.jayway.jsonpath.JsonPath; import com.project.ding.domain.dto.DingUserDTO; import io.vavr.control.Try; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; @Component public class DingUtil { @Autowired private DtService dtService; public List getAllDepartment() throws Exception { return dtService.getDepartmentService().list(null , true); } public List getAllUser() throws Exception { List list = getAllDepartment(); List userList = new ArrayList<>(); for (int i = 0; i < list.size(); i++) { List userInDepartment = getUserIdInDepartment(list.get(i).getId()); userList.addAll(userInDepartment); } return userList; } public List getUserIdInDepartment(Long id) throws Exception { List res = new ArrayList<>(); JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("dept_id" , id); jsonObject.addProperty("cursor" , 0); jsonObject.addProperty("size" , 100); String url = dtService.getDtConfigStorage().getApiUrl("/topapi/v2/user/list"); String responseContent = dtService.post(url, jsonObject); res.addAll(Try.of(() -> new ObjectMapper().convertValue( JsonPath.read(responseContent, "$.result.list"), new TypeReference>() {})).getOrElse(new ArrayList<>())); Boolean hasMore = Try.of(() -> new ObjectMapper().convertValue( JsonPath.read(responseContent, "$.result.has_more"), new TypeReference() {})).getOrElse(Boolean.FALSE); while (hasMore) { jsonObject.addProperty("cursor" , new ObjectMapper().convertValue( JsonPath.read(responseContent, "$.result.next_cursor"), new TypeReference() {})); String nextResponseContent = dtService.post(url, jsonObject); res.addAll(Try.of(() -> new ObjectMapper().convertValue( JsonPath.read(nextResponseContent, "$.result.list"), new TypeReference>() {})).getOrElse(new ArrayList<>())); hasMore = Try.of(() -> new ObjectMapper().convertValue( JsonPath.read(nextResponseContent, "$.result.has_more"), new TypeReference() {})).getOrElse(Boolean.FALSE); } return res; } }