实现申请采购推送信息到审批台出现语音提示和闪烁提示
This commit is contained in:
parent
01455720be
commit
74550c615b
|
@ -197,6 +197,12 @@
|
|||
<artifactId>jaxb-api</artifactId>
|
||||
<version>2.3.1</version>
|
||||
</dependency>
|
||||
|
||||
<!-- WebSocket 支持 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
package com.dite.znpt.config;
|
||||
|
||||
import com.dite.znpt.websocket.SimpleWebSocketHandler;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.socket.config.annotation.EnableWebSocket;
|
||||
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
|
||||
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
|
||||
|
||||
/**
|
||||
* WebSocket配置
|
||||
*/
|
||||
@Configuration
|
||||
@EnableWebSocket
|
||||
public class WebSocketConfig implements WebSocketConfigurer {
|
||||
|
||||
private final SimpleWebSocketHandler simpleWebSocketHandler;
|
||||
|
||||
public WebSocketConfig(SimpleWebSocketHandler simpleWebSocketHandler) {
|
||||
this.simpleWebSocketHandler = simpleWebSocketHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
|
||||
registry.addHandler(simpleWebSocketHandler, "/websocket")
|
||||
.setAllowedOrigins("*"); // 在生产环境中应该限制允许的源
|
||||
}
|
||||
}
|
|
@ -10,6 +10,7 @@ import com.dite.znpt.domain.vo.EquipmentApprovalReq;
|
|||
import com.dite.znpt.domain.vo.EquipmentApprovalResp;
|
||||
import com.dite.znpt.domain.vo.EquipmentProcurementApplyReq;
|
||||
import com.dite.znpt.service.EquipmentApprovalService;
|
||||
import com.dite.znpt.websocket.SimpleWebSocketHandler;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
@ -261,6 +262,21 @@ public class EquipmentApprovalServiceImpl implements EquipmentApprovalService {
|
|||
// 保存到数据库
|
||||
equipmentApprovalMapper.insert(entity);
|
||||
|
||||
// 发送通知 - 使用日志记录,后续可以扩展为WebSocket通知
|
||||
log.info("采购申请提交成功,设备名称: {}, 申请人: {}",
|
||||
req.getEquipmentName(), getCurrentUserName());
|
||||
|
||||
// 发送WebSocket通知
|
||||
try {
|
||||
SimpleWebSocketHandler.sendProcurementNotification(
|
||||
req.getEquipmentName(),
|
||||
getCurrentUserName()
|
||||
);
|
||||
log.info("WebSocket通知发送成功");
|
||||
} catch (Exception e) {
|
||||
log.error("WebSocket通知发送失败", e);
|
||||
}
|
||||
|
||||
log.info("采购申请提交成功,审批ID: {}", entity.getApprovalId());
|
||||
}
|
||||
|
||||
|
@ -317,15 +333,31 @@ public class EquipmentApprovalServiceImpl implements EquipmentApprovalService {
|
|||
* 获取当前用户名
|
||||
*/
|
||||
private String getCurrentUserName() {
|
||||
// TODO: 从安全上下文获取当前用户名
|
||||
return "当前用户";
|
||||
try {
|
||||
// 从Sa-Token上下文获取当前用户名
|
||||
Object loginId = cn.dev33.satoken.stp.StpUtil.getLoginId();
|
||||
if (loginId != null) {
|
||||
return loginId.toString();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("获取当前用户名失败: {}", e.getMessage());
|
||||
}
|
||||
return "未知用户";
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户ID
|
||||
*/
|
||||
private String getCurrentUserId() {
|
||||
// TODO: 从安全上下文获取当前用户ID
|
||||
return "current_user_id";
|
||||
try {
|
||||
// 从Sa-Token上下文获取当前用户ID
|
||||
Object loginId = cn.dev33.satoken.stp.StpUtil.getLoginId();
|
||||
if (loginId != null) {
|
||||
return loginId.toString();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("获取当前用户ID失败: {}", e.getMessage());
|
||||
}
|
||||
return "unknown_user_id";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,96 @@
|
|||
package com.dite.znpt.websocket;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.socket.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 简单的WebSocket处理器
|
||||
*/
|
||||
@Component
|
||||
public class SimpleWebSocketHandler implements WebSocketHandler {
|
||||
|
||||
// 存储所有连接的会话
|
||||
private static final Map<String, WebSocketSession> sessions = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
|
||||
String sessionId = session.getId();
|
||||
sessions.put(sessionId, session);
|
||||
System.out.println("WebSocket连接建立,sessionId: " + sessionId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
|
||||
// 处理接收到的消息
|
||||
System.out.println("收到WebSocket消息: " + message.getPayload());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
|
||||
System.err.println("WebSocket传输错误,sessionId: " + session.getId());
|
||||
exception.printStackTrace();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
|
||||
String sessionId = session.getId();
|
||||
sessions.remove(sessionId);
|
||||
System.out.println("WebSocket连接关闭,sessionId: " + sessionId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsPartialMessages() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送简单消息给所有连接的客户端
|
||||
*/
|
||||
public static void sendMessageToAll(String message) {
|
||||
System.out.println("准备发送WebSocket消息给 " + sessions.size() + " 个连接的客户端");
|
||||
System.out.println("消息内容: " + message);
|
||||
|
||||
final int[] successCount = {0};
|
||||
final int[] failCount = {0};
|
||||
|
||||
sessions.values().forEach(session -> {
|
||||
try {
|
||||
if (session.isOpen()) {
|
||||
session.sendMessage(new TextMessage(message));
|
||||
successCount[0]++;
|
||||
System.out.println("成功发送消息给sessionId: " + session.getId());
|
||||
} else {
|
||||
System.out.println("跳过已关闭的sessionId: " + session.getId());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
failCount[0]++;
|
||||
System.err.println("发送消息失败,sessionId: " + session.getId());
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
|
||||
System.out.println("WebSocket消息发送完成 - 成功: " + successCount[0] + ", 失败: " + failCount[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送采购申请通知
|
||||
*/
|
||||
public static void sendProcurementNotification(String equipmentName, String applicantName) {
|
||||
String notificationMessage = String.format(
|
||||
"{\"type\":\"PROCUREMENT_APPLICATION\",\"title\":\"新的采购申请\",\"content\":\"收到来自 %s 的设备采购申请:%s\"}",
|
||||
applicantName, equipmentName
|
||||
);
|
||||
|
||||
System.out.println("=== 发送采购申请通知 ===");
|
||||
System.out.println("设备名称: " + equipmentName);
|
||||
System.out.println("申请人: " + applicantName);
|
||||
System.out.println("通知消息: " + notificationMessage);
|
||||
System.out.println("当前连接数: " + sessions.size());
|
||||
|
||||
sendMessageToAll(notificationMessage);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue