jiangping
2025-04-25 d1062daf5ca214784ad42d7f62c95e1cd13a4ed6
server/src/main/java/com/doumee/core/utils/qiyeweixin/QywxUtil.java
@@ -9,10 +9,13 @@
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;
@@ -24,15 +27,16 @@
    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;
//        QywxUtil.qiweiUrl =  serviceurl;
    }
@@ -86,6 +90,7 @@
            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;
            }
@@ -109,12 +114,42 @@
        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.error("企业微信临时素材获取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;
@@ -130,6 +165,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
@@ -140,12 +188,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+"】================开始===="+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.postJson(qyUtil.qiweiUrl+url,param);
                QywxBaseResponse result = JSONObject.parseObject(res, typeReference.getType());
                logResult(result,name);
                return  result;
@@ -156,13 +204,30 @@
        }
        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(url,param);
                res = HttpsUtil.postJson(qyUtil.qiweiUrl+url,param);
                T result = JSONObject.parseObject(res, typeReference.getType());
//                logResult(result,name);
                return  result;