Compare commits
No commits in common. '73c643ab5b1cc3faaf229fe0e2ac6361c75192df' and '05fed99163a67c4440a6456dfaeb84fd956b7d4d' have entirely different histories.
73c643ab5b
...
05fed99163
@ -1,68 +0,0 @@ |
|||||||
package com.alops.web.controller.message; |
|
||||||
|
|
||||||
import com.alops.common.annotation.Log; |
|
||||||
import com.alops.common.core.domain.AjaxResult; |
|
||||||
import com.alops.common.enums.BusinessType; |
|
||||||
import com.alops.common.utils.SecurityUtils; |
|
||||||
import com.alops.system.domain.InternalMessage; |
|
||||||
import com.alops.system.domain.dto.AlertRuleAddDto; |
|
||||||
import com.alops.system.domain.vo.AlertRuleVo; |
|
||||||
import com.alops.system.service.IInternalMessageService; |
|
||||||
import io.swagger.annotations.ApiOperation; |
|
||||||
import org.springframework.beans.factory.annotation.Autowired; |
|
||||||
import org.springframework.security.access.prepost.PreAuthorize; |
|
||||||
import org.springframework.web.bind.annotation.*; |
|
||||||
|
|
||||||
import java.util.List; |
|
||||||
|
|
||||||
@RestController |
|
||||||
@RequestMapping("/message") |
|
||||||
public class messageController { |
|
||||||
|
|
||||||
@Autowired |
|
||||||
public IInternalMessageService iInternalMessageService; |
|
||||||
|
|
||||||
//变已读
|
|
||||||
@PreAuthorize("@ss.hasPermi('message:change')") |
|
||||||
@ApiOperation("变已读") |
|
||||||
@Log(title = "【变已读】") |
|
||||||
@PostMapping("/change/{ids}") |
|
||||||
public AjaxResult change(@PathVariable Long[] ids) { |
|
||||||
return AjaxResult.success("修改成功", iInternalMessageService.markMessagesAsRead(ids)); |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
//根据用户id获取未读量
|
|
||||||
@PreAuthorize("@ss.hasPermi('message:read')") |
|
||||||
@ApiOperation("未读量") |
|
||||||
@Log(title = "【未读量】") |
|
||||||
@PostMapping("/read") |
|
||||||
public AjaxResult read() { |
|
||||||
Long userId = SecurityUtils.getLoginUser().getUserId(); |
|
||||||
|
|
||||||
// 调用Service
|
|
||||||
int count = iInternalMessageService.getUnreadCountByUserId(userId); |
|
||||||
|
|
||||||
return AjaxResult.success("查询成功", count); |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
@PreAuthorize("@ss.hasPermi('message:view')") |
|
||||||
@ApiOperation("获取用户消息列表") |
|
||||||
@Log(title = "【消息列表】") |
|
||||||
@GetMapping("/list") |
|
||||||
public AjaxResult getMessageList() { |
|
||||||
// 获取当前登录用户ID
|
|
||||||
Long userId = SecurityUtils.getLoginUser().getUserId(); |
|
||||||
|
|
||||||
// 调用Service获取消息列表
|
|
||||||
List<InternalMessage> messages = iInternalMessageService.getMessagesByUserId(userId); |
|
||||||
|
|
||||||
return AjaxResult.success(messages); |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} |
|
||||||
@ -1,20 +0,0 @@ |
|||||||
package com.alops.system.config; |
|
||||||
|
|
||||||
import com.alops.system.hander.WebSocketServer; |
|
||||||
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的controller
|
|
||||||
@Configuration |
|
||||||
@EnableWebSocket |
|
||||||
public class WebSocketConfig implements WebSocketConfigurer { |
|
||||||
@Override |
|
||||||
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { |
|
||||||
|
|
||||||
registry.addHandler(new WebSocketServer(), "/ws/onlineMessage") |
|
||||||
.setAllowedOrigins("*"); |
|
||||||
|
|
||||||
} |
|
||||||
} |
|
||||||
@ -1,153 +0,0 @@ |
|||||||
package com.alops.system.hander; |
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper; |
|
||||||
import org.slf4j.Logger; |
|
||||||
import org.slf4j.LoggerFactory; |
|
||||||
import org.springframework.beans.factory.annotation.Autowired; |
|
||||||
import org.springframework.stereotype.Component; |
|
||||||
import org.springframework.util.MultiValueMap; |
|
||||||
import org.springframework.web.socket.CloseStatus; |
|
||||||
import org.springframework.web.socket.TextMessage; |
|
||||||
import org.springframework.web.socket.WebSocketSession; |
|
||||||
import org.springframework.web.socket.handler.TextWebSocketHandler; |
|
||||||
import org.springframework.web.util.UriComponentsBuilder; |
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper; |
|
||||||
|
|
||||||
import java.util.Date; |
|
||||||
import java.util.HashMap; |
|
||||||
import java.util.Map; |
|
||||||
import java.util.Set; |
|
||||||
import java.util.concurrent.ConcurrentHashMap; |
|
||||||
|
|
||||||
@Component |
|
||||||
public class WebSocketServer extends TextWebSocketHandler { |
|
||||||
private static final Logger logger = LoggerFactory.getLogger(WebSocketServer.class); |
|
||||||
// 存储在线用户 (Key: 用户ID, Value: WebSocketSession)
|
|
||||||
public static final ConcurrentHashMap<Long, WebSocketSession> onlineUsers = new ConcurrentHashMap<>(); |
|
||||||
|
|
||||||
|
|
||||||
private static ObjectMapper objectMapper; |
|
||||||
|
|
||||||
|
|
||||||
@Autowired |
|
||||||
public void setObjectMapper(ObjectMapper mapper) { |
|
||||||
WebSocketServer.objectMapper = mapper; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public void afterConnectionEstablished(WebSocketSession session) throws Exception { |
|
||||||
Long userId = getUserIdFromSession(session); |
|
||||||
if (userId != null) { |
|
||||||
logger.info("WebSocket 连接成功,用户上线: " + userId); |
|
||||||
onlineUsers.put(userId, session); |
|
||||||
} else { |
|
||||||
logger.warn("无效的用户连接,关闭连接: " + session.getId()); |
|
||||||
session.close(); // 没有 userId 的连接直接关闭
|
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { |
|
||||||
Long userId = getUserIdFromSession(session); |
|
||||||
if (userId != null) { |
|
||||||
|
|
||||||
logger.info("WebSocket 连接关闭,用户下线: " + userId + ", 关闭状态: " + status); |
|
||||||
onlineUsers.remove(userId); |
|
||||||
|
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
// 获取 WebSocketSession 中的 userId,并转换为 Long 类型
|
|
||||||
public static Long getUserIdFromSession(WebSocketSession session) { |
|
||||||
String query = session.getUri().getQuery(); |
|
||||||
logger.info("WebSocket连接查询参数: {}", query); |
|
||||||
|
|
||||||
if (query != null) { |
|
||||||
MultiValueMap<String, String> params = UriComponentsBuilder.newInstance() |
|
||||||
.query(query) |
|
||||||
.build() |
|
||||||
.getQueryParams(); |
|
||||||
String userIdStr = params.getFirst("userId"); |
|
||||||
if (userIdStr != null) { |
|
||||||
try { |
|
||||||
return Long.parseLong(userIdStr); |
|
||||||
} catch (NumberFormatException e) { |
|
||||||
logger.error("WebSocket连接无效的用户ID: {}", userIdStr, e); |
|
||||||
} |
|
||||||
} else { |
|
||||||
logger.warn("WebSocket连接没有 userId 参数"); |
|
||||||
} |
|
||||||
} else { |
|
||||||
logger.warn("WebSocket连接没有查询参数"); |
|
||||||
} |
|
||||||
return null; |
|
||||||
} |
|
||||||
//
|
|
||||||
// // 发送消息给指定用户
|
|
||||||
// public void sendMessageToUser(Long userId, String content, String title) throws Exception {
|
|
||||||
// // 获取对应用户的 WebSocket session
|
|
||||||
// WebSocketSession session = onlineUsers.get(userId);
|
|
||||||
//
|
|
||||||
// // 检查 session 是否有效且连接未关闭
|
|
||||||
// if (session != null && session.isOpen()) {
|
|
||||||
// // 创建一个 Map 来构建消息内容
|
|
||||||
// Map<String, Object> messageMap = new HashMap<>();
|
|
||||||
// messageMap.put("from UserName", fromUserName); // 发送者姓名
|
|
||||||
// messageMap.put("content", content); // 消息内容
|
|
||||||
//
|
|
||||||
// // 使用 Jackson ObjectMapper 将 Map 转换为 JSON 字符串
|
|
||||||
// ObjectMapper objectMapper = new ObjectMapper();
|
|
||||||
// String jsonMessage = objectMapper.writeValueAsString(messageMap);
|
|
||||||
//
|
|
||||||
// // 发送 JSON 格式的消息
|
|
||||||
// session.sendMessage(new TextMessage(jsonMessage));
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
/** |
|
||||||
* 发送通知到指定用户 |
|
||||||
* @param messageId 数据库中站内信的ID |
|
||||||
* @param title 通知标题,例如 "设备异常预警" |
|
||||||
* @param content 通知内容 |
|
||||||
*/ |
|
||||||
public static void sendAlertMessage(Long userId, Long messageId, String title, String content) { |
|
||||||
WebSocketSession session = onlineUsers.get(userId); |
|
||||||
try { |
|
||||||
if (session != null && session.isOpen()) { |
|
||||||
|
|
||||||
// 构建消息内容
|
|
||||||
Map<String, Object> messageMap = new HashMap<>(); |
|
||||||
messageMap.put("messageId", messageId); // 对应数据库ID
|
|
||||||
messageMap.put("title", title); // 弹窗标题
|
|
||||||
messageMap.put("content", content); // 弹窗内容
|
|
||||||
messageMap.put("timestamp", new Date()); // 发送时间
|
|
||||||
|
|
||||||
// 转换为 JSON
|
|
||||||
String jsonMessage = objectMapper.writeValueAsString(messageMap); |
|
||||||
|
|
||||||
// 发送
|
|
||||||
session.sendMessage(new TextMessage(jsonMessage)); |
|
||||||
logger.info("成功向用户 [{}] 发送预警消息: {}", session.getId(), jsonMessage); |
|
||||||
} |
|
||||||
} catch (Exception e) { |
|
||||||
logger.error("发送预警消息失败: messageId=" + messageId, e); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
public void sendMessageToUser1(Long userId, String message) throws Exception { |
|
||||||
WebSocketSession session = onlineUsers.get(userId); |
|
||||||
if (session != null && session.isOpen()) { |
|
||||||
session.sendMessage(new TextMessage(message)); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
// 获取所有在线用户 ID (Long 类型)
|
|
||||||
public Set<Long> getOnlineUserIds() { |
|
||||||
return onlineUsers.keySet(); |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} |
|
||||||
@ -1,74 +0,0 @@ |
|||||||
package com.alops.system.service; |
|
||||||
|
|
||||||
import java.util.List; |
|
||||||
|
|
||||||
import com.alops.system.domain.InternalMessage; |
|
||||||
|
|
||||||
|
|
||||||
/** |
|
||||||
* 站内信Service接口 |
|
||||||
* |
|
||||||
* @author ruoyi |
|
||||||
* @date 2025-10-23 |
|
||||||
*/ |
|
||||||
public interface IInternalMessageService |
|
||||||
{ |
|
||||||
|
|
||||||
|
|
||||||
/** |
|
||||||
* 根据用户ID获取消息列表,按时间排序 |
|
||||||
*/ |
|
||||||
List<InternalMessage> getMessagesByUserId(Long userId); |
|
||||||
/** |
|
||||||
* 根据用户ID获取未读消息数量 |
|
||||||
*/ |
|
||||||
int getUnreadCountByUserId(Long userId); |
|
||||||
public int markMessagesAsRead(Long[] ids); |
|
||||||
/** |
|
||||||
* 查询站内信 |
|
||||||
* |
|
||||||
* @param id 站内信主键 |
|
||||||
* @return 站内信 |
|
||||||
*/ |
|
||||||
public InternalMessage selectInternalMessageById(Long id); |
|
||||||
|
|
||||||
/** |
|
||||||
* 查询站内信列表 |
|
||||||
* |
|
||||||
* @param internalMessage 站内信 |
|
||||||
* @return 站内信集合 |
|
||||||
*/ |
|
||||||
public List<InternalMessage> selectInternalMessageList(InternalMessage internalMessage); |
|
||||||
|
|
||||||
/** |
|
||||||
* 新增站内信 |
|
||||||
* |
|
||||||
* @param internalMessage 站内信 |
|
||||||
* @return 结果 |
|
||||||
*/ |
|
||||||
public int insertInternalMessage(InternalMessage internalMessage); |
|
||||||
|
|
||||||
/** |
|
||||||
* 修改站内信 |
|
||||||
* |
|
||||||
* @param internalMessage 站内信 |
|
||||||
* @return 结果 |
|
||||||
*/ |
|
||||||
public int updateInternalMessage(InternalMessage internalMessage); |
|
||||||
|
|
||||||
/** |
|
||||||
* 批量删除站内信 |
|
||||||
* |
|
||||||
* @param ids 需要删除的站内信主键集合 |
|
||||||
* @return 结果 |
|
||||||
*/ |
|
||||||
public int deleteInternalMessageByIds(Long[] ids); |
|
||||||
|
|
||||||
/** |
|
||||||
* 删除站内信信息 |
|
||||||
* |
|
||||||
* @param id 站内信主键 |
|
||||||
* @return 结果 |
|
||||||
*/ |
|
||||||
public int deleteInternalMessageById(Long id); |
|
||||||
} |
|
||||||
@ -1,123 +0,0 @@ |
|||||||
package com.alops.system.service.impl; |
|
||||||
|
|
||||||
import java.util.Collections; |
|
||||||
import java.util.List; |
|
||||||
|
|
||||||
import com.alops.common.utils.DateUtils; |
|
||||||
import com.alops.system.domain.InternalMessage; |
|
||||||
import com.alops.system.mapper.InternalMessageMapper; |
|
||||||
import com.alops.system.service.IInternalMessageService; |
|
||||||
import org.springframework.beans.factory.annotation.Autowired; |
|
||||||
import org.springframework.stereotype.Service; |
|
||||||
import org.springframework.transaction.annotation.Transactional; |
|
||||||
|
|
||||||
|
|
||||||
/** |
|
||||||
* 站内信Service业务层处理 |
|
||||||
* |
|
||||||
* @author ruoyi |
|
||||||
* @date 2025-10-23 |
|
||||||
*/ |
|
||||||
@Service |
|
||||||
public class InternalMessageServiceImpl implements IInternalMessageService |
|
||||||
{ |
|
||||||
@Autowired |
|
||||||
private InternalMessageMapper internalMessageMapper; |
|
||||||
|
|
||||||
@Override |
|
||||||
public List<InternalMessage> getMessagesByUserId(Long userId) { |
|
||||||
if (userId == null) { |
|
||||||
return Collections.emptyList(); |
|
||||||
} |
|
||||||
return internalMessageMapper.selectMessagesByUserId(userId); |
|
||||||
} |
|
||||||
@Override |
|
||||||
public int getUnreadCountByUserId(Long userId) { |
|
||||||
if (userId == null) { |
|
||||||
return 0; |
|
||||||
} |
|
||||||
return internalMessageMapper.countUnreadByUserId(userId); |
|
||||||
} |
|
||||||
/** |
|
||||||
* 查询站内信 |
|
||||||
* |
|
||||||
* @param id 站内信主键 |
|
||||||
* @return 站内信 |
|
||||||
*/ |
|
||||||
@Override |
|
||||||
public InternalMessage selectInternalMessageById(Long id) |
|
||||||
{ |
|
||||||
return internalMessageMapper.selectInternalMessageById(id); |
|
||||||
} |
|
||||||
|
|
||||||
@Transactional(rollbackFor = Exception.class) |
|
||||||
public int markMessagesAsRead(Long[] ids) { |
|
||||||
if (ids == null || ids.length == 0) { |
|
||||||
return 0; |
|
||||||
} |
|
||||||
int updatedRows = internalMessageMapper.updateMessageStatusToRead(ids); |
|
||||||
return updatedRows; |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
/** |
|
||||||
* 查询站内信列表 |
|
||||||
* |
|
||||||
* @param internalMessage 站内信 |
|
||||||
* @return 站内信 |
|
||||||
*/ |
|
||||||
@Override |
|
||||||
public List<InternalMessage> selectInternalMessageList(InternalMessage internalMessage) |
|
||||||
{ |
|
||||||
return internalMessageMapper.selectInternalMessageList(internalMessage); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 新增站内信 |
|
||||||
* |
|
||||||
* @param internalMessage 站内信 |
|
||||||
* @return 结果 |
|
||||||
*/ |
|
||||||
@Override |
|
||||||
public int insertInternalMessage(InternalMessage internalMessage) |
|
||||||
{ |
|
||||||
internalMessage.setCreateTime(DateUtils.getNowDate()); |
|
||||||
return internalMessageMapper.insertInternalMessage(internalMessage); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 修改站内信 |
|
||||||
* |
|
||||||
* @param internalMessage 站内信 |
|
||||||
* @return 结果 |
|
||||||
*/ |
|
||||||
@Override |
|
||||||
public int updateInternalMessage(InternalMessage internalMessage) |
|
||||||
{ |
|
||||||
return internalMessageMapper.updateInternalMessage(internalMessage); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 批量删除站内信 |
|
||||||
* |
|
||||||
* @param ids 需要删除的站内信主键 |
|
||||||
* @return 结果 |
|
||||||
*/ |
|
||||||
@Override |
|
||||||
public int deleteInternalMessageByIds(Long[] ids) |
|
||||||
{ |
|
||||||
return internalMessageMapper.deleteInternalMessageByIds(ids); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 删除站内信信息 |
|
||||||
* |
|
||||||
* @param id 站内信主键 |
|
||||||
* @return 结果 |
|
||||||
*/ |
|
||||||
@Override |
|
||||||
public int deleteInternalMessageById(Long id) |
|
||||||
{ |
|
||||||
return internalMessageMapper.deleteInternalMessageById(id); |
|
||||||
} |
|
||||||
} |
|
||||||
Loading…
Reference in new issue