From e6a5f4ad48f1f8770c86098e1c293c5c5f36d44a Mon Sep 17 00:00:00 2001 From: tangw Date: Fri, 13 Mar 2026 17:13:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=93=E5=8D=B0=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../websocket/ChatWebSocketServer.java | 33 +++++++++++++++-- .../websocket/WebSocketMsgServiceImpl.java | 7 +++- .../websocket/WebSocketUser.java | 10 +++++- .../controller/RegRequestInfoController.java | 10 ++++++ .../service/RegRequestInfoService.java | 2 ++ .../impl/RegRequestInfoServiceImpl.java | 35 ++++++++++++++++++- 6 files changed, 92 insertions(+), 5 deletions(-) diff --git a/pf_interfaceCommon/src/main/java/com/transitPlatForm/interfaceCommon/websocket/ChatWebSocketServer.java b/pf_interfaceCommon/src/main/java/com/transitPlatForm/interfaceCommon/websocket/ChatWebSocketServer.java index 3b11853..e1da65c 100644 --- a/pf_interfaceCommon/src/main/java/com/transitPlatForm/interfaceCommon/websocket/ChatWebSocketServer.java +++ b/pf_interfaceCommon/src/main/java/com/transitPlatForm/interfaceCommon/websocket/ChatWebSocketServer.java @@ -1,11 +1,14 @@ package com.transitPlatForm.interfaceCommon.websocket; + import com.alibaba.fastjson2.JSON; import com.transitPlatForm.common.core.domain.model.LoginUser; import com.transitPlatForm.common.utils.SecurityUtils; import com.transitPlatForm.common.utils.spring.SpringUtils; import com.transitPlatForm.interfaceCommon.config.SpringContextWebSocketConfigurator; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import org.springframework.web.socket.server.standard.SpringConfigurator; import javax.websocket.*; import javax.websocket.server.PathParam; @@ -28,7 +31,7 @@ import java.util.concurrent.ConcurrentHashMap; @ServerEndpoint(value = "/ws/msgserver/{userId}", configurator = SpringContextWebSocketConfigurator.class) public class ChatWebSocketServer { // 🌟 核心固定参数白名单:这些参数不存入动态属性(单独解析为固定身份属性) - private static final Set CORE_PARAMS = new HashSet<>(Arrays.asList("token", "username", "role")); + private static final Set CORE_PARAMS = new HashSet<>(Arrays.asList("token", "username", "role")); // 🌟 容器改为存储封装后的WebSocketUser(key=userId) private static final Map ONLINE_USER_MAP = new ConcurrentHashMap<>(); private static final Map IP_TO_USERS_MAP = new ConcurrentHashMap<>(); @@ -141,7 +144,7 @@ public class ChatWebSocketServer { wsMessage.setMsgId(userId + "_" + System.currentTimeMillis()); } // 4. 获取Spring管理的业务层Bean,转发消息处理 - WebSocketMsgService msgService = SpringUtils.getBean(WebSocketMsgService.class); + WebSocketMsgService msgService = SpringUtils.getBean(WebSocketMsgService.class); if (msgService == null) { throw new RuntimeException("获取WebSocketMsgService Bean失败,请检查Spring配置"); } @@ -253,6 +256,32 @@ public class ChatWebSocketServer { } return roleUsers; } + /** + * 根据WebId筛选在线用户(如仅获取管理员) + */ + public static List getOnlineUsersByWebId(String webId) { + List roleUsers = new ArrayList<>(); + if (webId == null || webId.trim().isEmpty()) { + return roleUsers; + } + for (WebSocketUser user : ONLINE_USER_MAP.values()) { + if (webId.equals(user.getWebId()) && user.isConnected()) { + roleUsers.add(user); + } + } + return roleUsers; + } + public static String getUsersByWebId(String webId) { + if (webId == null || webId.trim().isEmpty()) { + return null; + } + for (WebSocketUser user : ONLINE_USER_MAP.values()) { + if (webId.equals(user.getWebId())) { + return user.getUserId(); + } + } + return null; + } /** * 新增:通过IP快速查询对应的userId(核心效率方法) */ diff --git a/pf_interfaceCommon/src/main/java/com/transitPlatForm/interfaceCommon/websocket/WebSocketMsgServiceImpl.java b/pf_interfaceCommon/src/main/java/com/transitPlatForm/interfaceCommon/websocket/WebSocketMsgServiceImpl.java index 2137da1..8a256c0 100644 --- a/pf_interfaceCommon/src/main/java/com/transitPlatForm/interfaceCommon/websocket/WebSocketMsgServiceImpl.java +++ b/pf_interfaceCommon/src/main/java/com/transitPlatForm/interfaceCommon/websocket/WebSocketMsgServiceImpl.java @@ -6,6 +6,8 @@ import com.transitPlatForm.common.core.redis.RedisCache; import com.transitPlatForm.common.utils.spring.SpringUtils; import org.springframework.stereotype.Service; import javax.websocket.Session; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; @@ -59,7 +61,7 @@ public class WebSocketMsgServiceImpl implements WebSocketMsgService { } } catch (Exception e) { e.printStackTrace(); - // ChatWebSocketServer.sendToUser(wsMessage.getSenderId(), "错误:消息处理失败:" + e.getMessage()); + // ChatWebSocketServer.sendToUser(wsMessage.getSenderId(), "错误:消息处理失败:" + e.getMessage()); String senderId = wsMessage.getSenderId(); if (senderId != null && session != null && session.isOpen()) { ChatWebSocketServer.sendToUser(senderId, "错误:消息处理失败,已加入重试队列"); @@ -229,6 +231,9 @@ public class WebSocketMsgServiceImpl implements WebSocketMsgService { case "username": user.setUsername(value); break; + case "webid": + user.setWebId(value); + break; case "role": user.setRole(value); break; diff --git a/pf_interfaceCommon/src/main/java/com/transitPlatForm/interfaceCommon/websocket/WebSocketUser.java b/pf_interfaceCommon/src/main/java/com/transitPlatForm/interfaceCommon/websocket/WebSocketUser.java index cb52ade..e5a3dbd 100644 --- a/pf_interfaceCommon/src/main/java/com/transitPlatForm/interfaceCommon/websocket/WebSocketUser.java +++ b/pf_interfaceCommon/src/main/java/com/transitPlatForm/interfaceCommon/websocket/WebSocketUser.java @@ -1,5 +1,4 @@ package com.transitPlatForm.interfaceCommon.websocket; - import javax.websocket.Session; import java.time.LocalDateTime; import java.util.Map; @@ -12,6 +11,7 @@ public class WebSocketUser { // 核心字段:用户唯一标识(仍保持final,不建议修改) private final String userId; // 身份属性(移除final修饰符,支持修改) + private String webId; private String username; // 用户名 private String role; // 角色(如ADMIN/USER) private String loginIp; // 登录IP @@ -46,7 +46,15 @@ public class WebSocketUser { public void setUsername(String username) { this.username = username; } + // ========== 可修改属性(getter + setter) ========== + public String getWebId() { + return webId; + } + // 添加username的setter方法 + public void setWebId(String webId) { + this.webId = webId; + } public String getRole() { return role; } diff --git a/pf_transitPF/src/main/java/com/transitPlatForm/transitPF/controller/RegRequestInfoController.java b/pf_transitPF/src/main/java/com/transitPlatForm/transitPF/controller/RegRequestInfoController.java index 98e3950..f149ab5 100644 --- a/pf_transitPF/src/main/java/com/transitPlatForm/transitPF/controller/RegRequestInfoController.java +++ b/pf_transitPF/src/main/java/com/transitPlatForm/transitPF/controller/RegRequestInfoController.java @@ -70,6 +70,16 @@ public class RegRequestInfoController extends BaseController { public Result cancelReq(String applyId){ return regRequestInfoService.cancelReq(applyId); } + @ApiOperation("打印机插件设置") + @GetMapping("/printsetup") + public Result printsetup() { + return regRequestInfoService.printsetup(); + } + @ApiOperation("打印机插件检查") + @GetMapping("/printcheck") + public Result printcheck() { + return regRequestInfoService.printcheck(); + } } diff --git a/pf_transitPF/src/main/java/com/transitPlatForm/transitPF/service/RegRequestInfoService.java b/pf_transitPF/src/main/java/com/transitPlatForm/transitPF/service/RegRequestInfoService.java index 3163349..0955844 100644 --- a/pf_transitPF/src/main/java/com/transitPlatForm/transitPF/service/RegRequestInfoService.java +++ b/pf_transitPF/src/main/java/com/transitPlatForm/transitPF/service/RegRequestInfoService.java @@ -16,4 +16,6 @@ public interface RegRequestInfoService { Result printBarCode(String applyId,String userName); Result cancelReq(String applyId); + Result printcheck(); + Result printsetup(); } diff --git a/pf_transitPF/src/main/java/com/transitPlatForm/transitPF/service/impl/RegRequestInfoServiceImpl.java b/pf_transitPF/src/main/java/com/transitPlatForm/transitPF/service/impl/RegRequestInfoServiceImpl.java index bd83341..56b6f19 100644 --- a/pf_transitPF/src/main/java/com/transitPlatForm/transitPF/service/impl/RegRequestInfoServiceImpl.java +++ b/pf_transitPF/src/main/java/com/transitPlatForm/transitPF/service/impl/RegRequestInfoServiceImpl.java @@ -5,6 +5,7 @@ import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSONUtil; import com.transitPlatForm.common.core.domain.Result; import com.transitPlatForm.common.core.domain.entity.LabIntersendList; +import com.transitPlatForm.common.core.domain.entity.SysLoginParam; import com.transitPlatForm.common.core.domain.entity.reg.*; import com.transitPlatForm.common.utils.SecurityUtils; import com.transitPlatForm.common.utils.ip.IpUtils; @@ -489,6 +490,10 @@ public class RegRequestInfoServiceImpl implements RegRequestInfoService { String ip= IpUtils.getIpAddr(); System.out.println("请求业务的IP:"+ip); List printUsers = ChatWebSocketServer.getOnlineUsersByLoginIp(ip); + if (printUsers.isEmpty()) { + String webid=getLocalid(); + printUsers= ChatWebSocketServer.getOnlineUsersByWebId(webid); + } if (printUsers.isEmpty()) { System.out.println("当前客户端打印插件不在线"); return new Result("-1","当前客户端打印插件不在线"); @@ -498,5 +503,33 @@ public class RegRequestInfoServiceImpl implements RegRequestInfoService { System.out.println("已给客户端[" + webSocketUser.getUsername() + "]推送打印通知,IP:" + webSocketUser.getLoginIp()); return new Result("0","打印成功"); } - + @Override + public Result printcheck(){ + String ip= IpUtils.getIpAddr(); + String UserId = ChatWebSocketServer.getUserIdByIp(ip); + if(UserId==null||"".equals(UserId)){ + String webid=getLocalid(); + UserId=ChatWebSocketServer.getUsersByWebId(webid); + if(UserId!=null&&!"".equals(UserId)){ + return new Result("0","客户端插件正常"); + } + return new Result("-1","当前客户端打印插件不在线或未安装"); + } + return new Result("0","客户端插件正常"); + } + @Override + public Result printsetup(){ + WebSocketMessage wsMessage=new WebSocketMessage(); + wsMessage.setMsgType("lisbarprint"); + wsMessage.setData("lisbarprint://setup"); + wsMessage.setSenderId(SecurityUtils.getUsername()); + return sendprintmsg(JSONUtil.toJsonStr(wsMessage)); + } + private String getLocalid() { + SysLoginParam user= SecurityUtils.getLoginUser().getSysLoginParam(); + if(user!=null) { + return user.getLocalid(); + } + return null; + } }