123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- 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<String,Object> msg = new HashMap<>();
- msg.put("msgtype","text");
- Map<String,Object> 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<String> 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;
- }
- }
|