From 8b957ee96d5ea0210cc85d1386c9ccd6535a519d Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Fri, 18 Jul 2025 16:39:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81stableToken=EF=BC=8C=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=97=A0=E9=99=90WXACode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/ycwl/basic/utils/WxMpUtil.java | 52 ++++++++++++++++--- 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/ycwl/basic/utils/WxMpUtil.java b/src/main/java/com/ycwl/basic/utils/WxMpUtil.java index 4dd7f94..fe9f76c 100644 --- a/src/main/java/com/ycwl/basic/utils/WxMpUtil.java +++ b/src/main/java/com/ycwl/basic/utils/WxMpUtil.java @@ -3,6 +3,7 @@ package com.ycwl.basic.utils; import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpUtil; import com.alibaba.fastjson.JSONObject; +import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.util.Date; @@ -12,8 +13,10 @@ import java.util.concurrent.locks.ReentrantLock; public class WxMpUtil { private static final String GET_WXA_CODE_URL = "https://api.weixin.qq.com/wxa/getwxacode?access_token=%s"; + private static final String GET_WXA_CODE_UNLIMITED_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 final String STABLE_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/stable_token?grant_type=client_credential&appid=%s&secret=%s&force_refresh=false"; public static final String GET_USER_PHONE_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=%s"; private static final Map tokens = new ConcurrentHashMap<>(); private static final Map expireTimes = new ConcurrentHashMap<>(); @@ -31,12 +34,27 @@ public class WxMpUtil { tokens.remove(appId); } return tokens.computeIfAbsent(appId, (k) -> { - String url = String.format(ACCESS_TOKEN_URL, appId, appSecret); - String response = HttpUtil.get(url); - JSONObject jsonObject = JSONObject.parseObject(response); - String token = jsonObject.getString("access_token"); - Date expireTime = new Date(System.currentTimeMillis() + jsonObject.getInteger("expires_in") * 1000 / 2); - expireTimes.put(appId, expireTime); + String token; + if (!System.getProperty("os.name").toLowerCase().startsWith("win")) { + String url = String.format(STABLE_ACCESS_TOKEN_URL, appId, appSecret); + JSONObject params = new JSONObject(); + params.put("grant_type", "client_credential"); + params.put("appid", appId); + params.put("secret", appSecret); + params.put("force_refresh", false); + String response = HttpUtil.post(url, params.toJSONString()); + JSONObject jsonObject = JSONObject.parseObject(response); + token = jsonObject.getString("access_token"); + Date expireTime = new Date(System.currentTimeMillis() + jsonObject.getInteger("expires_in") * 1000 / 2); + expireTimes.put(appId, expireTime); + } else { + String url = String.format(ACCESS_TOKEN_URL, appId, appSecret); + String response = HttpUtil.get(url); + JSONObject jsonObject = JSONObject.parseObject(response); + token = jsonObject.getString("access_token"); + Date expireTime = new Date(System.currentTimeMillis() + jsonObject.getInteger("expires_in") * 1000 / 2); + expireTimes.put(appId, expireTime); + } return token; }); } finally { @@ -66,6 +84,28 @@ public class WxMpUtil { } } + public static void generateUnlimitedWXAQRCode(String appId, String appSecret, String path, String scene, File targetFile) throws Exception { + String url = String.format(GET_WXA_CODE_UNLIMITED_URL, getAccessToken(appId, appSecret)); + JSONObject json = new JSONObject(); + json.put("page", path); + json.put("scene", scene); + json.put("check_path", false); + + try (HttpResponse response = HttpUtil.createPost(url).body(json.toJSONString()).header("Content-Type", "application/json").execute()) { + if (response.getStatus() != 200) { + throw new Exception("获取小程序二维码失败,原因为:" + response.body()); + } + InputStream inputStream = response.bodyStream(); + try (FileOutputStream fos = new FileOutputStream(targetFile)) { + int len; + byte[] buffer = new byte[1024]; + while ((len = inputStream.read(buffer)) != -1) { + fos.write(buffer, 0, len); + } + } + } + } + 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)); JSONObject json = new JSONObject();