From 30e858fa504b268b9b436afca0a1259cf6e8c488 Mon Sep 17 00:00:00 2001 From: MrShi <1878285526@qq.com> Date: 星期二, 19 八月 2025 11:01:40 +0800 Subject: [PATCH] 优化 --- server/src/main/java/com/doumee/core/utils/qiyeweixin/QywxUtil.java | 142 ++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 126 insertions(+), 16 deletions(-) diff --git a/server/src/main/java/com/doumee/core/utils/qiyeweixin/QywxUtil.java b/server/src/main/java/com/doumee/core/utils/qiyeweixin/QywxUtil.java index e52a082..c3faaed 100644 --- a/server/src/main/java/com/doumee/core/utils/qiyeweixin/QywxUtil.java +++ b/server/src/main/java/com/doumee/core/utils/qiyeweixin/QywxUtil.java @@ -6,13 +6,17 @@ import com.doumee.core.utils.HttpsUtil; import com.doumee.core.utils.qiyeweixin.model.request.QywxSendMsgRequest; import com.doumee.core.utils.qiyeweixin.model.response.*; +import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; +import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -20,19 +24,23 @@ @Component @Slf4j +@Data public class QywxUtil { private static JSONObject json = new JSONObject(); - @Autowired - private SystemDictDataBiz systemDictDataBiz; private static QywxUtil qyUtil; + + @Value("${qiwei.serviceurl}") + private String qiweiUrl; @PostConstruct private void init() { qyUtil = this; - systemDictDataBiz = qyUtil.systemDictDataBiz; + System.out.println(qyUtil.qiweiUrl); + System.out.println(qyUtil.getQiweiUrl()); +// QywxUtil.qiweiUrl = serviceurl; } @@ -55,7 +63,7 @@ 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>>(){}); + QywxBaseResponse response = sendHttpGetRequest(url,interfaceUrl[1], new TypeReference<QywxBaseResponse<String>>(){}); if(response.getErrcode()!=null && response.getErrcode() ==0){ return response.getAccess_token(); } @@ -81,13 +89,19 @@ return null; } public static QywxSendMsgResponse sendMsg(String token, QywxSendMsgRequest param) { - String[] interfaceUrl = QywxConstant.SEND_MSG; - String url = interfaceUrl[0].replace("${accesstoken}",token); - QywxSendMsgResponse response = sendHttpRequestSingle(url,interfaceUrl[1],JSONObject.toJSONString(param) - ,new TypeReference<QywxSendMsgResponse>(){}); - if(response.getErrcode()!=null && response.getErrcode() ==0){ - return response; + 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>(){},true); + 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) { @@ -104,12 +118,57 @@ 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 Map<String,Object> getMediaInputstreamMap(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); + Map<String,Object> map = HttpsUtil.connInputsteamReturnMap(url,"GET",null,null); + return map; + }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>(){}); + ,new TypeReference<QywxUserInfoResponse>(){},false); if(response.getErrcode()!=null && response.getErrcode() ==0){ + return response; + } + return null; + } + + /** + * + * @param token + * @param type 濯掍綋鏂囦欢绫诲瀷锛屽垎鍒湁鍥剧墖锛坕mage锛夈�佽闊筹紙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; @@ -125,6 +184,19 @@ 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 @@ -135,12 +207,12 @@ * @param <T> */ public static <T> QywxBaseResponse<T> sendHttpRequest(String url, String name, String param, TypeReference<QywxBaseResponse<T>> typeReference){ - log.info("銆�"+name+"銆�================寮�濮�===="+ JSONObject.toJSONString(param)); + log.info("銆�"+name+"銆�================寮�濮�===="+qyUtil.qiweiUrl+url+"\nparam==========================:"+ JSONObject.toJSONString(param)); if ( StringUtils.isNotBlank(url)) { String res = null; try { Map<String,String> headers = new HashMap<>(); - res = HttpsUtil.postJson(url,param); + res = HttpsUtil.get(qyUtil.qiweiUrl+url,true); QywxBaseResponse result = JSONObject.parseObject(res, typeReference.getType()); logResult(result,name); return result; @@ -151,13 +223,51 @@ } return null; } - public static <T extends QywxBaseSingleResponse> T sendHttpRequestSingle(String url, String name, String param, TypeReference<T> typeReference){ - log.info("銆�"+name+"銆�================寮�濮�===="+ JSONObject.toJSONString(param)); + public static <T> QywxBaseResponse<T> sendHttpGetRequest(String url, String name, TypeReference<QywxBaseResponse<T>> typeReference){ + log.info("銆�"+name+"銆�================寮�濮�===="+qyUtil.qiweiUrl+url+"\nparam==========================:"+ JSONObject.toJSONString(url)); if ( StringUtils.isNotBlank(url)) { String res = null; try { Map<String,String> headers = new HashMap<>(); - res = HttpsUtil.postJson(url,param); + res = HttpsUtil.get(qyUtil.qiweiUrl+url,true); + QywxBaseResponse result = JSONObject.parseObject(res, typeReference.getType()); + logResult(result,name); + return result; + }catch (Exception e){ + e.printStackTrace(); + log.error("銆�"+name+"銆�================澶辫触===="+ JSONObject.toJSONString(url)); + } + } + return null; + } + public static <T extends QywxBaseSingleResponse> T sendHttpRequestMultifile(String url, String name, String imgUrl , TypeReference<T> typeReference){ + log.info("銆�"+name+"銆�================寮�濮�===="+qyUtil.qiweiUrl+url+"\nparam==========================:"+ 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,boolean isPost){ + log.info("銆�"+name+"銆�================寮�濮�===="+qyUtil.qiweiUrl+url+"\nparam==========================:"+ JSONObject.toJSONString(param)); + if ( StringUtils.isNotBlank(url)) { + String res = null; + try { + Map<String,String> headers = new HashMap<>(); + if(isPost){ + res = HttpsUtil.postJson(qyUtil.qiweiUrl+url,param); + }else{ + res = HttpsUtil.get(qyUtil.qiweiUrl+url,true); + } T result = JSONObject.parseObject(res, typeReference.getType()); // logResult(result,name); return result; -- Gitblit v1.9.3