This commit is contained in:
2025-03-17 18:35:06 +08:00
parent 7e8eebdef5
commit 180ba67de8
5 changed files with 210 additions and 2 deletions

View File

@ -14,7 +14,8 @@ 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";
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 = "";
@ -61,6 +62,33 @@ 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("获取小程序码失败");
}
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 null;
}
public static void main(String[] args) throws Exception {
generateWXAQRCode("wxe7ff26af70bfc37c", "5252fbbc68513bc77b7cc0052b9f9695", "trial", "pages/home/index?scenicId=3955650120997015552", "sxlj_t.jpg");
}