111
k94314517
2025-07-09 d5984685e7efd46a5e850ade13b22fa727163a47
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package com.doumee.core.utils.qiyeweixin;
 
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.doumee.biz.system.SystemDictDataBiz;
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.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;
 
 
@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));
        }
    }
}