This commit is contained in:
2025-01-16 18:28:04 +08:00
parent 0bba613001
commit bbcbdd2839
24 changed files with 436 additions and 111 deletions

View File

@ -1,5 +1,6 @@
package com.ycwl.basic.utils;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
@ -10,15 +11,35 @@ import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.FileOutputStream;
import java.util.Date;
public class WxMpUtil {
private static final String GET_WXA_CODE_URL = "https://api.weixin.qq.com/wxa/getwxacode?access_token=%s";
public static void generateWXAQRCode(String accessToken, String path, String filePath) throws Exception {
String url = String.format(GET_WXA_CODE_URL, accessToken);
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();
private static String getAccessToken(String appId, String appSecret) {
if (ACCESS_TOKEN != null && !ACCESS_TOKEN.isEmpty()) {
if (expireTime.getTime() > System.currentTimeMillis()) {
return ACCESS_TOKEN;
}
}
String url = String.format(ACCESS_TOKEN_URL, appId, appSecret);
String response = HttpUtil.get(url);
JSONObject jsonObject = JSONObject.parseObject(response);
ACCESS_TOKEN = jsonObject.getString("access_token");
expireTime = new Date(System.currentTimeMillis() + jsonObject.getInteger("expires_in") * 1000);
return ACCESS_TOKEN;
}
public static void generateWXAQRCode(String appId, String appSecret, 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("env_version", "trial");
json.put("path", path);
json.put("width", 430);
StringEntity entity = new StringEntity(json.toJSONString(), "utf-8");
@ -36,4 +57,7 @@ public class WxMpUtil {
}
}
public static void main(String[] args) throws Exception {
generateWXAQRCode("wxe7ff26af70bfc37c", "5252fbbc68513bc77b7cc0052b9f9695", "pages/home/index?scenicId=3942994647776890880", "a.jpg");
}
}