¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.doumee.core.utils.qiyeweixin; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.doumee.core.utils.HttpsUtil; |
| | | import com.doumee.core.utils.qiyeweixin.model.request.QywxSendMsgRequest; |
| | | import com.doumee.core.utils.qiyeweixin.model.response.*; |
| | | import com.fasterxml.jackson.core.type.TypeReference; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | |
| | | @Component |
| | | @Slf4j |
| | | public class QywxUtil { |
| | | private static JSONObject json = new JSONObject(); |
| | | |
| | | |
| | | |
| | | private static QywxUtil qyUtil; |
| | | |
| | | // @Value("${qiwei.serviceurl}") |
| | | private String qiweiUrl; |
| | | |
| | | // @PostConstruct |
| | | private void init() { |
| | | qyUtil = this; |
| | | // QywxUtil.qiweiUrl = serviceurl; |
| | | } |
| | | |
| | | |
| | | public static String create(Map<String, Object> map, String url, String token) throws IOException { |
| | | // String token = getToken(QY_Constant.CORPID, QY_Constant.SCHEDULESECRET); |
| | | String postData = createPostData(map); |
| | | String response = HttpsUtil.post(url + token, postData, "application/json", false); |
| | | System.out.println("è·åå°çtoken======>" + token); |
| | | System.out.println("è¯·æ±æ°æ®======>" + postData); |
| | | System.out.println("åé微信çååºæ°æ®======>" + response); |
| | | return response; |
| | | } |
| | | |
| | | private static String createPostData(Map<String, Object> map) { |
| | | System.out.println("è¿å
¥createPostDataæ¹æ³-------------------------"); |
| | | return JSONObject.toJSONString(map); |
| | | } |
| | | |
| | | |
| | | public static String getAccessToken(String corpId, String corpSecret) { |
| | | String[] interfaceUrl = QywxConstant.GET_ACCESS_TOKEN; |
| | | String url = interfaceUrl[0].replace("${corpid}",corpId).replace("${secret}",corpSecret); |
| | | QywxBaseResponse response = sendHttpRequest(url,interfaceUrl[1],"",new TypeReference<QywxBaseResponse<String>>(){}); |
| | | if(response.getErrcode()!=null && response.getErrcode() ==0){ |
| | | return response.getAccess_token(); |
| | | } |
| | | return null; |
| | | } |
| | | public static List<QywxDepartInfoResponse> getDepartmentAll(String token) { |
| | | String[] interfaceUrl = QywxConstant.GET_DEPARTMENT_LIST; |
| | | String url = interfaceUrl[0].replace("${accesstoken}",token).replace("${id}",""); |
| | | QywxBaseResponse<List<QywxDepartInfoResponse>> response = sendHttpRequest(url,interfaceUrl[1],"",new TypeReference<QywxBaseResponse<List<QywxDepartInfoResponse>>>(){}); |
| | | if(response.getErrcode()!=null && response.getErrcode() ==0){ |
| | | return response.getData(); |
| | | } |
| | | return null; |
| | | } |
| | | public static QywxDepartInfoResponse getDepartmentInfo(String token,String departid) { |
| | | String[] interfaceUrl = QywxConstant.GET_DEPARTMENT_INFO; |
| | | String url = interfaceUrl[0].replace("${accesstoken}",token).replace("${id}",departid); |
| | | QywxBaseResponse<QywxDepartInfoResponse> response = sendHttpRequest(url,interfaceUrl[1],"" |
| | | ,new TypeReference< QywxBaseResponse<QywxDepartInfoResponse>>(){}); |
| | | if(response.getErrcode()!=null && response.getErrcode() ==0){ |
| | | return response.getData(); |
| | | } |
| | | return null; |
| | | } |
| | | public static QywxSendMsgResponse sendMsg(String token, QywxSendMsgRequest param) { |
| | | try{ |
| | | String[] interfaceUrl = QywxConstant.SEND_MSG; |
| | | String url = interfaceUrl[0].replace("${accesstoken}",token); |
| | | QywxSendMsgResponse response = sendHttpRequestSingle(url,interfaceUrl[1], JSONObject.toJSONString(param) |
| | | ,new TypeReference<QywxSendMsgResponse>(){}); |
| | | log.error("æ¨éæ¶æ¯ç»æ{}",JSONObject.toJSONString(response)); |
| | | if(response.getErrcode()!=null && response.getErrcode() ==0){ |
| | | return response; |
| | | } |
| | | }catch (Exception e){ |
| | | |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | public static List<QywxUserListResponse> getUserList(String token,String depatId) { |
| | | try { |
| | | String[] interfaceUrl = QywxConstant.GET_DEPARTMENT_USER_LIST; |
| | | String url = interfaceUrl[0].replace("${accesstoken}",token).replace("${departmentId}",depatId); |
| | | QywxBaseResponse<List<QywxUserListResponse>> response = sendHttpRequest(url,interfaceUrl[1],"",new TypeReference<QywxBaseResponse<List<QywxUserListResponse>>>(){}); |
| | | if(response.getErrcode()!=null && response.getErrcode() ==0){ |
| | | return response.getData(); |
| | | } |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | public static InputStream getMediaInputstream(String token, String media) { |
| | | try { |
| | | String[] interfaceUrl = QywxConstant.GET_MEDIA; |
| | | String url = qyUtil.qiweiUrl+interfaceUrl[0].replace("${accesstoken}",token).replace("${media_id}",media); |
| | | log.info("ä¼ä¸å¾®ä¿¡ä¸´æ¶ç´ æè·åurl=========="+url); |
| | | return HttpsUtil.connectionInputsteam(url,"GET",null,null); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | log.error("ä¼ä¸å¾®ä¿¡ä¸´æ¶ç´ æè·åerror=========="+e.getMessage()); |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | public static QywxUserInfoResponse getUserInfo(String token,String id) { |
| | | String[] interfaceUrl = QywxConstant.GET_USER_DETAIL; |
| | | String url = interfaceUrl[0].replace("${accesstoken}",token).replace("${id}",id); |
| | | QywxUserInfoResponse response = sendHttpRequestSingle(url,interfaceUrl[1],"" |
| | | ,new TypeReference<QywxUserInfoResponse>(){}); |
| | | if(response.getErrcode()!=null && response.getErrcode() ==0){ |
| | | return response; |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @param token |
| | | * @param type åªä½æä»¶ç±»åï¼å嫿å¾çï¼imageï¼ãè¯é³ï¼voiceï¼ãè§é¢ï¼videoï¼ï¼æ®éæä»¶ï¼fileï¼ |
| | | * @return |
| | | */ |
| | | public static QywxUploadMediaResponse uploadMedia(String token,String type,String imgurl) { |
| | | String[] interfaceUrl = QywxConstant.UPLOAD_TEMP_MEDIA; |
| | | String url = interfaceUrl[0].replace("${accesstoken}",token).replace("${type}",type); |
| | | QywxUploadMediaResponse response = sendHttpRequestMultifile(url,interfaceUrl[1],imgurl |
| | | ,new TypeReference<QywxUploadMediaResponse>(){}); |
| | | if(response !=null && response.getErrcode()!=null && response.getErrcode() ==0){ |
| | | return response; |
| | | } |
| | | return null; |
| | | } |
| | | public static QywxBaseResponse<String> getUserInfoByCode(String token,String code) { |
| | | String[] interfaceUrl = QywxConstant.GET_USER_BY_AUTH_CODE; |
| | | String url = interfaceUrl[0].replace("${accesstoken}",token).replace("${code}",code); |
| | | QywxBaseResponse<String> response = sendHttpRequest(url,interfaceUrl[1],"" |
| | | ,new TypeReference<QywxBaseResponse<String>>(){}); |
| | | if(response.getErrcode()!=null && response.getErrcode() ==0){ |
| | | return response; |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | public static String getJsApiTicket(String token) { |
| | | String[] interfaceUrl = QywxConstant.GET_JSAPI_TICKET; |
| | | String url = interfaceUrl[0].replace("${accesstoken}",token); |
| | | QywxBaseResponse<String> response = sendHttpRequest(url,interfaceUrl[1],"" |
| | | ,new TypeReference<QywxBaseResponse<String>>(){}); |
| | | if(response.getErrcode()!=null && response.getErrcode() ==0){ |
| | | return response.getTicket(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * åèµ·wmsæ¥å£è¯·æ± |
| | | * @param url |
| | | * @param name |
| | | * @param param |
| | | * @param typeReference |
| | | * @return |
| | | * @param <T> |
| | | */ |
| | | public static <T> QywxBaseResponse<T> sendHttpRequest(String url, String name, String param, TypeReference<QywxBaseResponse<T>> typeReference){ |
| | | log.info("ã"+name+"ã================å¼å§===="+url+"\nparam==========================:"+ JSONObject.toJSONString(param)); |
| | | if ( StringUtils.isNotBlank(url)) { |
| | | String res = null; |
| | | try { |
| | | Map<String,String> headers = new HashMap<>(); |
| | | res = HttpsUtil.postJson(qyUtil.qiweiUrl+url,param); |
| | | QywxBaseResponse result = JSONObject.parseObject(res, typeReference.getType()); |
| | | logResult(result,name); |
| | | return result; |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | log.error("ã"+name+"ã================失败===="+ JSONObject.toJSONString(param)); |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | public static <T extends QywxBaseSingleResponse> T sendHttpRequestMultifile(String url, String name, String imgUrl , TypeReference<T> typeReference){ |
| | | log.info("ã"+name+"ã================å¼å§===="+ imgUrl); |
| | | if ( StringUtils.isNotBlank(url)) { |
| | | String res = null; |
| | | try { |
| | | res = HttpsUtil.uploadTempMedia (qyUtil.qiweiUrl+url,imgUrl); |
| | | log.info("ä»ä¼å¾®æ¥å£:{}----ä¸ä¼ 临æ¶ç´ æç»æ:{}",url,res); |
| | | JSONObject jsonObject = JSONObject.parseObject(res); |
| | | T result = JSONObject.parseObject(res, typeReference.getType()); |
| | | return result; |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | log.error("ã"+name+"ã================失败===="+ imgUrl); |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | public static <T extends QywxBaseSingleResponse> T sendHttpRequestSingle(String url, String name, String param, TypeReference<T> typeReference){ |
| | | log.info("ã"+name+"ã================å¼å§===="+ JSONObject.toJSONString(param)); |
| | | if ( StringUtils.isNotBlank(url)) { |
| | | String res = null; |
| | | try { |
| | | Map<String,String> headers = new HashMap<>(); |
| | | res = HttpsUtil.postJson(qyUtil.qiweiUrl+url,param); |
| | | T result = JSONObject.parseObject(res, typeReference.getType()); |
| | | // logResult(result,name); |
| | | return result; |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | log.error("ã"+name+"ã================失败===="+ JSONObject.toJSONString(param)); |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | private static void logResult(QywxBaseResponse res,String name) { |
| | | if( res.getErrcode() !=null && res.getErrcode().equals(0)){ |
| | | log.info("ãä¼ä¸å¾®ä¿¡æ¥å£ï¼"+name+"ã================æå====\n"+ JSONObject.toJSONString(res)); |
| | | }else{ |
| | | log.error("ãä¼ä¸å¾®ä¿¡æ¥å£ï¼"+name+"ã================失败====ï¼\n"+ JSONObject.toJSONString(res)); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |