乱七八糟的调整,wx接口对其

This commit is contained in:
Jerry Yan 2025-03-25 10:49:28 +08:00
parent f9a8a5f20e
commit 9ec6825372
2 changed files with 26 additions and 49 deletions

View File

@ -1,22 +1,16 @@
package com.ycwl.basic.utils; package com.ycwl.basic.utils;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil; import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject; 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.FileOutputStream;
import java.io.InputStream;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
public class WxMpUtil { 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_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 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 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 String ACCESS_TOKEN = "";
private static Date expireTime = new Date(); 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 { 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)); String url = String.format(GET_WXA_CODE_URL, getAccessToken(appId, appSecret));
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put("scene", "wxa_code_generate");
json.put("env_version", envVersion); json.put("env_version", envVersion);
json.put("path", path); json.put("path", path);
json.put("width", 1000); 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)) { try (HttpResponse response = HttpUtil.createPost(url).body(json.toJSONString()).header("Content-Type", "application/json").execute()) {
if (response.getStatusLine().getStatusCode() != 200) { if (response.getStatus() != 200) {
expireTime = new Date(); throw new Exception("获取小程序二维码失败,原因为:" + response.body());
throw new Exception("获取小程序码失败");
} }
HttpEntity responseEntity = response.getEntity(); InputStream inputStream = response.bodyStream();
if (responseEntity != null) { try (FileOutputStream fos = new FileOutputStream(filePath)) {
byte[] bytes = EntityUtils.toByteArray(responseEntity); int len;
try (FileOutputStream fos = new FileOutputStream(filePath)) { byte[] buffer = new byte[1024];
fos.write(bytes); 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 { 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)); String url = String.format(GET_URL_LICK_URL, getAccessToken(appId, appSecret));
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put("path", path); json.put("path", path);
json.put("query", query); json.put("query", query);
StringEntity entity = new StringEntity(json.toJSONString(), "utf-8");
httpPost.setEntity(entity); try (HttpResponse response = HttpUtil.createPost(url).body(json.toJSONString()).header("Content-Type", "application/json").execute()) {
httpPost.setHeader("Content-Type", "application/json"); String responseStr = response.body();
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
if (response.getStatusLine().getStatusCode() != 200) { JSONObject jsonObject = JSONObject.parseObject(responseStr);
expireTime = new Date(); if (jsonObject.getInteger("errcode") != 0) {
throw new Exception("获取小程序码失败"); throw new Exception("获取url_link失败原因为" + jsonObject.getString("errmsg"));
} }
HttpEntity responseEntity = response.getEntity(); return jsonObject.getString("url_link");
if (responseEntity != null) {
String responseStr = EntityUtils.toString(responseEntity);
JSONObject jsonObject = JSONObject.parseObject(responseStr);
return jsonObject.getString("url_link");
}
} catch (Exception e) {
e.printStackTrace();
} }
return null;
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
generateWXAQRCode("wxe7ff26af70bfc37c", "5252fbbc68513bc77b7cc0052b9f9695", "trial", "pages/home/index?scenicId=3955650120997015552", "sxlj_t.jpg"); generateWXAQRCode("wxe7ff26af70bfc37c", "5252fbbc68513bc77b7cc0052b9f9695", "trial", "pages/home/index?scenicId=3955650120997015552", "sxlj_t.jpg");
} }
} }

View File

@ -1,8 +1,5 @@
server: server:
port: 8030 port: 8030
tomcat:
threads:
min-spare: 64
spring: spring:
application: application:
@ -22,8 +19,8 @@ spring:
password: ZhEnTuAi2024zHeNtUaI password: ZhEnTuAi2024zHeNtUaI
hikari: hikari:
connection-timeout: 10000 # 等待连接池分配连接的最大时长毫秒超过这个时长还没可用的连接则发生SQLException 默认:30秒 connection-timeout: 10000 # 等待连接池分配连接的最大时长毫秒超过这个时长还没可用的连接则发生SQLException 默认:30秒
minimum-idle: 10 # 最小连接数 minimum-idle: 4 # 最小连接数
maximum-pool-size: 50 # 最大连接数 maximum-pool-size: 64 # 最大连接数
auto-commit: true # 事务自动提交 auto-commit: true # 事务自动提交
idle-timeout: 120000 # 连接超时的最大时长(毫秒) idle-timeout: 120000 # 连接超时的最大时长(毫秒)
pool-name: DateSourceHikariCP # 连接池名字 pool-name: DateSourceHikariCP # 连接池名字
@ -39,8 +36,8 @@ spring:
password: '' password: ''
jedis: jedis:
pool: pool:
max-active: 100 # 连接池最大连接数(使用负值表示没有限制) max-active: 64 # 连接池最大连接数(使用负值表示没有限制)
min-idle: 1 # 连接池中的最小空闲连接 min-idle: 2 # 连接池中的最小空闲连接
timeout: 1000 timeout: 1000
# 配置用户头像存放静态资源文件夹 # 配置用户头像存放静态资源文件夹
resources: resources: