diff --git a/src/main/java/com/ycwl/basic/utils/WxMpUtil.java b/src/main/java/com/ycwl/basic/utils/WxMpUtil.java index 69f34b6..1cec290 100644 --- a/src/main/java/com/ycwl/basic/utils/WxMpUtil.java +++ b/src/main/java/com/ycwl/basic/utils/WxMpUtil.java @@ -1,22 +1,16 @@ package com.ycwl.basic.utils; +import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpUtil; import com.alibaba.fastjson.JSONObject; -import org.apache.http.HttpEntity; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; -import org.apache.http.util.EntityUtils; - import java.io.FileOutputStream; +import java.io.InputStream; import java.util.Date; +import java.util.HashMap; public class WxMpUtil { private static final String GET_WXA_CODE_URL = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s"; private static final String GET_URL_LICK_URL = "https://api.weixin.qq.com/wxa/generate_urllink?access_token=%s"; - private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s"; private static String ACCESS_TOKEN = ""; private static Date expireTime = new Date(); @@ -37,26 +31,22 @@ public class WxMpUtil { public static void generateWXAQRCode(String appId, String appSecret, String envVersion, String path, String filePath) throws Exception { String url = String.format(GET_WXA_CODE_URL, getAccessToken(appId, appSecret)); - CloseableHttpClient httpClient = HttpClients.createDefault(); - HttpPost httpPost = new HttpPost(url); JSONObject json = new JSONObject(); + json.put("scene", "wxa_code_generate"); json.put("env_version", envVersion); json.put("path", path); json.put("width", 1000); - StringEntity entity = new StringEntity(json.toJSONString(), "utf-8"); - httpPost.setEntity(entity); - httpPost.setHeader("Content-Type", "application/json"); - try (CloseableHttpResponse response = httpClient.execute(httpPost)) { - if (response.getStatusLine().getStatusCode() != 200) { - expireTime = new Date(); - throw new Exception("获取小程序码失败"); + try (HttpResponse response = HttpUtil.createPost(url).body(json.toJSONString()).header("Content-Type", "application/json").execute()) { + if (response.getStatus() != 200) { + throw new Exception("获取小程序二维码失败,原因为:" + response.body()); } - HttpEntity responseEntity = response.getEntity(); - if (responseEntity != null) { - byte[] bytes = EntityUtils.toByteArray(responseEntity); - try (FileOutputStream fos = new FileOutputStream(filePath)) { - fos.write(bytes); + InputStream inputStream = response.bodyStream(); + try (FileOutputStream fos = new FileOutputStream(filePath)) { + int len; + byte[] buffer = new byte[1024]; + while ((len = inputStream.read(buffer)) != -1) { + fos.write(buffer, 0, len); } } } @@ -64,32 +54,22 @@ public class WxMpUtil { public static String generateUrlLink(String appId, String appSecret, String path, String query) throws Exception { String url = String.format(GET_URL_LICK_URL, getAccessToken(appId, appSecret)); - CloseableHttpClient httpClient = HttpClients.createDefault(); - HttpPost httpPost = new HttpPost(url); JSONObject json = new JSONObject(); json.put("path", path); json.put("query", query); - StringEntity entity = new StringEntity(json.toJSONString(), "utf-8"); - httpPost.setEntity(entity); - httpPost.setHeader("Content-Type", "application/json"); - try (CloseableHttpResponse response = httpClient.execute(httpPost)) { - if (response.getStatusLine().getStatusCode() != 200) { - expireTime = new Date(); - throw new Exception("获取小程序码失败"); + + try (HttpResponse response = HttpUtil.createPost(url).body(json.toJSONString()).header("Content-Type", "application/json").execute()) { + String responseStr = response.body(); + + JSONObject jsonObject = JSONObject.parseObject(responseStr); + if (jsonObject.getInteger("errcode") != 0) { + throw new Exception("获取url_link失败,原因为:" + jsonObject.getString("errmsg")); } - HttpEntity responseEntity = response.getEntity(); - if (responseEntity != null) { - String responseStr = EntityUtils.toString(responseEntity); - JSONObject jsonObject = JSONObject.parseObject(responseStr); - return jsonObject.getString("url_link"); - } - } catch (Exception e) { - e.printStackTrace(); + return jsonObject.getString("url_link"); } - return null; } public static void main(String[] args) throws Exception { generateWXAQRCode("wxe7ff26af70bfc37c", "5252fbbc68513bc77b7cc0052b9f9695", "trial", "pages/home/index?scenicId=3955650120997015552", "sxlj_t.jpg"); } -} +} \ No newline at end of file diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index 0289001..36bf363 100644 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -1,8 +1,5 @@ server: port: 8030 - tomcat: - threads: - min-spare: 64 spring: application: @@ -22,8 +19,8 @@ spring: password: ZhEnTuAi2024zHeNtUaI hikari: connection-timeout: 10000 # 等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLException, 默认:30秒 - minimum-idle: 10 # 最小连接数 - maximum-pool-size: 50 # 最大连接数 + minimum-idle: 4 # 最小连接数 + maximum-pool-size: 64 # 最大连接数 auto-commit: true # 事务自动提交 idle-timeout: 120000 # 连接超时的最大时长(毫秒) pool-name: DateSourceHikariCP # 连接池名字 @@ -39,8 +36,8 @@ spring: password: '' jedis: pool: - max-active: 100 # 连接池最大连接数(使用负值表示没有限制) - min-idle: 1 # 连接池中的最小空闲连接 + max-active: 64 # 连接池最大连接数(使用负值表示没有限制) + min-idle: 2 # 连接池中的最小空闲连接 timeout: 1000 # 配置用户头像存放静态资源文件夹 resources: