package com.ruoyi.web.controller.webSocket;
|
|
import com.ruoyi.common.core.controller.BaseController;
|
import com.ruoyi.common.core.domain.R;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.web.bind.annotation.*;
|
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Set;
|
|
/**
|
* WebSocket控制器
|
* 提供REST接口用于触发服务端主动发送消息
|
* 支持基于AppUser的消息发送
|
*/
|
@RestController
|
@RequestMapping("/open/websocket")
|
@Slf4j
|
public class WebSocketController extends BaseController {
|
|
|
@Autowired
|
private WebSocketUserService webSocketUserService;
|
|
// ==================== 基于AppUser的WebSocket接口 ====================
|
|
/**
|
* 向指定用户发送消息
|
* @param appUserId 用户ID
|
* @param request 请求参数
|
* @return 操作结果
|
*/
|
@PostMapping("/user/send/{appUserId}")
|
public R<String> sendMessageToUser(@PathVariable String appUserId, @RequestBody Map<String, String> request) {
|
try {
|
String message = request.get("message");
|
if (message == null || message.trim().isEmpty()) {
|
return R.fail("消息内容不能为空");
|
}
|
|
int deviceCount = webSocketUserService.sendMessageToUser(appUserId, message);
|
if (deviceCount > 0) {
|
return R.ok("消息发送成功,发送到 " + deviceCount + " 个设备");
|
} else {
|
return R.fail("用户不在线或发送失败");
|
}
|
} catch (Exception e) {
|
log.error("向用户发送消息失败", e);
|
return R.fail("发送消息失败: " + e.getMessage());
|
}
|
}
|
|
/**
|
* 向指定用户发送JSON格式消息
|
* @param appUserId 用户ID
|
* @param request 请求参数
|
* @return 操作结果
|
*/
|
@PostMapping("/user/send-json/{appUserId}")
|
public R<String> sendJsonMessageToUser(@PathVariable String appUserId, @RequestBody Map<String, Object> request) {
|
try {
|
String messageType = (String) request.get("messageType");
|
Object data = request.get("data");
|
|
if (messageType == null || messageType.trim().isEmpty()) {
|
return R.fail("消息类型不能为空");
|
}
|
|
int deviceCount = webSocketUserService.sendJsonMessageToUser(appUserId, messageType, data);
|
if (deviceCount > 0) {
|
return R.ok("JSON消息发送成功,发送到 " + deviceCount + " 个设备");
|
} else {
|
return R.fail("用户不在线或发送失败");
|
}
|
} catch (Exception e) {
|
log.error("向用户发送JSON消息失败", e);
|
return R.fail("发送消息失败: " + e.getMessage());
|
}
|
}
|
|
/**
|
* 向多个用户发送消息
|
* @param request 请求参数
|
* @return 操作结果
|
*/
|
@PostMapping("/user/send-multiple")
|
public R<String> sendMessageToUsers(@RequestBody Map<String, Object> request) {
|
try {
|
@SuppressWarnings("unchecked")
|
List<String> appUserIds = (List<String>) request.get("appUserIds");
|
String message = (String) request.get("message");
|
|
if (appUserIds == null || appUserIds.isEmpty()) {
|
return R.fail("用户ID列表不能为空");
|
}
|
if (message == null || message.trim().isEmpty()) {
|
return R.fail("消息内容不能为空");
|
}
|
|
int successUserCount = webSocketUserService.sendMessageToUsers(appUserIds, message);
|
return R.ok("消息发送完成,成功发送到 " + successUserCount + " 个用户");
|
} catch (Exception e) {
|
log.error("向多个用户发送消息失败", e);
|
return R.fail("发送消息失败: " + e.getMessage());
|
}
|
}
|
|
/**
|
* 发送任务分配消息
|
* @param request 请求参数
|
* @return 操作结果
|
*/
|
@PostMapping("/user/task-assignment")
|
public R<String> sendTaskAssignment(@RequestBody Map<String, String> request) {
|
try {
|
String taskId = request.get("taskId");
|
String taskName = request.get("taskName");
|
String assignee = request.get("assignee");
|
String assigneeUserId = request.get("assigneeUserId");
|
|
if (taskId == null || taskName == null || assignee == null) {
|
return R.fail("任务ID、任务名称和分配人不能为空");
|
}
|
|
int successCount = webSocketUserService.sendTaskAssignment(taskId, taskName, assignee, assigneeUserId);
|
return R.ok("任务分配消息发送完成,成功发送到 " + successCount + " 个设备");
|
} catch (Exception e) {
|
log.error("发送任务分配消息失败", e);
|
return R.fail("发送任务分配消息失败: " + e.getMessage());
|
}
|
}
|
|
/**
|
* 发送状态更新消息
|
* @param request 请求参数
|
* @return 操作结果
|
*/
|
@PostMapping("/user/status-update")
|
public R<String> sendStatusUpdate(@RequestBody Map<String, String> request) {
|
try {
|
String status = request.get("status");
|
String details = request.get("details");
|
String targetUserId = request.get("targetUserId");
|
|
if (status == null || status.trim().isEmpty()) {
|
return R.fail("状态不能为空");
|
}
|
|
int successCount = webSocketUserService.sendStatusUpdate(status, details, targetUserId);
|
return R.ok("状态更新消息发送完成,成功发送到 " + successCount + " 个设备");
|
} catch (Exception e) {
|
log.error("发送状态更新消息失败", e);
|
return R.fail("发送状态更新消息失败: " + e.getMessage());
|
}
|
}
|
|
/**
|
* 发送任务状态更新消息
|
* @param request 请求参数
|
* @return 操作结果
|
*/
|
@PostMapping("/user/task-status-update")
|
public R<String> sendTaskStatusUpdate(@RequestBody Map<String, String> request) {
|
try {
|
String taskId = request.get("taskId");
|
String taskStatus = request.get("taskStatus");
|
String message = request.get("message");
|
String targetUserId = request.get("targetUserId");
|
|
if (taskId == null || taskStatus == null) {
|
return R.fail("任务ID和任务状态不能为空");
|
}
|
|
int successCount = webSocketUserService.sendTaskStatusUpdate(taskId, taskStatus, message, targetUserId);
|
return R.ok("任务状态更新消息发送完成,成功发送到 " + successCount + " 个设备");
|
} catch (Exception e) {
|
log.error("发送任务状态更新消息失败", e);
|
return R.fail("发送任务状态更新消息失败: " + e.getMessage());
|
}
|
}
|
|
/**
|
* 发送位置更新消息
|
* @param request 请求参数
|
* @return 操作结果
|
*/
|
@PostMapping("/user/location-update")
|
public R<String> sendLocationUpdate(@RequestBody Map<String, String> request) {
|
try {
|
String userId = request.get("userId");
|
String longitude = request.get("longitude");
|
String latitude = request.get("latitude");
|
String address = request.get("address");
|
|
if (userId == null || longitude == null || latitude == null) {
|
return R.fail("用户ID、经度和纬度不能为空");
|
}
|
|
int successCount = webSocketUserService.sendLocationUpdate(userId, longitude, latitude, address);
|
return R.ok("位置更新消息发送完成,成功发送到 " + successCount + " 个设备");
|
} catch (Exception e) {
|
log.error("发送位置更新消息失败", e);
|
return R.fail("发送位置更新消息失败: " + e.getMessage());
|
}
|
}
|
|
/**
|
* 获取用户连接信息
|
* @param appUserId 用户ID
|
* @return 连接信息
|
*/
|
@GetMapping("/user/info/{appUserId}")
|
public R getUserConnectionInfo(@PathVariable String appUserId) {
|
try {
|
Map<String, Object> info = webSocketUserService.getUserConnectionInfo(appUserId);
|
return R.ok(info, "获取用户连接信息成功");
|
} catch (Exception e) {
|
log.error("获取用户连接信息失败", e);
|
return R.fail("获取用户连接信息失败: " + e.getMessage());
|
}
|
}
|
|
/**
|
* 获取所有连接统计信息
|
* @return 统计信息
|
*/
|
@GetMapping("/user/stats")
|
public R getConnectionStats() {
|
try {
|
Map<String, Object> stats = webSocketUserService.getConnectionStats();
|
return R.ok(stats, "获取连接统计信息成功");
|
} catch (Exception e) {
|
log.error("获取连接统计信息失败", e);
|
return R.fail("获取连接统计信息失败: " + e.getMessage());
|
}
|
}
|
|
/**
|
* 检查用户是否在线
|
* @param appUserId 用户ID
|
* @return 检查结果
|
*/
|
@GetMapping("/user/online/{appUserId}")
|
public R checkUserOnline(@PathVariable String appUserId) {
|
try {
|
boolean isOnline = webSocketUserService.isUserOnline(appUserId);
|
return R.ok(isOnline, "用户在线状态检查完成");
|
} catch (Exception e) {
|
log.error("检查用户在线状态失败", e);
|
return R.fail("检查用户在线状态失败: " + e.getMessage());
|
}
|
}
|
|
/**
|
* 获取在线用户列表
|
* @return 在线用户列表
|
*/
|
@GetMapping("/user/online-list")
|
public R getOnlineUserIds() {
|
try {
|
Set<String> onlineUserIds = webSocketUserService.getOnlineUserIds();
|
return R.ok(onlineUserIds, "获取在线用户列表成功");
|
} catch (Exception e) {
|
log.error("获取在线用户列表失败", e);
|
return R.fail("获取在线用户列表失败: " + e.getMessage());
|
}
|
}
|
}
|