|  | @ -0,0 +1,258 @@
 | 
	
		
			
				|  |  | package com.yihu.jw.care.util;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSON;
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONArray;
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | import com.alibaba.xxpt.gateway.shared.client.http.ExecutableClient;
 | 
	
		
			
				|  |  | import com.alibaba.xxpt.gateway.shared.client.http.GetClient;
 | 
	
		
			
				|  |  | import com.alibaba.xxpt.gateway.shared.client.http.PostClient;
 | 
	
		
			
				|  |  | import com.yihu.jw.doctor.dao.BaseDoctorDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.doctor.BaseDoctorDO;
 | 
	
		
			
				|  |  | import org.apache.commons.lang3.StringUtils;
 | 
	
		
			
				|  |  | import org.slf4j.Logger;
 | 
	
		
			
				|  |  | import org.slf4j.LoggerFactory;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | import org.springframework.jdbc.core.JdbcTemplate;
 | 
	
		
			
				|  |  | import org.springframework.stereotype.Component;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import javax.annotation.PostConstruct;
 | 
	
		
			
				|  |  | import java.util.ArrayList;
 | 
	
		
			
				|  |  | import java.util.List;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
		
			
				|  |  |  * 阿里巴巴钉钉工具类
 | 
	
		
			
				|  |  |  * Created by yeshijie on 2022/3/23.
 | 
	
		
			
				|  |  |  */
 | 
	
		
			
				|  |  | @Component
 | 
	
		
			
				|  |  | public class DingdingUtil {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private static final Logger logger = LoggerFactory.getLogger(DingdingUtil.class);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     //测试-专有钉钉
 | 
	
		
			
				|  |  | //    private static final String AppKey = "ceshiyzh-jtP6zf3cfZEqs8UmmYNra";
 | 
	
		
			
				|  |  | //    private static final String AppSecret = "NbbH9viHPDNTPTuQPz2Y0Y06F88krYWTTPLv0h01";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     //正式-浙政钉
 | 
	
		
			
				|  |  |     private static final String zhdAppKey = "zhyzh-r085NCVALJYmgcDc7oBVFRZj";
 | 
	
		
			
				|  |  |     private static final String zhdAppSecret = "fH8ZVDGAmJaeF7ujwAZgCi40w0U3im9J801vBaSF";
 | 
	
		
			
				|  |  |     private static final String zhdDomainName = "openplatform.dg-work.cn";
 | 
	
		
			
				|  |  |     private static final String zhdtenantId = "50495309";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     //正式-专有钉钉
 | 
	
		
			
				|  |  |     private static final String AppKey = "zhyzh-67H0tpyZr7m8crdJsxI3YVZb";
 | 
	
		
			
				|  |  |     private static final String AppSecret = "xK0VBIfeUj0gBm0Gp55180Qk6i866230Qch1T96v";
 | 
	
		
			
				|  |  |     private static final String DomainName = "openplatform-pro.ding.zj.gov.cn";
 | 
	
		
			
				|  |  |     private static final String tenantId = "196729";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private static final String gettokenApi = "/gettoken.json";
 | 
	
		
			
				|  |  |     private static final String dingtalk_app_user = "/rpc/oauth2/dingtalk_app_user.json";
 | 
	
		
			
				|  |  |     private static final String get_by_mobiles = "/mozi/employee/get_by_mobiles";
 | 
	
		
			
				|  |  |     private static final String getuserinfo_bycode = "/rpc/oauth2/getuserinfo_bycode.json";
 | 
	
		
			
				|  |  |     private static final String sendMsg = "/chat/sendMsg";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     //单聊默认发送者id
 | 
	
		
			
				|  |  |     private static final String p2pSenderId = "78728186";
 | 
	
		
			
				|  |  | //    private static final String p2pSenderId = "821606";
 | 
	
		
			
				|  |  |     public static final String picMediaId = "$iwHSAADGgQoAC9EkWgKsb2N0ZXQtc3RyZWFtAwAEAAUABrzrLoqjhIdIKXPi5GCsroJXXzE3ZmI0OGFiNWJhBwAIAAmhMA";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     //executableClient要单例,并且使用前要初始化,只需要初始化一次
 | 
	
		
			
				|  |  |     private static ExecutableClient executableClient = null;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BaseDoctorDao doctorDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private JdbcTemplate jdbcTemplate;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 测试浙政钉
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public void testZzd() {
 | 
	
		
			
				|  |  | //        ExecutableClient executableClient = ExecutableClient.getInstance();
 | 
	
		
			
				|  |  | //        executableClient.setAccessKey(zhdAppKey);
 | 
	
		
			
				|  |  | //        executableClient.setSecretKey(zhdAppSecret);
 | 
	
		
			
				|  |  | //        executableClient.setDomainName(zhdDomainName);
 | 
	
		
			
				|  |  | //        executableClient.setProtocal("https");
 | 
	
		
			
				|  |  | //        executableClient.init();
 | 
	
		
			
				|  |  | //        logger.info("get_by_mobiles========="+zhdDomainName);
 | 
	
		
			
				|  |  | //        //调用API
 | 
	
		
			
				|  |  | //        PostClient postClient = executableClient.newPostClient(get_by_mobiles);
 | 
	
		
			
				|  |  | //        //设置参数
 | 
	
		
			
				|  |  | //        postClient.addParameter("areaCode", "86");
 | 
	
		
			
				|  |  | //        //手机号码列表,逗号分隔,最多50个
 | 
	
		
			
				|  |  | //        postClient.addParameter("mobiles", "15659713528");
 | 
	
		
			
				|  |  | //        postClient.addParameter("tenantId", zhdtenantId);
 | 
	
		
			
				|  |  | //        postClient.addParameter("namespace", "local");
 | 
	
		
			
				|  |  | //        //Call API
 | 
	
		
			
				|  |  | //        String apiResult2 = postClient.post();
 | 
	
		
			
				|  |  | //        logger.info("get_by_mobiles========="+apiResult2);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @PostConstruct
 | 
	
		
			
				|  |  |     public void clientInit(){
 | 
	
		
			
				|  |  |         if(executableClient == null){
 | 
	
		
			
				|  |  |             synchronized(ExecutableClient.class){
 | 
	
		
			
				|  |  |                 if(executableClient == null){
 | 
	
		
			
				|  |  |                     executableClient = ExecutableClient.getInstance();
 | 
	
		
			
				|  |  |                     executableClient.setAccessKey(AppKey);
 | 
	
		
			
				|  |  |                     executableClient.setSecretKey(AppSecret);
 | 
	
		
			
				|  |  |                     executableClient.setDomainName(DomainName);
 | 
	
		
			
				|  |  |                     executableClient.setProtocal("https");
 | 
	
		
			
				|  |  |                     executableClient.init();
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 获取AccessToken
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public String getAccessToken(){
 | 
	
		
			
				|  |  |         GetClient getClient = executableClient.newGetClient(gettokenApi);
 | 
	
		
			
				|  |  |         //设置参数
 | 
	
		
			
				|  |  |         getClient.addParameter("appkey", AppKey);
 | 
	
		
			
				|  |  |         getClient.addParameter("appsecret", AppSecret);
 | 
	
		
			
				|  |  |         //调用API
 | 
	
		
			
				|  |  |         String apiResult = getClient.get();
 | 
	
		
			
				|  |  |         logger.info(apiResult);
 | 
	
		
			
				|  |  |         //{"success":true,"content":{"data":{"expiresIn":7200,"accessToken":"app_4197121c698246cea9ec660902324edd"},"success":true,"requestId":"276077ca16480212814976877e76c6","responseMessage":"OK","responseCode":"0","bizErrorCode":"0"},"bizErrorCode":"0"}
 | 
	
		
			
				|  |  |         JSONObject jsonObject = JSON.parseObject(apiResult);
 | 
	
		
			
				|  |  |         if(jsonObject.getBoolean("success")){
 | 
	
		
			
				|  |  |             JSONObject content = jsonObject.getJSONObject("content");
 | 
	
		
			
				|  |  |             if(content.getBoolean("success")){
 | 
	
		
			
				|  |  |                 return content.getJSONObject("data").getString("accessToken");
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return null;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 获取用户信息
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public JSONObject dingtalk_app_user(String authCode){
 | 
	
		
			
				|  |  |         //调用API
 | 
	
		
			
				|  |  |         PostClient postClient = executableClient.newPostClient(dingtalk_app_user);
 | 
	
		
			
				|  |  |         //设置参数
 | 
	
		
			
				|  |  |         postClient.addParameter("access_token", getAccessToken());
 | 
	
		
			
				|  |  |         postClient.addParameter("auth_code", authCode);
 | 
	
		
			
				|  |  |         //Call API
 | 
	
		
			
				|  |  |         String apiResult = postClient.post();
 | 
	
		
			
				|  |  |         logger.info(apiResult);
 | 
	
		
			
				|  |  |         //{"success":true,"content":{"success":false,"responseMessage":"code失效或不存在","responseCode":"240111","bizErrorCode":"MBS-B001-02-16-240111"}}
 | 
	
		
			
				|  |  |         return JSON.parseObject(apiResult);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 根据手机号获取员工账户id
 | 
	
		
			
				|  |  |      * @param mobiles
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public JSONArray get_by_mobiles(String mobiles){
 | 
	
		
			
				|  |  |         //调用API
 | 
	
		
			
				|  |  |         PostClient postClient = executableClient.newPostClient(get_by_mobiles);
 | 
	
		
			
				|  |  |         //设置参数
 | 
	
		
			
				|  |  |         postClient.addParameter("areaCode", "86");
 | 
	
		
			
				|  |  |         //手机号码列表,逗号分隔,最多50个
 | 
	
		
			
				|  |  |         postClient.addParameter("mobiles", mobiles);
 | 
	
		
			
				|  |  |         postClient.addParameter("tenantId", tenantId);
 | 
	
		
			
				|  |  |         postClient.addParameter("namespace", "local");
 | 
	
		
			
				|  |  |         //Call API
 | 
	
		
			
				|  |  |         String apiResult = postClient.post();
 | 
	
		
			
				|  |  |         logger.info(apiResult);
 | 
	
		
			
				|  |  |         //{"success":true,"content":{"data":[{"accountId":821685,"mobile":"15859634562","employeeCode":"GE_f27a7231e19f4b31b8f11d2859d1be07","status":0},{"accountId":821606,"mobile":"13559485270","employeeCode":"GE_10933a0f8bc949149143163cba5bb81f","status":0}],"success":true,"requestId":"2760828316480239373806874ed97b","responseMessage":"OK","responseCode":"0","bizErrorCode":"0"},"bizErrorCode":"0"}
 | 
	
		
			
				|  |  |         JSONObject jsonObject = JSON.parseObject(apiResult);
 | 
	
		
			
				|  |  |         if(jsonObject.getBoolean("success")){
 | 
	
		
			
				|  |  |             JSONObject content = jsonObject.getJSONObject("content");
 | 
	
		
			
				|  |  |             if(content.getBoolean("success")){
 | 
	
		
			
				|  |  |                 return content.getJSONArray("data");
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return null;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 初始化更新钉钉账户id
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public void yktDoctorIdInit(){
 | 
	
		
			
				|  |  |         List<BaseDoctorDO> doctorDOList = doctorDao.findByYktDoctorIdNull();
 | 
	
		
			
				|  |  |         List<BaseDoctorDO> doctorDOs = new ArrayList<>();
 | 
	
		
			
				|  |  |         for (BaseDoctorDO doctorDO:doctorDOList){
 | 
	
		
			
				|  |  |             JSONArray jsonArray = get_by_mobiles(doctorDO.getMobile());
 | 
	
		
			
				|  |  |             if(jsonArray!=null&&jsonArray.size()>0){
 | 
	
		
			
				|  |  |                 long accountId = jsonArray.getJSONObject(0).getLong("accountId");
 | 
	
		
			
				|  |  |                 doctorDO.setYktDoctorId(accountId+"");
 | 
	
		
			
				|  |  |                 doctorDOs.add(doctorDO);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         if(doctorDOs.size()>0){
 | 
	
		
			
				|  |  |             doctorDao.save(doctorDOs);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 更新单个钉钉账户id
 | 
	
		
			
				|  |  |      * @param mobile
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public void updYktDoctorId(String mobile){
 | 
	
		
			
				|  |  |         List<BaseDoctorDO> doctorDOs = doctorDao.findByMobile(mobile);
 | 
	
		
			
				|  |  |         if(doctorDOs.size()>0){
 | 
	
		
			
				|  |  |             BaseDoctorDO doctorDO = doctorDOs.get(0);
 | 
	
		
			
				|  |  |             JSONArray jsonArray = get_by_mobiles(doctorDO.getMobile());
 | 
	
		
			
				|  |  |             if(jsonArray!=null&&jsonArray.size()>0){
 | 
	
		
			
				|  |  |                 long accountId = jsonArray.getJSONObject(0).getLong("accountId");
 | 
	
		
			
				|  |  |                 doctorDO.setYktDoctorId(accountId+"");
 | 
	
		
			
				|  |  |                 doctorDao.save(doctorDOs);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public String getuserinfo_bycode(String code){
 | 
	
		
			
				|  |  |         //调用API
 | 
	
		
			
				|  |  |         PostClient postClient = executableClient.newPostClient(getuserinfo_bycode);
 | 
	
		
			
				|  |  |         //设置参数
 | 
	
		
			
				|  |  |         postClient.addParameter("access_token", getAccessToken());
 | 
	
		
			
				|  |  |         postClient.addParameter("code", code);
 | 
	
		
			
				|  |  |         //Call API
 | 
	
		
			
				|  |  |         String apiResult = postClient.post();
 | 
	
		
			
				|  |  |         logger.info(apiResult);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | //        JSONObject jsonObject = JSON.parseObject(apiResult);
 | 
	
		
			
				|  |  | //        if(jsonObject.getBoolean("success")){
 | 
	
		
			
				|  |  | //            JSONObject content = jsonObject.getJSONObject("content");
 | 
	
		
			
				|  |  | //            if(content.getBoolean("success")){
 | 
	
		
			
				|  |  | //                return content.getJSONObject("data").getString("accessToken");
 | 
	
		
			
				|  |  | //            }
 | 
	
		
			
				|  |  | //        }
 | 
	
		
			
				|  |  |         return null;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 发送单聊消息
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public String sendP2pMsg(String msg,String receiverId){
 | 
	
		
			
				|  |  |         return this.sendP2pMsg(msg, receiverId,null);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 发送单聊消息
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public String sendP2pMsg(String msg,String receiverId,String sendId){
 | 
	
		
			
				|  |  |         PostClient postClient = executableClient.newPostClient(sendMsg);
 | 
	
		
			
				|  |  |         //Set the parameters
 | 
	
		
			
				|  |  |         postClient.addParameter("msg", msg);
 | 
	
		
			
				|  |  |         postClient.addParameter("senderId", StringUtils.isBlank(sendId)?p2pSenderId:sendId);
 | 
	
		
			
				|  |  |         postClient.addParameter("receiverId", receiverId);
 | 
	
		
			
				|  |  |         postClient.addParameter("chatId", "");
 | 
	
		
			
				|  |  |         postClient.addParameter("tenantId", tenantId);
 | 
	
		
			
				|  |  |         postClient.addParameter("chatType", "1");
 | 
	
		
			
				|  |  |         //Call API
 | 
	
		
			
				|  |  |         String apiResult = postClient.post();
 | 
	
		
			
				|  |  |         logger.info("响应结果"+apiResult);
 | 
	
		
			
				|  |  |         return apiResult;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | }
 |