This commit is contained in:
2025-07-27 08:59:08 +08:00
parent e9f44dd851
commit 563d83f849
35 changed files with 482 additions and 255 deletions

View File

@@ -2,7 +2,8 @@ package com.ycwl.basic.utils;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.ycwl.basic.utils.JacksonUtil;
import java.util.HashMap;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
@@ -37,22 +38,22 @@ public class WxMpUtil {
String token;
if (!System.getProperty("os.name").toLowerCase().startsWith("win")) {
String url = String.format(STABLE_ACCESS_TOKEN_URL, appId, appSecret);
JSONObject params = new JSONObject();
Map<String, Object> params = new HashMap<>();
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);
String response = HttpUtil.post(url, JacksonUtil.toJSONString(params));
Map<String, Object> jsonObject = JacksonUtil.parseObject(response, Map.class);
token = (String) jsonObject.get("access_token");
Date expireTime = new Date(System.currentTimeMillis() + ((Integer) jsonObject.get("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);
Map<String, Object> jsonObject = JacksonUtil.parseObject(response, Map.class);
token = (String) jsonObject.get("access_token");
Date expireTime = new Date(System.currentTimeMillis() + ((Integer) jsonObject.get("expires_in")) * 1000 / 2);
expireTimes.put(appId, expireTime);
}
return token;
@@ -64,12 +65,12 @@ public class WxMpUtil {
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));
JSONObject json = new JSONObject();
Map<String, Object> json = new HashMap<>();
json.put("env_version", envVersion);
json.put("path", path);
json.put("width", 1000);
try (HttpResponse response = HttpUtil.createPost(url).body(json.toJSONString()).header("Content-Type", "application/json").execute()) {
try (HttpResponse response = HttpUtil.createPost(url).body(JacksonUtil.toJSONString(json)).header("Content-Type", "application/json").execute()) {
if (response.getStatus() != 200) {
throw new Exception("获取小程序二维码失败,原因为:" + response.body());
}
@@ -86,12 +87,12 @@ 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();
Map<String, Object> json = new HashMap<>();
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()) {
try (HttpResponse response = HttpUtil.createPost(url).body(JacksonUtil.toJSONString(json)).header("Content-Type", "application/json").execute()) {
if (response.getStatus() != 200) {
throw new Exception("获取小程序二维码失败,原因为:" + response.body());
}
@@ -108,34 +109,34 @@ 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));
JSONObject json = new JSONObject();
Map<String, Object> json = new HashMap<>();
json.put("path", path);
json.put("query", query);
try (HttpResponse response = HttpUtil.createPost(url).body(json.toJSONString()).header("Content-Type", "application/json").execute()) {
try (HttpResponse response = HttpUtil.createPost(url).body(JacksonUtil.toJSONString(json)).header("Content-Type", "application/json").execute()) {
String responseStr = response.body();
JSONObject jsonObject = JSONObject.parseObject(responseStr);
if (jsonObject.getInteger("errcode") != 0) {
throw new Exception("获取url_link失败,原因为:" + jsonObject.getString("errmsg"));
Map<String, Object> jsonObject = JacksonUtil.parseObject(responseStr, Map.class);
if ((Integer) jsonObject.get("errcode") != 0) {
throw new Exception("获取url_link失败,原因为:" + (String) jsonObject.get("errmsg"));
}
return jsonObject.getString("url_link");
return (String) jsonObject.get("url_link");
}
}
public static String getUserPhone(String appId, String appSecret, String code) throws Exception {
String url = String.format(GET_USER_PHONE_URL, getAccessToken(appId, appSecret));
JSONObject json = new JSONObject();
Map<String, Object> json = new HashMap<>();
json.put("code", code);
try (HttpResponse response = HttpUtil.createPost(url).body(json.toJSONString()).header("Content-Type", "application/json").execute()) {
try (HttpResponse response = HttpUtil.createPost(url).body(JacksonUtil.toJSONString(json)).header("Content-Type", "application/json").execute()) {
String responseStr = response.body();
JSONObject jsonObject = JSONObject.parseObject(responseStr);
if (jsonObject.getInteger("errcode") != 0) {
throw new Exception("获取用户手机号失败,原因为:" + jsonObject.getString("errmsg"));
Map<String, Object> jsonObject = JacksonUtil.parseObject(responseStr, Map.class);
if ((Integer) jsonObject.get("errcode") != 0) {
throw new Exception("获取用户手机号失败,原因为:" + (String) jsonObject.get("errmsg"));
}
return jsonObject.getJSONObject("phone_info").getString("phoneNumber");
return (String) ((Map<String, Object>) jsonObject.get("phone_info")).get("phoneNumber");
}
}