From 8cb96509a0d29882be906240f7b21f93baf644c5 Mon Sep 17 00:00:00 2001 From: Zheng Jie <201507802@qq.com> Date: Thu, 6 Jul 2023 20:42:05 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E4=BD=BF=E7=94=A8fastjson?= =?UTF-8?q?=E8=BF=9B=E8=A1=8CJson=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/me/zhengjie/utils/SecurityUtils.java | 16 +++-- .../me/zhengjie/utils/TranslatorUtil.java | 66 ------------------- .../service/impl/SysLogServiceImpl.java | 13 ++-- .../mnt/websocket/WebSocketServer.java | 13 ++-- .../service/impl/QiNiuServiceImpl.java | 5 +- 5 files changed, 23 insertions(+), 90 deletions(-) delete mode 100644 eladmin-common/src/main/java/me/zhengjie/utils/TranslatorUtil.java diff --git a/eladmin-common/src/main/java/me/zhengjie/utils/SecurityUtils.java b/eladmin-common/src/main/java/me/zhengjie/utils/SecurityUtils.java index 08f4c042..28e4672c 100644 --- a/eladmin-common/src/main/java/me/zhengjie/utils/SecurityUtils.java +++ b/eladmin-common/src/main/java/me/zhengjie/utils/SecurityUtils.java @@ -15,9 +15,9 @@ */ package me.zhengjie.utils; -import cn.hutool.json.JSONArray; -import cn.hutool.json.JSONObject; -import cn.hutool.json.JSONUtil; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import me.zhengjie.exception.BadRequestException; import me.zhengjie.utils.enums.DataScopeEnum; @@ -68,7 +68,9 @@ public class SecurityUtils { */ public static Long getCurrentUserId() { UserDetails userDetails = getCurrentUser(); - return new JSONObject(new JSONObject(userDetails).get("user")).get("id", Long.class); + // 将 Java 对象转换为 JSONObject 对象 + JSONObject jsonObject = (JSONObject) JSON.toJSON(userDetails); + return jsonObject.getJSONObject("user").getLong("id"); } /** @@ -77,8 +79,10 @@ public class SecurityUtils { */ public static List getCurrentUserDataScope(){ UserDetails userDetails = getCurrentUser(); - JSONArray array = JSONUtil.parseArray(new JSONObject(userDetails).get("dataScopes")); - return JSONUtil.toList(array,Long.class); + // 将 Java 对象转换为 JSONObject 对象 + JSONObject jsonObject = (JSONObject) JSON.toJSON(userDetails); + JSONArray jsonArray = jsonObject.getJSONArray("dataScopes"); + return JSON.parseArray(jsonArray.toJSONString(), Long.class); } /** diff --git a/eladmin-common/src/main/java/me/zhengjie/utils/TranslatorUtil.java b/eladmin-common/src/main/java/me/zhengjie/utils/TranslatorUtil.java deleted file mode 100644 index f2bd5d2d..00000000 --- a/eladmin-common/src/main/java/me/zhengjie/utils/TranslatorUtil.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2019-2020 Zheng Jie - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package me.zhengjie.utils; - -import cn.hutool.json.JSONArray; -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.URL; -import java.net.URLEncoder; - -/** - * @author Zheng Jie - * 翻译工具类 - */ -public class TranslatorUtil { - - public static String translate(String word){ - try { - String url = "https://translate.googleapis.com/translate_a/single?" + - "client=gtx&" + - "sl=en" + - "&tl=zh-CN" + - "&dt=t&q=" + URLEncoder.encode(word, "UTF-8"); - - URL obj = new URL(url); - HttpURLConnection con = (HttpURLConnection) obj.openConnection(); - con.setRequestProperty("User-Agent", "Mozilla/5.0"); - - BufferedReader in = new BufferedReader( - new InputStreamReader(con.getInputStream())); - String inputLine; - StringBuilder response = new StringBuilder(); - - while ((inputLine = in.readLine()) != null) { - response.append(inputLine); - } - in.close(); - return parseResult(response.toString()); - }catch (Exception e){ - return word; - } - } - - private static String parseResult(String inputJson){ - JSONArray jsonArray2 = (JSONArray) new JSONArray(inputJson).get(0); - StringBuilder result = new StringBuilder(); - for (Object o : jsonArray2) { - result.append(((JSONArray) o).get(0).toString()); - } - return result.toString(); - } -} diff --git a/eladmin-logging/src/main/java/me/zhengjie/service/impl/SysLogServiceImpl.java b/eladmin-logging/src/main/java/me/zhengjie/service/impl/SysLogServiceImpl.java index 19089bf8..ae92e82b 100644 --- a/eladmin-logging/src/main/java/me/zhengjie/service/impl/SysLogServiceImpl.java +++ b/eladmin-logging/src/main/java/me/zhengjie/service/impl/SysLogServiceImpl.java @@ -17,8 +17,8 @@ package me.zhengjie.service.impl; import cn.hutool.core.lang.Dict; import cn.hutool.core.util.ObjectUtil; -import cn.hutool.json.JSONObject; -import cn.hutool.json.JSONUtil; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import lombok.RequiredArgsConstructor; import me.zhengjie.domain.SysLog; import me.zhengjie.repository.LogRepository; @@ -36,7 +36,6 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; - import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.lang.reflect.Method; @@ -98,9 +97,9 @@ public class SysLogServiceImpl implements SysLogService { sysLog.setParams(getParameter(method, joinPoint.getArgs())); // 记录登录用户,隐藏密码信息 if(signature.getName().equals("login") && StringUtils.isNotEmpty(sysLog.getParams())){ - JSONObject obj = JSONUtil.parseObj(sysLog.getParams()); - sysLog.setUsername(obj.getStr("username", "")); - sysLog.setParams(JSONUtil.toJsonStr(Dict.create().set("username", sysLog.getUsername()))); + JSONObject obj = JSON.parseObject(sysLog.getParams()); + sysLog.setUsername(obj.getString("username")); + sysLog.setParams(JSON.toJSONString(Dict.create().set("username", sysLog.getUsername()))); } sysLog.setBrowser(browser); logRepository.save(sysLog); @@ -133,7 +132,7 @@ public class SysLogServiceImpl implements SysLogService { if (argList.isEmpty()) { return ""; } - return argList.size() == 1 ? JSONUtil.toJsonStr(argList.get(0)) : JSONUtil.toJsonStr(argList); + return argList.size() == 1 ? JSON.toJSONString(argList.get(0)) : JSON.toJSONString(argList); } @Override diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/mnt/websocket/WebSocketServer.java b/eladmin-system/src/main/java/me/zhengjie/modules/mnt/websocket/WebSocketServer.java index 9c92165b..000a5158 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/mnt/websocket/WebSocketServer.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/mnt/websocket/WebSocketServer.java @@ -15,10 +15,9 @@ */ package me.zhengjie.modules.mnt.websocket; -import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.JSON; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; - import javax.websocket.*; import javax.websocket.server.PathParam; import javax.websocket.server.ServerEndpoint; @@ -37,7 +36,7 @@ public class WebSocketServer { /** * concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。 */ - private static CopyOnWriteArraySet webSocketSet = new CopyOnWriteArraySet(); + private static final CopyOnWriteArraySet webSocketSet = new CopyOnWriteArraySet(); /** * 与某个客户端的连接会话,需要通过它来给客户端发送数据 @@ -55,11 +54,7 @@ public class WebSocketServer { public void onOpen(Session session,@PathParam("sid") String sid) { this.session = session; //如果存在就先删除一个,防止重复推送消息 - for (WebSocketServer webSocket:webSocketSet) { - if (webSocket.sid.equals(sid)) { - webSocketSet.remove(webSocket); - } - } + webSocketSet.removeIf(webSocket -> webSocket.sid.equals(sid)); webSocketSet.add(this); this.sid=sid; } @@ -105,7 +100,7 @@ public class WebSocketServer { * 群发自定义消息 * */ public static void sendInfo(SocketMsg socketMsg,@PathParam("sid") String sid) throws IOException { - String message = JSONObject.toJSONString(socketMsg); + String message = JSON.toJSONString(socketMsg); log.info("推送消息到"+sid+",推送内容:"+message); for (WebSocketServer item : webSocketSet) { try { diff --git a/eladmin-tools/src/main/java/me/zhengjie/service/impl/QiNiuServiceImpl.java b/eladmin-tools/src/main/java/me/zhengjie/service/impl/QiNiuServiceImpl.java index 485b54b7..60093d7b 100644 --- a/eladmin-tools/src/main/java/me/zhengjie/service/impl/QiNiuServiceImpl.java +++ b/eladmin-tools/src/main/java/me/zhengjie/service/impl/QiNiuServiceImpl.java @@ -119,7 +119,7 @@ public class QiNiuServiceImpl implements QiNiuService { qiniuContent.setType(qiniuConfig.getType()); qiniuContent.setKey(FileUtil.getFileNameNoEx(putRet.key)); qiniuContent.setUrl(qiniuConfig.getHost()+"/"+putRet.key); - qiniuContent.setSize(FileUtil.getSize(Integer.parseInt(file.getSize()+""))); + qiniuContent.setSize(FileUtil.getSize(Integer.parseInt(String.valueOf(file.getSize())))); return qiniuContentRepository.save(qiniuContent); } return content; @@ -190,7 +190,7 @@ public class QiNiuServiceImpl implements QiNiuService { for (FileInfo item : items) { if(qiniuContentRepository.findByKey(FileUtil.getFileNameNoEx(item.key)) == null){ qiniuContent = new QiniuContent(); - qiniuContent.setSize(FileUtil.getSize(Integer.parseInt(item.fsize+""))); + qiniuContent.setSize(FileUtil.getSize(Integer.parseInt(String.valueOf(item.fsize)))); qiniuContent.setSuffix(FileUtil.getExtensionName(item.key)); qiniuContent.setKey(FileUtil.getFileNameNoEx(item.key)); qiniuContent.setType(config.getType()); @@ -203,6 +203,7 @@ public class QiNiuServiceImpl implements QiNiuService { } @Override + @Transactional(rollbackFor = Exception.class) public void deleteAll(Long[] ids, QiniuConfig config) { for (Long id : ids) { delete(findByContentId(id), config);