perf:移除hutool-json,直接使用fastjson

This commit is contained in:
曾文豪
2024-08-23 15:04:20 +08:00
parent 9bab4cdb25
commit 4bcae2f8d1
23 changed files with 176 additions and 116 deletions

View File

@@ -1,12 +1,12 @@
package com.tiesheng.platform.config.ding;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.LogFactory;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.tiesheng.platform.config.ding.bean.*;
import com.tiesheng.util.exception.ApiException;
import com.tiesheng.util.service.TsCacheService;
@@ -80,7 +80,7 @@ public class PlatformDingConfig {
Response response = OkHttpUtil.ofHttpClient().build().newCall(request).execute();
if (response.isSuccessful() && response.body() != null) {
String rawBody = response.body().string();
DingResponse<T> bean = JSONUtil.toBean(rawBody, typeReference, true);
DingResponse<T> bean = JSON.parseObject(rawBody, typeReference);
bean.setRawBody(rawBody);
return bean;
} else {
@@ -116,8 +116,8 @@ public class PlatformDingConfig {
query.put("appkey", dingConfigBean.getAppKey());
query.put("appsecret", dingConfigBean.getAppSecret());
String response = OkHttpUtil.get("https://oapi.dingtalk.com/gettoken", query);
JSONObject respJson = JSONUtil.parseObj(response);
accessToken = respJson.getStr("access_token");
JSONObject respJson = JSON.parseObject(response);
accessToken = respJson.getString("access_token");
TsCacheService.of().put(CACHE_ACCESS_TOKEN + dingConfigBean.getAppKey(),
accessToken, respJson.getLong("expires_in"));
@@ -174,8 +174,12 @@ public class PlatformDingConfig {
* @see <a href="https://open.dingtalk.com/document/isvapp-server/obtain-the-userid-of-a-user-by-using-the-log-free"></a>
*/
public DingUserSimple getUserIdByCode(String service, String code) {
JSONObject object = new JSONObject();
object.put("code", code);
DingResponse<DingUserSimple> resp = doRequest(service, "https://oapi.dingtalk.com/topapi/v2/user/getuserinfo",
JSONUtil.createObj().putOpt("code", code), new TypeReference<DingResponse<DingUserSimple>>() {
object, new TypeReference<DingResponse<DingUserSimple>>() {
});
return resp.getResult();
}
@@ -192,9 +196,13 @@ public class PlatformDingConfig {
public DingUserInfo topapiV2UserGet(String service, String ddUserId) {
DingConfigBean dingConfigBean = getConfigBean(service);
JSONObject object = new JSONObject();
object.put("userid", ddUserId);
DingUserInfo userInfo = doRequest(service, "https://oapi.dingtalk.com/topapi/v2/user/get",
JSONUtil.createObj().putOpt("userid", ddUserId), new TypeReference<DingResponse<DingUserInfo>>() {
object, new TypeReference<DingResponse<DingUserInfo>>() {
}).getResult();
// 设置一下job_number
userInfo.setJobNumber(userInfo.getJobNumber());
userInfo.setAppId(dingConfigBean.getAppKey());
@@ -214,8 +222,11 @@ public class PlatformDingConfig {
return new ArrayList<>();
}
JSONObject object = new JSONObject();
object.put("dept_id", deptId);
return doRequest(service, "https://oapi.dingtalk.com/topapi/v2/department/listsub",
JSONUtil.createObj().putOpt("dept_id", deptId), new TypeReference<DingResponse<List<DingDeptVo>>>() {
object, new TypeReference<DingResponse<List<DingDeptVo>>>() {
}).getResult();
}
@@ -232,9 +243,13 @@ public class PlatformDingConfig {
return DingUserListVo.fail();
}
JSONObject object = new JSONObject();
object.put("dept_id", deptId);
object.put("cursor", cursor);
object.put("size", 100);
return doRequest(service, "https://oapi.dingtalk.com/topapi/v2/user/list",
JSONUtil.createObj().putOpt("dept_id", deptId).putOpt("cursor", cursor).putOpt("size", 100),
new TypeReference<DingResponse<DingUserListVo>>() {
object, new TypeReference<DingResponse<DingUserListVo>>() {
}).getResult();
}
@@ -293,24 +308,28 @@ public class PlatformDingConfig {
DingConfigBean configBean = getConfigBean(service);
JSONObject msg = new JSONObject();
if (StrUtil.isEmpty(actionUrl)) {
msg.set("msgtype", "markdown");
msg.set("markdown", JSONUtil.createObj()
.set("title", title).set("text", markdown)
);
JSONObject markdownObj = new JSONObject();
markdownObj.put("title", title);
markdownObj.put("text", markdown);
msg.put("msgtype", "markdown");
msg.put("markdown", markdownObj);
} else {
msg.set("msgtype", "action_card");
msg.set("action_card", JSONUtil.createObj()
.set("title", title).set("markdown", markdown)
.set("single_title", "点击查看").set("single_url", actionUrl)
);
JSONObject actionCard = new JSONObject();
actionCard.put("title", title);
actionCard.put("markdown", markdown);
actionCard.put("single_title", "点击查看");
actionCard.put("single_url", actionUrl);
msg.put("msgtype", "action_card");
msg.put("action_card", actionCard);
}
JSONObject body = new JSONObject();
body.putOpt("agent_id", configBean.getAgentId());
body.putOpt("userid_list", CollUtil.join(userIds, ","));
body.putOpt("msg", msg);
body.put("agent_id", configBean.getAgentId());
body.put("userid_list", CollUtil.join(userIds, ","));
body.put("msg", msg);
return doRequest(service, "https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2", body,
new TypeReference<DingResponse<String>>() {
});

View File

@@ -1,6 +1,7 @@
package com.tiesheng.platform.config.ding.bean;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONPath;
import java.util.Objects;
@@ -41,7 +42,8 @@ public class DingResponse<T> {
* @return
*/
public <E> E getRawValue(String path, Class<E> tClass) {
return JSONUtil.parse(getRawBody()).getByPath(path, tClass);
JSONPath jsonPath = JSONPath.compile(path);
return jsonPath.eval(JSON.parseObject(getRawBody()), tClass);
}
///////////////////////////////////////////////////////////////////////////

View File

@@ -2,8 +2,8 @@ package com.tiesheng.platform.config.wxmini;
import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tiesheng.platform.config.wxmp.bean.WxConfigBean;
import com.tiesheng.util.exception.ApiException;
import com.tiesheng.util.service.http.OkHttpUtil;
@@ -56,8 +56,8 @@ public class PlatformWxminiConfig {
+ "&secret=" + configBean.getAppSecret()
+ "&js_code=" + code + "&grant_type=authorization_code");
JSONObject object = JSONUtil.parseObj(body);
return object.getStr("openid");
JSONObject object = JSON.parseObject(body);
return object.getString("openid");
}
/**
@@ -72,8 +72,8 @@ public class PlatformWxminiConfig {
+ "?grant_type=client_credential&appid=" + configBean.getAppId()
+ "&secret=" + configBean.getAppSecret());
JSONObject object = JSONUtil.parseObj(body);
return object.getStr("access_token");
JSONObject object = JSON.parseObject(body);
return object.getString("access_token");
}

View File

@@ -4,9 +4,9 @@ package com.tiesheng.platform.config.wxmp;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.LogFactory;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tiesheng.platform.config.wxmp.bean.WxConfigBean;
import com.tiesheng.platform.config.wxmp.bean.WxJsapiSignature;
import com.tiesheng.platform.config.wxmp.bean.WxOAuth2AccessToken;
@@ -70,8 +70,8 @@ public class PlatformWxmpConfig {
query.put("appid", configBean.getAppId());
query.put("secret", configBean.getAppSecret());
String response = OkHttpUtil.get("https://api.weixin.qq.com/cgi-bin/token", query);
JSONObject respJson = JSONUtil.parseObj(response);
accessToken = respJson.getStr("access_token");
JSONObject respJson = JSON.parseObject(response);
accessToken = respJson.getString("access_token");
TsCacheService.of().put(CACHE_ACCESS_TOKEN + configBean.getAppId(), accessToken,
respJson.getLong("expires_in"));
}
@@ -93,8 +93,8 @@ public class PlatformWxmpConfig {
query.put("type", "jsapi");
String response = OkHttpUtil.get("https://api.weixin.qq.com/cgi-bin/ticket/getticket", query);
LogFactory.get().info("getJsapiTicket: " + response);
JSONObject respJson = JSONUtil.parseObj(response);
jsapiTicket = respJson.getStr("ticket");
JSONObject respJson = JSON.parseObject(response);
jsapiTicket = respJson.getString("ticket");
TsCacheService.of().put(CACHE_JSAPI_TICKET + configBean.getAppId(), jsapiTicket,
respJson.getLong("expires_in"));
}

View File

@@ -1,7 +1,7 @@
package com.tiesheng.platform.config.wxmp.bean;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tiesheng.util.service.http.OkHttpUtil;
/**
@@ -15,11 +15,11 @@ public class WxOAuth2AccessToken {
public static WxOAuth2AccessToken create(String appId, String secret, String code) {
String response = OkHttpUtil.get("https://api.weixin.qq.com/sns/oauth2/access_token" +
"?appid=" + appId + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code");
JSONObject respJson = JSONUtil.parseObj(response);
JSONObject respJson = JSON.parseObject(response);
WxOAuth2AccessToken oAuth2AccessToken = new WxOAuth2AccessToken();
oAuth2AccessToken.setOpenid(respJson.getStr("openid"));
oAuth2AccessToken.setAccessToken(respJson.getStr("access_token"));
oAuth2AccessToken.setOpenid(respJson.getString("openid"));
oAuth2AccessToken.setAccessToken(respJson.getString("access_token"));
return oAuth2AccessToken;
}

View File

@@ -1,6 +1,6 @@
package com.tiesheng.platform.config.wxmp.bean;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.tiesheng.util.service.http.OkHttpUtil;
/**
@@ -33,7 +33,7 @@ public class WxUserInfo {
String s = OkHttpUtil.get("https://api.weixin.qq.com/sns/userinfo"
+ "?access_token=" + oAuth2AccessToken.getAccessToken()
+ "&openid=" + oAuth2AccessToken.getOpenid() + "&lang=zh_CN");
return JSONUtil.toBean(s, WxUserInfo.class);
return JSON.parseObject(s, WxUserInfo.class);
}