提交 | 用户 | age
|
5c5945
|
1 |
package com.hx.mp.util; |
E |
2 |
|
|
3 |
import com.hx.util.OSSUtil; |
|
4 |
import com.hx.util.SimpleTool; |
|
5 |
import com.hx.util.StringUtils; |
|
6 |
import net.sf.json.JSONException; |
|
7 |
import net.sf.json.JSONObject; |
|
8 |
import org.apache.commons.codec.binary.Base64; |
486aaa
|
9 |
import org.apache.commons.io.FileUtils; |
5c5945
|
10 |
import org.apache.commons.io.IOUtils; |
E |
11 |
import org.springframework.stereotype.Component; |
|
12 |
|
|
13 |
import java.io.*; |
|
14 |
import java.net.HttpURLConnection; |
|
15 |
import java.net.URL; |
|
16 |
import java.nio.charset.StandardCharsets; |
|
17 |
import java.text.SimpleDateFormat; |
|
18 |
import java.util.Date; |
|
19 |
|
|
20 |
/** |
|
21 |
* 微信获取权限基本工具类 |
|
22 |
* @author ChenJiaHe |
|
23 |
* @Date 2020-07-14 |
|
24 |
*/ |
|
25 |
@Component |
|
26 |
public class MPWeixinBaseUtil { |
|
27 |
|
|
28 |
// 类型 |
|
29 |
private static final String GRANT_TYPE = "client_credential"; |
|
30 |
|
|
31 |
/**获取access_token链接*/ |
|
32 |
private static final String GETACCESS_TOKENURL = "https://api.weixin.qq.com/cgi-bin/token?grant_type="; |
|
33 |
/**通过code获取小程序信息链接*/ |
|
34 |
private static final String JSCODE2SESSION_URL = "https://api.weixin.qq.com/sns/jscode2session?"; |
|
35 |
/**发送订阅消息通知链接*/ |
|
36 |
private static final String SEND_SUBSCRIBE_MESSAGE = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="; |
|
37 |
//生成小程序二维码地址(方形) |
|
38 |
public static final String MAKE_TWOCODE_SQUARE_URL = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token="; |
|
39 |
//生成小程序二维码地址(圆形) |
|
40 |
public static final String MAKE_TWOCODE_ROUND_URL = "https://api.weixin.qq.com/wxa/getwxacode?access_token="; |
|
41 |
//生成无限二维码 |
|
42 |
public static final String URL_UNLIMIT_SQUARE = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={0}"; |
|
43 |
////////////////////////////////////////////////// |
|
44 |
|
|
45 |
/** (小程序)通过code换取网页授权access_token/如果是snsapi_base模式的授权,这里就可以拿到openId了 ***/ |
|
46 |
public static JSONObject getJscode2session(String appid,String secret,String code) { |
|
47 |
|
|
48 |
return HttpURLUtil(JSCODE2SESSION_URL+"appid="+appid+"&secret="+secret |
|
49 |
+"&grant_type=authorization_code&js_code=" + code,null); |
|
50 |
} |
|
51 |
|
|
52 |
/**发送订阅消息通知**/ |
|
53 |
public static JSONObject sendSubscribeMessage(String data,String accessToken){ |
|
54 |
return HttpURLUtil(SEND_SUBSCRIBE_MESSAGE+accessToken,data); |
|
55 |
} |
|
56 |
|
|
57 |
/** 获取access_token */ |
|
58 |
public static JSONObject getAccessToken(String appid,String secret) throws Exception { |
|
59 |
String access_token; |
|
60 |
// 通过WX接口获取access_token |
|
61 |
JSONObject obj = getApplication_Access_tokenForUrl(appid,secret); |
|
62 |
return obj; |
|
63 |
} |
|
64 |
|
|
65 |
/** 从weixin接口获取Access_token **/ |
|
66 |
public static JSONObject getApplication_Access_tokenForUrl(String appid,String secret) { |
|
67 |
return HttpURLUtil(GETACCESS_TOKENURL+GRANT_TYPE +"&appid="+appid +"&secret="+secret,null); |
|
68 |
} |
|
69 |
|
|
70 |
/**生成无限二维码,并上传到OSS*/ |
|
71 |
public static String createUnlimitQrCode(String at, String scene, String page, int width, boolean autoColor, |
|
72 |
JSONObject lineColor, boolean isHyaline, String fileName, String keyId, |
|
73 |
String keySecret, String endPoint, String bucket) |
|
74 |
{ |
|
75 |
String imgUrl = null; |
|
76 |
InputStream in = null; |
|
77 |
HttpURLConnection conn = null; |
|
78 |
|
|
79 |
try { |
|
80 |
//生成发送数据 |
|
81 |
JSONObject obj = new JSONObject(); |
|
82 |
obj.put("scene", scene); |
|
83 |
obj.put("width", width); |
|
84 |
obj.put("page", page); |
|
85 |
obj.put("auto_color", autoColor); |
|
86 |
obj.put("line_color", lineColor); |
|
87 |
obj.put("is_hyaline", isHyaline); |
|
88 |
|
|
89 |
// 创建url资源 |
|
90 |
URL url = new URL(StringUtils.format(URL_UNLIMIT_SQUARE, at)); |
|
91 |
// 建立http连接 |
|
92 |
conn = (HttpURLConnection) url.openConnection(); |
|
93 |
// 设置允许输出 |
|
94 |
conn.setDoOutput(true); |
|
95 |
conn.setDoInput(true); |
|
96 |
// 设置不用缓存 |
|
97 |
conn.setUseCaches(false); |
|
98 |
// 设置传递方式 |
|
99 |
conn.setRequestMethod("POST"); |
|
100 |
// 设置维持长连接 |
|
101 |
conn.setRequestProperty("Connection", "Keep-Alive"); |
|
102 |
// 设置文件字符集: |
|
103 |
conn.setRequestProperty("Charset", "UTF-8"); |
|
104 |
// 设置文件类型: |
|
105 |
conn.setRequestProperty("contentType", "application/json"); |
|
106 |
// 开始连接请求 |
|
107 |
conn.connect(); |
|
108 |
OutputStream out = conn.getOutputStream(); |
|
109 |
// 写入请求的字符串 |
|
110 |
out.write((obj.toString()).getBytes()); |
|
111 |
out.flush(); |
|
112 |
out.close(); |
|
113 |
|
|
114 |
// 请求返回的状态 |
|
115 |
if (conn.getResponseCode() == 200) { |
|
116 |
// 请求返回的数据 |
|
117 |
in = conn.getInputStream(); |
|
118 |
imgUrl = OSSUtil.uploadImg(fileName, in, keyId, keySecret, endPoint, bucket); |
|
119 |
conn.disconnect(); |
|
120 |
conn = null; |
|
121 |
} |
|
122 |
|
|
123 |
if (in != null) { |
|
124 |
in.close(); |
|
125 |
in = null; |
|
126 |
} |
|
127 |
|
|
128 |
if (conn != null) { |
|
129 |
conn.disconnect(); |
|
130 |
conn = null; |
|
131 |
} |
|
132 |
}catch (Exception e) |
|
133 |
{ |
|
134 |
e.printStackTrace(); |
|
135 |
|
|
136 |
if (in != null) { |
|
137 |
try { |
|
138 |
in.close(); |
|
139 |
}catch (Exception ep) |
|
140 |
{ |
|
141 |
ep.printStackTrace(); |
|
142 |
} |
|
143 |
in = null; |
|
144 |
} |
|
145 |
|
|
146 |
if (conn != null) { |
|
147 |
conn.disconnect(); |
|
148 |
conn = null; |
|
149 |
} |
|
150 |
} |
|
151 |
|
|
152 |
return imgUrl; |
|
153 |
} |
|
154 |
|
486aaa
|
155 |
/**生成无限二维码,返回临时文 |
C |
156 |
* @param at 微信token |
|
157 |
* @param scene 参数,只能32位,最好不要中文 |
|
158 |
* @param page 跳转链接 |
|
159 |
* @param width 宽度 |
|
160 |
* @param autoColor 默认false |
|
161 |
* @param lineColor 默认null |
|
162 |
* @param isHyaline 默认false |
|
163 |
* @return |
|
164 |
*/ |
|
165 |
public static File createUnlimitQrCode(String at, String scene, String page, int width, boolean autoColor, |
|
166 |
JSONObject lineColor, boolean isHyaline) throws IOException { |
|
167 |
String imgUrl = null; |
|
168 |
InputStream in = null; |
|
169 |
HttpURLConnection conn = null; |
|
170 |
|
|
171 |
//创建临时文件 |
|
172 |
File file = File.createTempFile("temp", ".jpg"); |
|
173 |
|
|
174 |
try { |
|
175 |
//生成发送数据 |
|
176 |
JSONObject obj = new JSONObject(); |
|
177 |
obj.put("scene", scene); |
|
178 |
obj.put("width", width); |
|
179 |
obj.put("page", page); |
|
180 |
obj.put("auto_color", autoColor); |
|
181 |
obj.put("line_color", lineColor); |
|
182 |
obj.put("is_hyaline", isHyaline); |
|
183 |
|
|
184 |
// 创建url资源 |
|
185 |
URL url = new URL(StringUtils.format(URL_UNLIMIT_SQUARE, at)); |
|
186 |
// 建立http连接 |
|
187 |
conn = (HttpURLConnection) url.openConnection(); |
|
188 |
// 设置允许输出 |
|
189 |
conn.setDoOutput(true); |
|
190 |
conn.setDoInput(true); |
|
191 |
// 设置不用缓存 |
|
192 |
conn.setUseCaches(false); |
|
193 |
// 设置传递方式 |
|
194 |
conn.setRequestMethod("POST"); |
|
195 |
// 设置维持长连接 |
|
196 |
conn.setRequestProperty("Connection", "Keep-Alive"); |
|
197 |
// 设置文件字符集: |
|
198 |
conn.setRequestProperty("Charset", "UTF-8"); |
|
199 |
// 设置文件类型: |
|
200 |
conn.setRequestProperty("contentType", "application/json"); |
|
201 |
// 开始连接请求 |
|
202 |
conn.connect(); |
|
203 |
OutputStream out = conn.getOutputStream(); |
|
204 |
// 写入请求的字符串 |
|
205 |
out.write((obj.toString()).getBytes()); |
|
206 |
out.flush(); |
|
207 |
out.close(); |
|
208 |
|
|
209 |
// 请求返回的状态 |
|
210 |
if (conn.getResponseCode() == 200) { |
|
211 |
// 请求返回的数据 |
|
212 |
in = conn.getInputStream(); |
|
213 |
//输入到临时文件 |
|
214 |
/*OutputStream os = new FileOutputStream(file); |
|
215 |
int bytesRead = 0; |
|
216 |
byte[] buffer = new byte[8192]; |
|
217 |
while ((bytesRead = in.read(buffer, 0, 8192)) != -1) { |
|
218 |
os.write(buffer, 0, bytesRead); |
|
219 |
}*/ |
|
220 |
FileUtils.copyInputStreamToFile(in, file); |
|
221 |
|
|
222 |
conn.disconnect(); |
|
223 |
conn = null; |
|
224 |
} |
|
225 |
|
|
226 |
if (in != null) { |
|
227 |
in.close(); |
|
228 |
in = null; |
|
229 |
} |
|
230 |
|
|
231 |
if (conn != null) { |
|
232 |
conn.disconnect(); |
|
233 |
conn = null; |
|
234 |
} |
|
235 |
}catch (Exception e) { |
|
236 |
e.printStackTrace(); |
|
237 |
if (in != null) { |
|
238 |
try { |
|
239 |
in.close(); |
|
240 |
}catch (Exception ep) { |
|
241 |
ep.printStackTrace(); |
|
242 |
} |
|
243 |
in = null; |
|
244 |
} |
|
245 |
|
|
246 |
if (conn != null) { |
|
247 |
conn.disconnect(); |
|
248 |
conn = null; |
|
249 |
} |
|
250 |
}finally { |
|
251 |
file.deleteOnExit(); |
|
252 |
} |
|
253 |
return file; |
|
254 |
} |
|
255 |
|
|
256 |
|
5c5945
|
257 |
/**生成小程序二维码工具(方形) |
E |
258 |
* path 二维码跳转链接 |
|
259 |
* width 二维码宽度,默认是430 |
|
260 |
* saveUrl 保存图片的全路径 |
|
261 |
* seeUrl 显示图片路径 |
|
262 |
* appid 小程序配置 |
|
263 |
* secret 小程序配置 |
|
264 |
* return 二维码显示图片链接 |
|
265 |
* @throws Exception |
|
266 |
* */ |
|
267 |
public static String twoCodeImgSquare(String at, String path,Integer width,String saveUrl, |
|
268 |
String seeUrl,String appid,String secret) throws Exception { |
|
269 |
|
|
270 |
if(!SimpleTool.checkNotNull(width)) { |
|
271 |
width = 430; |
|
272 |
} |
|
273 |
//生成发送数据 |
|
274 |
JSONObject obj = new JSONObject(); |
|
275 |
obj.put("path", path); |
|
276 |
obj.put("width", width); |
|
277 |
|
|
278 |
//二维码图片路径 |
|
279 |
String codeUrl = ""; |
|
280 |
codeUrl = HttpURLUtilMakeCodeImg(MAKE_TWOCODE_SQUARE_URL+at,obj,saveUrl,seeUrl); |
|
281 |
if(SimpleTool.checkNotNull(codeUrl)){ |
|
282 |
if(!codeUrl.startsWith("/")){ |
|
283 |
codeUrl = "/"+codeUrl; |
|
284 |
} |
|
285 |
} |
|
286 |
return codeUrl; |
|
287 |
} |
|
288 |
|
|
289 |
public static String twoCodeImgSquare(String at, String path,Integer width, String fileName, String keyId, |
|
290 |
String keySecret, String endPoint, String bucket) throws Exception { |
|
291 |
|
|
292 |
if(!SimpleTool.checkNotNull(width)) { |
|
293 |
width = 430; |
|
294 |
} |
|
295 |
//生成发送数据 |
|
296 |
JSONObject obj = new JSONObject(); |
|
297 |
obj.put("path", path); |
|
298 |
obj.put("width", width); |
|
299 |
|
|
300 |
String imgUrl = null; |
|
301 |
InputStream in = null; |
|
302 |
HttpURLConnection conn = null; |
|
303 |
|
|
304 |
try { |
|
305 |
|
|
306 |
// 创建url资源 |
|
307 |
URL url = new URL(MAKE_TWOCODE_SQUARE_URL + at); |
|
308 |
// 建立http连接 |
|
309 |
conn = (HttpURLConnection) url.openConnection(); |
|
310 |
// 设置允许输出 |
|
311 |
conn.setDoOutput(true); |
|
312 |
conn.setDoInput(true); |
|
313 |
// 设置不用缓存 |
|
314 |
conn.setUseCaches(false); |
|
315 |
// 设置传递方式 |
|
316 |
conn.setRequestMethod("POST"); |
|
317 |
// 设置维持长连接 |
|
318 |
conn.setRequestProperty("Connection", "Keep-Alive"); |
|
319 |
// 设置文件字符集: |
|
320 |
conn.setRequestProperty("Charset", "UTF-8"); |
|
321 |
// 设置文件类型: |
|
322 |
conn.setRequestProperty("contentType", "application/json"); |
|
323 |
// 开始连接请求 |
|
324 |
conn.connect(); |
|
325 |
OutputStream out = conn.getOutputStream(); |
|
326 |
// 写入请求的字符串 |
|
327 |
out.write((obj.toString()).getBytes()); |
|
328 |
out.flush(); |
|
329 |
out.close(); |
|
330 |
|
|
331 |
// 请求返回的状态 |
|
332 |
if (conn.getResponseCode() == 200) { |
|
333 |
// 请求返回的数据 |
|
334 |
in = conn.getInputStream(); |
|
335 |
imgUrl = OSSUtil.uploadImg(fileName, in, keyId, keySecret, endPoint, bucket); |
|
336 |
conn.disconnect(); |
|
337 |
conn = null; |
|
338 |
} |
|
339 |
|
|
340 |
if (in != null) { |
|
341 |
in.close(); |
|
342 |
in = null; |
|
343 |
} |
|
344 |
|
|
345 |
if (conn != null) { |
|
346 |
conn.disconnect(); |
|
347 |
conn = null; |
|
348 |
} |
|
349 |
}catch (Exception e) |
|
350 |
{ |
|
351 |
e.printStackTrace(); |
|
352 |
|
|
353 |
if (in != null) { |
|
354 |
try { |
|
355 |
in.close(); |
|
356 |
}catch (Exception ep) |
|
357 |
{ |
|
358 |
ep.printStackTrace(); |
|
359 |
} |
|
360 |
in = null; |
|
361 |
} |
|
362 |
|
|
363 |
if (conn != null) { |
|
364 |
conn.disconnect(); |
|
365 |
conn = null; |
|
366 |
} |
|
367 |
} |
|
368 |
|
|
369 |
return imgUrl; |
|
370 |
|
|
371 |
|
|
372 |
} |
|
373 |
|
|
374 |
/** 生成二维码的图片 返回图片路径 |
|
375 |
* urlx 执行链接 |
|
376 |
* objx 数据 |
|
377 |
* saveUrl 保存图片路径 |
|
378 |
* seeUrl 显示图片路径 |
|
379 |
* |
|
380 |
* **/ |
|
381 |
public static String HttpURLUtilMakeCodeImg(String urlx,JSONObject objx,String saveUrl,String seeUrl) throws IOException { |
|
382 |
String realpath = null; |
|
383 |
|
|
384 |
// 创建url资源 |
|
385 |
URL url = new URL(urlx); |
|
386 |
// 建立http连接 |
|
387 |
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
|
388 |
// 设置允许输出 |
|
389 |
conn.setDoOutput(true); |
|
390 |
|
|
391 |
conn.setDoInput(true); |
|
392 |
|
|
393 |
// 设置不用缓存 |
|
394 |
conn.setUseCaches(false); |
|
395 |
// 设置传递方式 |
|
396 |
conn.setRequestMethod("POST"); |
|
397 |
// 设置维持长连接 |
|
398 |
conn.setRequestProperty("Connection", "Keep-Alive"); |
|
399 |
// 设置文件字符集: |
|
400 |
conn.setRequestProperty("Charset", "UTF-8"); |
|
401 |
// 转换为字节数组 |
|
402 |
// byte[] data = (objx.toString()).getBytes(); |
|
403 |
// 设置文件长度 |
|
404 |
// conn.setRequestProperty("Content-Length",String.valueOf(data.length)); |
|
405 |
|
|
406 |
// 设置文件类型: |
|
407 |
conn.setRequestProperty("contentType", "application/json"); |
|
408 |
// 开始连接请求 |
|
409 |
conn.connect(); |
|
410 |
OutputStream out = conn.getOutputStream(); |
|
411 |
// 写入请求的字符串 |
|
412 |
out.write((objx.toString()).getBytes()); |
|
413 |
out.flush(); |
|
414 |
out.close(); |
|
415 |
|
|
416 |
// 请求返回的状态 |
|
417 |
if (conn.getResponseCode() == 200) { |
|
418 |
System.out.println("连接成功"); |
|
419 |
// 请求返回的数据 |
|
420 |
InputStream in = conn.getInputStream(); |
|
421 |
ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); |
|
422 |
byte[] temp = new byte[1024]; |
|
423 |
int rc = 0; |
|
424 |
while ((rc = in.read(temp, 0, 100)) > 0) { |
|
425 |
swapStream.write(temp, 0, rc); |
|
426 |
} |
|
427 |
byte[] buffer2 = swapStream.toByteArray(); |
|
428 |
String base64 = Base64.encodeBase64String(buffer2); |
|
429 |
|
|
430 |
//生成名称 |
|
431 |
java.util.Random r=new java.util.Random(); |
|
432 |
StringBuilder str=new StringBuilder();//定义变长字符串 |
|
433 |
for(int i=0;i<8;i++){ |
|
434 |
str.append(r.nextInt(10)); |
|
435 |
} |
|
436 |
Date date = new Date(); |
|
437 |
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss"); |
|
438 |
String imgName = sf.format(date); |
|
439 |
imgName = imgName+str.toString(); |
|
440 |
|
|
441 |
//文件保存位置 |
|
442 |
File saveDir = new File(saveUrl); |
|
443 |
if(!saveDir.exists()){ |
|
444 |
saveDir.mkdir(); |
|
445 |
} |
|
446 |
File file = new File(saveDir+"/"+imgName+".png"); |
|
447 |
FileOutputStream fos = new FileOutputStream(file); |
|
448 |
fos.write(buffer2); |
|
449 |
if(fos!=null){ |
|
450 |
fos.close(); |
|
451 |
} |
|
452 |
if(in!=null){ |
|
453 |
in.close(); |
|
454 |
} |
|
455 |
realpath = seeUrl+"/"+imgName+".png"; |
|
456 |
} else { |
|
457 |
System.out.println("no++"); |
|
458 |
} |
|
459 |
return realpath; |
|
460 |
} |
|
461 |
|
|
462 |
/** 请求http协议 获取信息工具 **/ |
|
463 |
private static JSONObject HttpURLUtil(String url,String data) { |
|
464 |
HttpURLConnection con = null; |
|
465 |
URL u; |
|
466 |
String wxMsgXml; |
|
467 |
JSONObject obj; |
|
468 |
try { |
|
469 |
u = new URL(url); |
|
470 |
con = (HttpURLConnection) u.openConnection(); |
|
471 |
con.setRequestMethod("POST"); |
|
472 |
con.setDoOutput(true); |
|
473 |
con.setDoInput(true); |
|
474 |
con.setUseCaches(false); |
|
475 |
con.setReadTimeout(5000); |
|
476 |
con.setRequestProperty("Charset", "UTF-8"); |
|
477 |
con.setRequestProperty("Content-Type", |
|
478 |
"application/x-www-form-urlencoded"); |
|
479 |
if (data != null) { |
|
480 |
OutputStream os = con.getOutputStream(); |
|
481 |
os.write(data.getBytes("utf-8")); |
|
482 |
} |
|
483 |
if (con.getResponseCode() != 200) |
|
484 |
throw new RuntimeException("请求url失败"); |
|
485 |
// 读取返回内容 |
|
486 |
wxMsgXml = IOUtils.toString(con.getInputStream(), StandardCharsets.UTF_8); |
|
487 |
// 判断返回参数是否正确/成功 |
|
488 |
obj = JSONObject.fromObject(wxMsgXml); |
|
489 |
// System.out.println("HttpURLUtil:"+wxMsgXml); |
|
490 |
} catch (Exception e) { |
|
491 |
e.printStackTrace(); |
|
492 |
obj = new JSONObject(); |
|
493 |
try { |
|
494 |
obj.put("status", 1); |
|
495 |
obj.put("errMsg", e.getMessage()); |
|
496 |
} catch (JSONException e1) { |
|
497 |
e1.printStackTrace(); |
|
498 |
} |
|
499 |
} finally { |
|
500 |
if (con != null) { |
|
501 |
con.disconnect(); |
|
502 |
} |
|
503 |
} |
|
504 |
return obj; |
|
505 |
} |
|
506 |
|
|
507 |
} |