You've already forked FrameTour-BE
支持stableToken,支持无限WXACode
This commit is contained in:
@@ -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<String, String> tokens = new ConcurrentHashMap<>();
|
||||
private static final Map<String, Date> 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();
|
||||
|
Reference in New Issue
Block a user