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

@@ -274,4 +274,238 @@ public class JacksonUtil {
public static ObjectMapper getObjectMapper() {
return objectMapper;
}
// ==== FastJSON 兼容性方法 ====
/**
* 兼容 FastJSON 的 JSON.toJSONString() 方法
* @param obj 要转换的对象
* @return JSON字符串
*/
public static String toJSONString(Object obj) {
return toJson(obj);
}
/**
* 兼容 FastJSON 的 JSON.parseObject() 方法
* @param json JSON字符串
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 转换后的对象
*/
public static <T> T parseObject(String json, Class<T> clazz) {
return fromJson(json, clazz);
}
/**
* 兼容 FastJSON 的 JSON.parseArray() 方法
* @param json JSON字符串
* @param clazz 数组元素类型
* @param <T> 泛型类型
* @return List对象
*/
public static <T> List<T> parseArray(String json, Class<T> clazz) {
return fromJsonToList(json, clazz);
}
/**
* 带TypeReference的parseObject方法,支持复杂泛型
* @param json JSON字符串
* @param typeReference 类型引用
* @param <T> 泛型类型
* @return 转换后的对象
*/
public static <T> T parseObject(String json, TypeReference<T> typeReference) {
return fromJson(json, typeReference);
}
/**
* 将Map或其他对象转换为指定类型,兼容fastjson的toJavaObject方法
* @param obj 源对象
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 转换后的对象
*/
public static <T> T toJavaObject(Object obj, Class<T> clazz) {
if (obj == null) {
return null;
}
try {
// 如果对象已经是目标类型,直接返回
if (clazz.isInstance(obj)) {
return clazz.cast(obj);
}
// 先转为JSON字符串,再转为目标对象
String json = toJson(obj);
return fromJson(json, clazz);
} catch (Exception e) {
throw new RuntimeException("对象转换失败", e);
}
}
/**
* 兼容 FastJSON 的 JSONObject.parseObject() 方法
* @param json JSON字符串
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 转换后的对象
*/
public static <T> T parseObjectCompat(String json, Class<T> clazz) {
return fromJson(json, clazz);
}
/**
* 兼容 FastJSON 的 JSONObject.toJSONString() 方法
* @param obj 要转换的对象
* @return JSON字符串
*/
public static String toJSONStringCompat(Object obj) {
return toJson(obj);
}
/**
* 兼容 FastJSON 的 JSONObject.parseArray() 方法
* @param json JSON字符串
* @param clazz 数组元素类型
* @param <T> 泛型类型
* @return List对象
*/
public static <T> List<T> parseArrayCompat(String json, Class<T> clazz) {
return fromJsonToList(json, clazz);
}
/**
* 简单的JSON对象包装类,兼容部分JSONObject的用法
*/
public static class JSONObjectCompat {
private final JsonNode node;
private JSONObjectCompat(JsonNode node) {
this.node = node;
}
/**
* 解析JSON字符串为JSONObjectCompat
* @param json JSON字符串
* @return JSONObjectCompat对象
*/
public static JSONObjectCompat parseObject(String json) {
return new JSONObjectCompat(getJsonNode(json));
}
/**
* 获取JSON数组
* @param key 键名
* @return JSON数组的List表示
*/
public List<JSONObjectCompat> getJSONArray(String key) {
JsonNode arrayNode = node.get(key);
if (arrayNode != null && arrayNode.isArray()) {
List<JSONObjectCompat> result = new java.util.ArrayList<>();
for (JsonNode item : arrayNode) {
result.add(new JSONObjectCompat(item));
}
return result;
}
return null;
}
/**
* 获取JSON对象
* @param index 索引
* @return JSONObjectCompat对象
*/
public JSONObjectCompat getJSONObject(int index) {
if (node.isArray() && index < node.size()) {
return new JSONObjectCompat(node.get(index));
}
return null;
}
/**
* 获取字符串值
* @param key 键名
* @return 字符串值
*/
public String getString(String key) {
JsonNode valueNode = node.get(key);
return valueNode != null && !valueNode.isNull() ? valueNode.asText() : null;
}
/**
* 获取整数值
* @param key 键名
* @return 整数值
*/
public Integer getInteger(String key) {
JsonNode valueNode = node.get(key);
return valueNode != null && !valueNode.isNull() ? valueNode.asInt() : null;
}
/**
* 获取长整数值
* @param key 键名
* @return 长整数值
*/
public Long getLong(String key) {
JsonNode valueNode = node.get(key);
return valueNode != null && !valueNode.isNull() ? valueNode.asLong() : null;
}
/**
* 获取双精度浮点数值
* @param key 键名
* @return 双精度浮点数值
*/
public Double getDouble(String key) {
JsonNode valueNode = node.get(key);
return valueNode != null && !valueNode.isNull() ? valueNode.asDouble() : null;
}
/**
* 获取布尔值
* @param key 键名
* @return 布尔值
*/
public Boolean getBoolean(String key) {
JsonNode valueNode = node.get(key);
return valueNode != null && !valueNode.isNull() ? valueNode.asBoolean() : null;
}
/**
* 获取键集合
* @return 键的Set集合
*/
public java.util.Set<String> keySet() {
if (node.isObject()) {
java.util.Set<String> keys = new java.util.HashSet<>();
node.fieldNames().forEachRemaining(keys::add);
return keys;
}
return java.util.Collections.emptySet();
}
/**
* 检查是否包含指定键
* @param key 键名
* @return 是否包含
*/
public boolean containsKey(String key) {
return node.has(key);
}
/**
* 转换为Java对象
* @param clazz 目标类型
* @param <T> 泛型类型
* @return Java对象
*/
public <T> T toJavaObject(Class<T> clazz) {
try {
return objectMapper.treeToValue(node, clazz);
} catch (JsonProcessingException e) {
throw new RuntimeException("JSON节点转对象失败", e);
}
}
}
}

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");
}
}