打印插件

This commit is contained in:
tangw 2026-03-13 17:13:09 +08:00
parent d055bae2e8
commit e6a5f4ad48
6 changed files with 92 additions and 5 deletions

View File

@ -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<String> CORE_PARAMS = new HashSet<>(Arrays.asList("token", "username", "role"));
private static final Set<String> CORE_PARAMS = new HashSet<>(Arrays.asList("token", "username", "role"));
// 🌟 容器改为存储封装后的WebSocketUser(key=userId)
private static final Map<String, WebSocketUser> ONLINE_USER_MAP = new ConcurrentHashMap<>();
private static final Map<String, String> 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<WebSocketUser> getOnlineUsersByWebId(String webId) {
List<WebSocketUser> 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(核心效率方法)
*/

View File

@ -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;

View File

@ -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;
}

View File

@ -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();
}
}

View File

@ -16,4 +16,6 @@ public interface RegRequestInfoService {
Result printBarCode(String applyId,String userName);
Result cancelReq(String applyId);
Result printcheck();
Result printsetup();
}

View File

@ -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<WebSocketUser> 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;
}
}