package com.zjugis.iebps.service.impl; import cn.hutool.core.util.ObjectUtil; import com.alibaba.fastjson.JSONObject; import com.alibaba.xxpt.gateway.shared.api.request.*; import com.alibaba.xxpt.gateway.shared.api.response.*; import com.alibaba.xxpt.gateway.shared.client.http.*; import com.zjugis.iebps.beans.dataobject.DingUser; import com.zjugis.iebps.exception.BusinessException; import com.zjugis.iebps.service.IDingUserService; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.util.FileCopyUtils; import org.springframework.util.StringUtils; import org.springframework.web.multipart.MultipartFile; import javax.annotation.PostConstruct; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletResponse; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; @Service public class DingUserServiceImpl implements IDingUserService { @Value("${ding.appKey}") private String appkey; @Value("${ding.appSecret}") private String appSecret; @Value("${ding.domainName}") private String domainName; @Value("${ding.tenantId}") private String tenantId; @Value("${ding.comAppKey}") private String comAppKey; @Value("${ding.comAppSecret}") private String comAppSecret; @Value("${ding.compressNum}") private Integer compressNum; private ExecutableClient executableClient; @PostConstruct public void initExecutableClient(){ executableClient = ExecutableClient.getInstance(); executableClient.setAccessKey(comAppKey); executableClient.setSecretKey(comAppSecret); executableClient.setDomainName(domainName); executableClient.setProtocal("https"); executableClient.init(); } @Override public Map getOrganizationByCode(String xzqdm) { Map map = new HashMap<>(); String api = "/mozi/organization/getOrganizationByCode"; PostClient postClient = executableClient.newPostClient(api); //Set the parameters postClient.addParameter("organizationCode", xzqdm); postClient.addParameter("tenantId", tenantId); //Call API String apiResult = postClient.post(); JSONObject content = JSONObject.parseObject(apiResult).getJSONObject("content"); if (!content.getBoolean("success")) { throw new BusinessException(content.getString("responseMessage")); } map.put("data",content.get("data")); executableClient.destroy(); return map; } @Override public Map getEmployeeByCode(String employeeCode) { Map map = new HashMap<>(); String api = "/mozi/employee/getEmployeeByCode"; IntelligentGetClient intelligentGetClient = executableClient.newIntelligentGetClient(api); OapiMoziEmployeeGetEmployeeByCodeRequest oapiMoziEmployeeGetEmployeeByCodeRequest = new OapiMoziEmployeeGetEmployeeByCodeRequest(); oapiMoziEmployeeGetEmployeeByCodeRequest.setEmployeeCode(employeeCode); oapiMoziEmployeeGetEmployeeByCodeRequest.setTenantId(Long.valueOf(tenantId)); //获取结果 OapiMoziEmployeeGetEmployeeByCodeResponse apiResult = intelligentGetClient.get(oapiMoziEmployeeGetEmployeeByCodeRequest); map.put("data",apiResult); executableClient.destroy(); return map; } @Override public Map scopesV2() { Map map = new HashMap<>(); String api = "/auth/scopesV2"; PostClient postClient = executableClient.newPostClient(api); postClient.addParameter("tenantId", tenantId); String apiResult = postClient.post(); map.put("data",apiResult); executableClient.destroy(); return map; } @Override public Map workNotification(String receiverIds, String msg, String organizationCodes) { Map map = new HashMap<>(); String api = "/message/workNotification"; PostClient postClient = executableClient.newPostClient(api); if (!StringUtils.isEmpty(organizationCodes)){ postClient.addParameter("organizationCodes", organizationCodes); } postClient.addParameter("receiverIds", receiverIds); postClient.addParameter("tenantId", tenantId); postClient.addParameter("bizMsgId", UUID.randomUUID().toString()); postClient.addParameter("msg", msg); String apiResult = postClient.post(); System.out.println(apiResult); map.put("data",apiResult); executableClient.destroy(); return map; } public Map sendDingMessageByText(String receiverIds, String content) { Map map = new HashMap<>(); //executableClient保证单例 String api = "/message/workNotification"; IntelligentGetClient intelligentGetClient = executableClient.newIntelligentGetClient(api); OapiMessageWorkNotificationRequest oapiMessageWorkNotificationRequest = new OapiMessageWorkNotificationRequest(); //接收人用户ID oapiMessageWorkNotificationRequest.setReceiverIds(receiverIds); //租户ID oapiMessageWorkNotificationRequest.setTenantId(tenantId); //业务消息id oapiMessageWorkNotificationRequest.setBizMsgId(UUID.randomUUID().toString()); //消息对象 Map msg = new HashMap<>(); msg.put("msgtype","text"); Map text = new HashMap<>(); text.put("content",content); msg.put("text",text); oapiMessageWorkNotificationRequest.setMsg(JSONObject.toJSONString(msg)); System.out.println("apiResult Start---------------------------------------"); //获取结果 OapiMessageWorkNotificationResponse apiResult = intelligentGetClient.get(oapiMessageWorkNotificationRequest); System.out.println("apiResult---------------------------------------"); System.out.println(apiResult); map.put("data",apiResult); executableClient.destroy(); return map; } //查询组织人员Code @Override public Object queryOrganizationEmployeeCodes(String organizationCode, Integer pageNo){ String api = "/mozi/organization/pageOrganizationEmployeeCodes"; IntelligentGetClient intelligentGetClient = executableClient.newIntelligentGetClient(api); OapiMoziOrganizationPageOrganizationEmployeeCodesRequest oapiMoziOrganizationPageOrganizationEmployeeCodesRequest = new OapiMoziOrganizationPageOrganizationEmployeeCodesRequest(); //null oapiMoziOrganizationPageOrganizationEmployeeCodesRequest.setReturnTotalSize(true); oapiMoziOrganizationPageOrganizationEmployeeCodesRequest.setPageSize(20); oapiMoziOrganizationPageOrganizationEmployeeCodesRequest.setOrganizationCode(organizationCode); oapiMoziOrganizationPageOrganizationEmployeeCodesRequest.setPageNo(pageNo); oapiMoziOrganizationPageOrganizationEmployeeCodesRequest.setEmployeePositionStatus("TOTAL"); oapiMoziOrganizationPageOrganizationEmployeeCodesRequest.setTenantId(Long.valueOf(tenantId)); OapiMoziOrganizationPageOrganizationEmployeeCodesResponse apiResult = intelligentGetClient.get(oapiMoziOrganizationPageOrganizationEmployeeCodesRequest); System.out.println("queryOrganizationEmployeeCodes~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); System.out.println(tenantId); System.out.println(apiResult.getContent().getData()); System.out.println(JSONObject.toJSON(apiResult)); executableClient.destroy(); return JSONObject.toJSON(apiResult); } //批量查询人员详情根据人员Code @Override public Object queryListEmployeesByCodes(List employeeCodes){ String api = "/mozi/employee/listEmployeesByCodes"; IntelligentPostClient intelligentPostClient = executableClient.newIntelligentPostClient(api); OapiMoziEmployeeListEmployeesByCodesRequest oapiMoziEmployeeListEmployeesByCodesRequest = new OapiMoziEmployeeListEmployeesByCodesRequest(); //null oapiMoziEmployeeListEmployeesByCodesRequest.setEmployeeCodes(employeeCodes); oapiMoziEmployeeListEmployeesByCodesRequest.setTenantId(Long.valueOf(tenantId)); OapiMoziEmployeeListEmployeesByCodesResponse apiResult = intelligentPostClient.post(oapiMoziEmployeeListEmployeesByCodesRequest); System.out.println("queryListEmployeesByCodes~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); System.out.println(apiResult.getContent().getData()); System.out.println(JSONObject.toJSON(apiResult)); executableClient.destroy(); return JSONObject.toJSON(apiResult); } //查询组织下人员总数 @Override public Object getOrganizationEmployeeCount(String organizationCode){ //executableClient保证单例 IntelligentGetClient intelligentGetClient = executableClient.newIntelligentGetClient("/mozi/organization/getOrganizationEmployeeCount"); OapiMoziOrganizationGetOrganizationEmployeeCountRequest oapiMoziOrganizationGetOrganizationEmployeeCountRequest = new OapiMoziOrganizationGetOrganizationEmployeeCountRequest(); //组织code oapiMoziOrganizationGetOrganizationEmployeeCountRequest.setOrganizationCode(organizationCode); //租户id oapiMoziOrganizationGetOrganizationEmployeeCountRequest.setTenantId(Long.valueOf(tenantId)); //获取结果 OapiMoziOrganizationGetOrganizationEmployeeCountResponse apiResult = intelligentGetClient.get(oapiMoziOrganizationGetOrganizationEmployeeCountRequest); System.out.println("getOrganizationEmployeeCount~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); System.out.println(apiResult.getContent().getData()); System.out.println(JSONObject.toJSON(apiResult)); executableClient.destroy(); return JSONObject.toJSON(apiResult); } //查询组织子孙组织详情 @Override public Object pageSubOrganizationCodes(String organizationCode, Integer pageNo){ //executableClient保证单例 IntelligentGetClient intelligentGetClient = executableClient.newIntelligentGetClient("/mozi/organization/pageSubOrganizationCodes"); OapiMoziOrganizationPageSubOrganizationCodesRequest oapiMoziOrganizationPageSubOrganizationCodesRequest = new OapiMoziOrganizationPageSubOrganizationCodesRequest(); oapiMoziOrganizationPageSubOrganizationCodesRequest.setReturnTotalSize(true); oapiMoziOrganizationPageSubOrganizationCodesRequest.setOrganizationCode(organizationCode); oapiMoziOrganizationPageSubOrganizationCodesRequest.setPageNo(pageNo); oapiMoziOrganizationPageSubOrganizationCodesRequest.setStatus("TOTAL"); oapiMoziOrganizationPageSubOrganizationCodesRequest.setTenantId(Long.valueOf(tenantId)); //获取结果 OapiMoziOrganizationPageSubOrganizationCodesResponse apiResult = intelligentGetClient.get(oapiMoziOrganizationPageSubOrganizationCodesRequest); System.out.println("getOrganizationEmployeeCount~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); System.out.println(apiResult.getContent().getData()); System.out.println(JSONObject.toJSON(apiResult)); executableClient.destroy(); return JSONObject.toJSON(apiResult); } @Override public JSONObject getAccessToken(String key, String Secret) { String api = "/gettoken.json"; GetClient getClient = executableClient.newGetClient(api); //设置参数 getClient.addParameter("appkey", key); getClient.addParameter("appsecret", Secret); //调用API String apiResult = getClient.get(); apiResult = new String(apiResult.getBytes(), StandardCharsets.UTF_8); System.out.println("getAccessToken~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); System.out.println(apiResult); return JSONObject.parseObject(apiResult); } @Override public JSONObject getUserDing(String accessToken, String authCode) { String api = "/rpc/oauth2/dingtalk_app_user.json"; PostClient postClient = executableClient.newPostClient(api); //Set the parameters postClient.addParameter("access_token", accessToken); postClient.addParameter("auth_code", authCode); //Call API String apiResult = postClient.post(); JSONObject content = JSONObject.parseObject(apiResult); System.out.println("getAccessToken~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); System.out.println(content); return content; } @Override public void downloadFileByDing(String mediaId, HttpServletResponse response) { JSONObject jsonObject = this.getAccessToken(appkey, appSecret); String token = ""; if( ObjectUtil.isNotNull(jsonObject.getJSONObject("content")) && ObjectUtil.isNotNull(jsonObject.getJSONObject("content").getJSONObject("data")) && ObjectUtil.isNotNull(jsonObject.getJSONObject("content").getJSONObject("data").getString("accessToken")) ){ token = jsonObject.getJSONObject("content").getJSONObject("data").getString("accessToken"); } if(org.apache.commons.lang3.StringUtils.isNotEmpty(token)){ CloseableHttpResponse closeableHttpResponse = null; try { String api = "/media/download"; GetClient getClient = executableClient.newGetClient(api); getClient.addParameter("access_token", token); getClient.addParameter("media_id", mediaId); closeableHttpResponse = getClient.getB(); HttpEntity entity = closeableHttpResponse.getEntity(); InputStream is = entity.getContent(); BufferedImage bufferedImage = compressImage(is, 1200); ImageIO.write(bufferedImage, "jpg", response.getOutputStream()); } catch (Exception e) { e.printStackTrace(); }finally { if (response != null) { try { closeableHttpResponse.close(); } catch (Exception e) { e.printStackTrace(); } } } } } @Override public void downloadFileByTest(MultipartFile file, HttpServletResponse response){ try{ InputStream inputStream = file.getInputStream(); BufferedImage bufferedImage = compressImage(inputStream, 1200); ImageIO.write(bufferedImage, "jpg", response.getOutputStream()); }catch (IOException ex){ ex.printStackTrace(); } } @Override public Integer getCompressNumByImg() { return compressNum; } private DingUser getTinkerUserDing(String key, String Secret, String code, int codeType) { String accessToken = getAccessToken(executableClient, key, Secret); DingUser userDing; if (codeType == 1) { userDing = getUserDing(executableClient, accessToken, code); } else { userDing = getUserDingByCode(executableClient, accessToken, code); } executableClient.destroy(); return userDing; } private String getAccessToken(ExecutableClient executableClient, String key, String Secret) { String api = "/gettoken.json"; GetClient getClient = executableClient.newGetClient(api); //设置参数 getClient.addParameter("appkey", key); getClient.addParameter("appsecret", Secret); //调用API String apiResult = getClient.get(); apiResult = new String(apiResult.getBytes(), StandardCharsets.UTF_8); System.out.println(apiResult+"======apiResult"); return JSONObject.parseObject(apiResult).getJSONObject("content").getJSONObject("data").get("accessToken").toString(); } private DingUser getUserDing(ExecutableClient executableClient, String accessToken, String authCode) { String api = "/rpc/oauth2/dingtalk_app_user.json"; PostClient postClient = executableClient.newPostClient(api); //Set the parameters postClient.addParameter("access_token", accessToken); postClient.addParameter("auth_code", authCode); //Call API String apiResult = postClient.post(); JSONObject content = JSONObject.parseObject(apiResult).getJSONObject("content"); if (!content.getBoolean("success")) { throw new BusinessException(content.getString("responseMessage")); } DingUser data = content.getObject("data", DingUser.class); System.out.println(data.getAccountId()+"=========AccountId"); return content.getObject("data", DingUser.class); } private DingUser getUserDingByCode(ExecutableClient executableClient, String accessToken, String code) { String api = "/rpc/oauth2/getuserinfo_bycode.json"; PostClient postClient = executableClient.newPostClient(api); //Set the parameters postClient.addParameter("access_token", accessToken); postClient.addParameter("code", code); //Call API String apiResult = postClient.post(); JSONObject content = JSONObject.parseObject(apiResult).getJSONObject("content"); if (!content.getBoolean("success")) { throw new BusinessException(content.getString("responseMessage")); } return content.getObject("data", DingUser.class); } public static BufferedImage compressImage(InputStream inputStream, int width) throws IOException { BufferedImage originalImage = ImageIO.read(inputStream); int nW = originalImage.getWidth(), nH = originalImage.getHeight(); if(nW > width){ nW = width; nH = nW * originalImage.getHeight() / originalImage.getWidth(); } Image compressedImage = originalImage.getScaledInstance(nW, nH, Image.SCALE_SMOOTH); BufferedImage outputImage = new BufferedImage(nW, nH, BufferedImage.TYPE_INT_RGB); outputImage.createGraphics().drawImage(compressedImage, 0, 0, null); return outputImage; } }