DingUserServiceImpl.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. package com.zjugis.iebps.service.impl;
  2. import cn.hutool.core.util.ObjectUtil;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.alibaba.xxpt.gateway.shared.api.request.*;
  5. import com.alibaba.xxpt.gateway.shared.api.response.*;
  6. import com.alibaba.xxpt.gateway.shared.client.http.*;
  7. import com.zjugis.iebps.beans.dataobject.DingUser;
  8. import com.zjugis.iebps.exception.BusinessException;
  9. import com.zjugis.iebps.service.IDingUserService;
  10. import org.apache.http.HttpEntity;
  11. import org.apache.http.client.methods.CloseableHttpResponse;
  12. import org.springframework.beans.factory.annotation.Value;
  13. import org.springframework.stereotype.Service;
  14. import org.springframework.util.FileCopyUtils;
  15. import org.springframework.util.StringUtils;
  16. import org.springframework.web.multipart.MultipartFile;
  17. import javax.annotation.PostConstruct;
  18. import javax.imageio.ImageIO;
  19. import javax.servlet.http.HttpServletResponse;
  20. import java.awt.*;
  21. import java.awt.image.BufferedImage;
  22. import java.io.File;
  23. import java.io.FileInputStream;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.nio.charset.StandardCharsets;
  27. import java.util.HashMap;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.UUID;
  31. @Service
  32. public class DingUserServiceImpl implements IDingUserService {
  33. @Value("${ding.appKey}")
  34. private String appkey;
  35. @Value("${ding.appSecret}")
  36. private String appSecret;
  37. @Value("${ding.domainName}")
  38. private String domainName;
  39. @Value("${ding.tenantId}")
  40. private String tenantId;
  41. @Value("${ding.comAppKey}")
  42. private String comAppKey;
  43. @Value("${ding.comAppSecret}")
  44. private String comAppSecret;
  45. @Value("${ding.compressNum}")
  46. private Integer compressNum;
  47. private ExecutableClient executableClient;
  48. @PostConstruct
  49. public void initExecutableClient(){
  50. executableClient = ExecutableClient.getInstance();
  51. executableClient.setAccessKey(comAppKey);
  52. executableClient.setSecretKey(comAppSecret);
  53. executableClient.setDomainName(domainName);
  54. executableClient.setProtocal("https");
  55. executableClient.init();
  56. }
  57. @Override
  58. public Map getOrganizationByCode(String xzqdm) {
  59. Map map = new HashMap<>();
  60. String api = "/mozi/organization/getOrganizationByCode";
  61. PostClient postClient = executableClient.newPostClient(api);
  62. //Set the parameters
  63. postClient.addParameter("organizationCode", xzqdm);
  64. postClient.addParameter("tenantId", tenantId);
  65. //Call API
  66. String apiResult = postClient.post();
  67. JSONObject content = JSONObject.parseObject(apiResult).getJSONObject("content");
  68. if (!content.getBoolean("success")) {
  69. throw new BusinessException(content.getString("responseMessage"));
  70. }
  71. map.put("data",content.get("data"));
  72. executableClient.destroy();
  73. return map;
  74. }
  75. @Override
  76. public Map getEmployeeByCode(String employeeCode) {
  77. Map map = new HashMap<>();
  78. String api = "/mozi/employee/getEmployeeByCode";
  79. IntelligentGetClient intelligentGetClient = executableClient.newIntelligentGetClient(api);
  80. OapiMoziEmployeeGetEmployeeByCodeRequest oapiMoziEmployeeGetEmployeeByCodeRequest = new OapiMoziEmployeeGetEmployeeByCodeRequest();
  81. oapiMoziEmployeeGetEmployeeByCodeRequest.setEmployeeCode(employeeCode);
  82. oapiMoziEmployeeGetEmployeeByCodeRequest.setTenantId(Long.valueOf(tenantId));
  83. //获取结果
  84. OapiMoziEmployeeGetEmployeeByCodeResponse apiResult = intelligentGetClient.get(oapiMoziEmployeeGetEmployeeByCodeRequest);
  85. map.put("data",apiResult);
  86. executableClient.destroy();
  87. return map;
  88. }
  89. @Override
  90. public Map scopesV2() {
  91. Map map = new HashMap<>();
  92. String api = "/auth/scopesV2";
  93. PostClient postClient = executableClient.newPostClient(api);
  94. postClient.addParameter("tenantId", tenantId);
  95. String apiResult = postClient.post();
  96. map.put("data",apiResult);
  97. executableClient.destroy();
  98. return map;
  99. }
  100. @Override
  101. public Map workNotification(String receiverIds, String msg, String organizationCodes) {
  102. Map map = new HashMap<>();
  103. String api = "/message/workNotification";
  104. PostClient postClient = executableClient.newPostClient(api);
  105. if (!StringUtils.isEmpty(organizationCodes)){
  106. postClient.addParameter("organizationCodes", organizationCodes);
  107. }
  108. postClient.addParameter("receiverIds", receiverIds);
  109. postClient.addParameter("tenantId", tenantId);
  110. postClient.addParameter("bizMsgId", UUID.randomUUID().toString());
  111. postClient.addParameter("msg", msg);
  112. String apiResult = postClient.post();
  113. System.out.println(apiResult);
  114. map.put("data",apiResult);
  115. executableClient.destroy();
  116. return map;
  117. }
  118. public Map sendDingMessageByText(String receiverIds, String content) {
  119. Map map = new HashMap<>();
  120. //executableClient保证单例
  121. String api = "/message/workNotification";
  122. IntelligentGetClient intelligentGetClient = executableClient.newIntelligentGetClient(api);
  123. OapiMessageWorkNotificationRequest oapiMessageWorkNotificationRequest = new OapiMessageWorkNotificationRequest();
  124. //接收人用户ID
  125. oapiMessageWorkNotificationRequest.setReceiverIds(receiverIds);
  126. //租户ID
  127. oapiMessageWorkNotificationRequest.setTenantId(tenantId);
  128. //业务消息id
  129. oapiMessageWorkNotificationRequest.setBizMsgId(UUID.randomUUID().toString());
  130. //消息对象
  131. Map<String,Object> msg = new HashMap<>();
  132. msg.put("msgtype","text");
  133. Map<String,Object> text = new HashMap<>();
  134. text.put("content",content);
  135. msg.put("text",text);
  136. oapiMessageWorkNotificationRequest.setMsg(JSONObject.toJSONString(msg));
  137. System.out.println("apiResult Start---------------------------------------");
  138. //获取结果
  139. OapiMessageWorkNotificationResponse apiResult = intelligentGetClient.get(oapiMessageWorkNotificationRequest);
  140. System.out.println("apiResult---------------------------------------");
  141. System.out.println(apiResult);
  142. map.put("data",apiResult);
  143. executableClient.destroy();
  144. return map;
  145. }
  146. //查询组织人员Code
  147. @Override
  148. public Object queryOrganizationEmployeeCodes(String organizationCode, Integer pageNo){
  149. String api = "/mozi/organization/pageOrganizationEmployeeCodes";
  150. IntelligentGetClient intelligentGetClient = executableClient.newIntelligentGetClient(api);
  151. OapiMoziOrganizationPageOrganizationEmployeeCodesRequest oapiMoziOrganizationPageOrganizationEmployeeCodesRequest = new OapiMoziOrganizationPageOrganizationEmployeeCodesRequest();
  152. //null
  153. oapiMoziOrganizationPageOrganizationEmployeeCodesRequest.setReturnTotalSize(true);
  154. oapiMoziOrganizationPageOrganizationEmployeeCodesRequest.setPageSize(20);
  155. oapiMoziOrganizationPageOrganizationEmployeeCodesRequest.setOrganizationCode(organizationCode);
  156. oapiMoziOrganizationPageOrganizationEmployeeCodesRequest.setPageNo(pageNo);
  157. oapiMoziOrganizationPageOrganizationEmployeeCodesRequest.setEmployeePositionStatus("TOTAL");
  158. oapiMoziOrganizationPageOrganizationEmployeeCodesRequest.setTenantId(Long.valueOf(tenantId));
  159. OapiMoziOrganizationPageOrganizationEmployeeCodesResponse apiResult = intelligentGetClient.get(oapiMoziOrganizationPageOrganizationEmployeeCodesRequest);
  160. System.out.println("queryOrganizationEmployeeCodes~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  161. System.out.println(tenantId);
  162. System.out.println(apiResult.getContent().getData());
  163. System.out.println(JSONObject.toJSON(apiResult));
  164. executableClient.destroy();
  165. return JSONObject.toJSON(apiResult);
  166. }
  167. //批量查询人员详情根据人员Code
  168. @Override
  169. public Object queryListEmployeesByCodes(List<String> employeeCodes){
  170. String api = "/mozi/employee/listEmployeesByCodes";
  171. IntelligentPostClient intelligentPostClient = executableClient.newIntelligentPostClient(api);
  172. OapiMoziEmployeeListEmployeesByCodesRequest oapiMoziEmployeeListEmployeesByCodesRequest = new OapiMoziEmployeeListEmployeesByCodesRequest();
  173. //null
  174. oapiMoziEmployeeListEmployeesByCodesRequest.setEmployeeCodes(employeeCodes);
  175. oapiMoziEmployeeListEmployeesByCodesRequest.setTenantId(Long.valueOf(tenantId));
  176. OapiMoziEmployeeListEmployeesByCodesResponse apiResult = intelligentPostClient.post(oapiMoziEmployeeListEmployeesByCodesRequest);
  177. System.out.println("queryListEmployeesByCodes~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  178. System.out.println(apiResult.getContent().getData());
  179. System.out.println(JSONObject.toJSON(apiResult));
  180. executableClient.destroy();
  181. return JSONObject.toJSON(apiResult);
  182. }
  183. //查询组织下人员总数
  184. @Override
  185. public Object getOrganizationEmployeeCount(String organizationCode){
  186. //executableClient保证单例
  187. IntelligentGetClient intelligentGetClient = executableClient.newIntelligentGetClient("/mozi/organization/getOrganizationEmployeeCount");
  188. OapiMoziOrganizationGetOrganizationEmployeeCountRequest oapiMoziOrganizationGetOrganizationEmployeeCountRequest = new OapiMoziOrganizationGetOrganizationEmployeeCountRequest();
  189. //组织code
  190. oapiMoziOrganizationGetOrganizationEmployeeCountRequest.setOrganizationCode(organizationCode);
  191. //租户id
  192. oapiMoziOrganizationGetOrganizationEmployeeCountRequest.setTenantId(Long.valueOf(tenantId));
  193. //获取结果
  194. OapiMoziOrganizationGetOrganizationEmployeeCountResponse apiResult = intelligentGetClient.get(oapiMoziOrganizationGetOrganizationEmployeeCountRequest);
  195. System.out.println("getOrganizationEmployeeCount~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  196. System.out.println(apiResult.getContent().getData());
  197. System.out.println(JSONObject.toJSON(apiResult));
  198. executableClient.destroy();
  199. return JSONObject.toJSON(apiResult);
  200. }
  201. //查询组织子孙组织详情
  202. @Override
  203. public Object pageSubOrganizationCodes(String organizationCode, Integer pageNo){
  204. //executableClient保证单例
  205. IntelligentGetClient intelligentGetClient = executableClient.newIntelligentGetClient("/mozi/organization/pageSubOrganizationCodes");
  206. OapiMoziOrganizationPageSubOrganizationCodesRequest oapiMoziOrganizationPageSubOrganizationCodesRequest = new OapiMoziOrganizationPageSubOrganizationCodesRequest();
  207. oapiMoziOrganizationPageSubOrganizationCodesRequest.setReturnTotalSize(true);
  208. oapiMoziOrganizationPageSubOrganizationCodesRequest.setOrganizationCode(organizationCode);
  209. oapiMoziOrganizationPageSubOrganizationCodesRequest.setPageNo(pageNo);
  210. oapiMoziOrganizationPageSubOrganizationCodesRequest.setStatus("TOTAL");
  211. oapiMoziOrganizationPageSubOrganizationCodesRequest.setTenantId(Long.valueOf(tenantId));
  212. //获取结果
  213. OapiMoziOrganizationPageSubOrganizationCodesResponse apiResult = intelligentGetClient.get(oapiMoziOrganizationPageSubOrganizationCodesRequest);
  214. System.out.println("getOrganizationEmployeeCount~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  215. System.out.println(apiResult.getContent().getData());
  216. System.out.println(JSONObject.toJSON(apiResult));
  217. executableClient.destroy();
  218. return JSONObject.toJSON(apiResult);
  219. }
  220. @Override
  221. public JSONObject getAccessToken(String key, String Secret) {
  222. String api = "/gettoken.json";
  223. GetClient getClient = executableClient.newGetClient(api);
  224. //设置参数
  225. getClient.addParameter("appkey", key);
  226. getClient.addParameter("appsecret", Secret);
  227. //调用API
  228. String apiResult = getClient.get();
  229. apiResult = new String(apiResult.getBytes(), StandardCharsets.UTF_8);
  230. System.out.println("getAccessToken~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  231. System.out.println(apiResult);
  232. return JSONObject.parseObject(apiResult);
  233. }
  234. @Override
  235. public JSONObject getUserDing(String accessToken, String authCode) {
  236. String api = "/rpc/oauth2/dingtalk_app_user.json";
  237. PostClient postClient = executableClient.newPostClient(api);
  238. //Set the parameters
  239. postClient.addParameter("access_token", accessToken);
  240. postClient.addParameter("auth_code", authCode);
  241. //Call API
  242. String apiResult = postClient.post();
  243. JSONObject content = JSONObject.parseObject(apiResult);
  244. System.out.println("getAccessToken~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  245. System.out.println(content);
  246. return content;
  247. }
  248. @Override
  249. public void downloadFileByDing(String mediaId, HttpServletResponse response) {
  250. JSONObject jsonObject = this.getAccessToken(appkey, appSecret);
  251. String token = "";
  252. if(
  253. ObjectUtil.isNotNull(jsonObject.getJSONObject("content"))
  254. && ObjectUtil.isNotNull(jsonObject.getJSONObject("content").getJSONObject("data"))
  255. && ObjectUtil.isNotNull(jsonObject.getJSONObject("content").getJSONObject("data").getString("accessToken"))
  256. ){
  257. token = jsonObject.getJSONObject("content").getJSONObject("data").getString("accessToken");
  258. }
  259. if(org.apache.commons.lang3.StringUtils.isNotEmpty(token)){
  260. CloseableHttpResponse closeableHttpResponse = null;
  261. try {
  262. String api = "/media/download";
  263. GetClient getClient = executableClient.newGetClient(api);
  264. getClient.addParameter("access_token", token);
  265. getClient.addParameter("media_id", mediaId);
  266. closeableHttpResponse = getClient.getB();
  267. HttpEntity entity = closeableHttpResponse.getEntity();
  268. InputStream is = entity.getContent();
  269. BufferedImage bufferedImage = compressImage(is, 1200);
  270. ImageIO.write(bufferedImage, "jpg", response.getOutputStream());
  271. } catch (Exception e) {
  272. e.printStackTrace();
  273. }finally {
  274. if (response != null) {
  275. try {
  276. closeableHttpResponse.close();
  277. } catch (Exception e) {
  278. e.printStackTrace();
  279. }
  280. }
  281. }
  282. }
  283. }
  284. @Override
  285. public void downloadFileByTest(MultipartFile file, HttpServletResponse response){
  286. try{
  287. InputStream inputStream = file.getInputStream();
  288. BufferedImage bufferedImage = compressImage(inputStream, 1200);
  289. ImageIO.write(bufferedImage, "jpg", response.getOutputStream());
  290. }catch (IOException ex){
  291. ex.printStackTrace();
  292. }
  293. }
  294. @Override
  295. public Integer getCompressNumByImg() {
  296. return compressNum;
  297. }
  298. private DingUser getTinkerUserDing(String key, String Secret, String code, int codeType) {
  299. String accessToken = getAccessToken(executableClient, key, Secret);
  300. DingUser userDing;
  301. if (codeType == 1) {
  302. userDing = getUserDing(executableClient, accessToken, code);
  303. } else {
  304. userDing = getUserDingByCode(executableClient, accessToken, code);
  305. }
  306. executableClient.destroy();
  307. return userDing;
  308. }
  309. private String getAccessToken(ExecutableClient executableClient, String key, String Secret) {
  310. String api = "/gettoken.json";
  311. GetClient getClient = executableClient.newGetClient(api);
  312. //设置参数
  313. getClient.addParameter("appkey", key);
  314. getClient.addParameter("appsecret", Secret);
  315. //调用API
  316. String apiResult = getClient.get();
  317. apiResult = new String(apiResult.getBytes(), StandardCharsets.UTF_8);
  318. System.out.println(apiResult+"======apiResult");
  319. return JSONObject.parseObject(apiResult).getJSONObject("content").getJSONObject("data").get("accessToken").toString();
  320. }
  321. private DingUser getUserDing(ExecutableClient executableClient, String accessToken, String authCode) {
  322. String api = "/rpc/oauth2/dingtalk_app_user.json";
  323. PostClient postClient = executableClient.newPostClient(api);
  324. //Set the parameters
  325. postClient.addParameter("access_token", accessToken);
  326. postClient.addParameter("auth_code", authCode);
  327. //Call API
  328. String apiResult = postClient.post();
  329. JSONObject content = JSONObject.parseObject(apiResult).getJSONObject("content");
  330. if (!content.getBoolean("success")) {
  331. throw new BusinessException(content.getString("responseMessage"));
  332. }
  333. DingUser data = content.getObject("data", DingUser.class);
  334. System.out.println(data.getAccountId()+"=========AccountId");
  335. return content.getObject("data", DingUser.class);
  336. }
  337. private DingUser getUserDingByCode(ExecutableClient executableClient, String accessToken, String code) {
  338. String api = "/rpc/oauth2/getuserinfo_bycode.json";
  339. PostClient postClient = executableClient.newPostClient(api);
  340. //Set the parameters
  341. postClient.addParameter("access_token", accessToken);
  342. postClient.addParameter("code", code);
  343. //Call API
  344. String apiResult = postClient.post();
  345. JSONObject content = JSONObject.parseObject(apiResult).getJSONObject("content");
  346. if (!content.getBoolean("success")) {
  347. throw new BusinessException(content.getString("responseMessage"));
  348. }
  349. return content.getObject("data", DingUser.class);
  350. }
  351. public static BufferedImage compressImage(InputStream inputStream, int width) throws IOException {
  352. BufferedImage originalImage = ImageIO.read(inputStream);
  353. int nW = originalImage.getWidth(), nH = originalImage.getHeight();
  354. if(nW > width){
  355. nW = width;
  356. nH = nW * originalImage.getHeight() / originalImage.getWidth();
  357. }
  358. Image compressedImage = originalImage.getScaledInstance(nW, nH, Image.SCALE_SMOOTH);
  359. BufferedImage outputImage = new BufferedImage(nW, nH, BufferedImage.TYPE_INT_RGB);
  360. outputImage.createGraphics().drawImage(compressedImage, 0, 0, null);
  361. return outputImage;
  362. }
  363. }