| New file |
| | |
| | | package com.xiaopeng.pushMessage.webSocket.controller; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.xiaopeng.common.core.domain.R; |
| | | import com.xiaopeng.common.core.web.controller.BaseController; |
| | | import com.xiaopeng.pushMessage.webSocket.service.WebSocketUserService; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | 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的消息发送 |
| | | */ |
| | | @Slf4j |
| | | @Api(tags = "socket消息推送") |
| | | @RestController |
| | | @RequestMapping("/inner/websocket") |
| | | public class WebSocketController extends BaseController { |
| | | |
| | | |
| | | @Autowired |
| | | private WebSocketUserService webSocketUserService; |
| | | |
| | | // ==================== 基于XpAppUser的WebSocket接口 ==================== |
| | | |
| | | // 消息格式 "{\"type\":\"mission_push\",\"content\":\"{\\\"missionId\\\":\\\"1983875114637811713\\\"}\",\"timestamp\":\"2025-10-27T09:22:45.681\"}"; |
| | | /** |
| | | * 发送订单状态 |
| | | * 状态 0=已取消,1=待接单,2=待出发,3=接驾中,4=待客上车,5=乘客已上车,6=服务中,7=待客下车,8=待付款,9=待评价,10=已完成 |
| | | * {"type":"ORDER_STATUS","content":"{\"orderId\":\"1983875114637811713\",\"status\":1}","timestamp":"176569945845456"} |
| | | * |
| | | * @param appUserId 用户id |
| | | * @param message 消息内容 |
| | | * @return 操作结果 |
| | | */ |
| | | @ApiOperation(value = "发送订单状态") |
| | | @PostMapping("/order/sendStatus") |
| | | public R<String> sendStatus(@RequestParam String appUserId, |
| | | @RequestParam String message) { |
| | | try { |
| | | if (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()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 发送车辆路线、时间 |
| | | * routeType : 1=接单线路 2=行程线路 |
| | | * {"type":"ORDER_ROUTE","content":"{\"orderId\":\"1983875114637811713\",\"routeType\":1,\"route\":"{}"}","timestamp":"176569945845456"} |
| | | * |
| | | * @param appUserId 用户id |
| | | * @param message 消息内容 |
| | | * @return 操作结果 |
| | | */ |
| | | @ApiOperation(value = "发送车辆路线、时间") |
| | | @PostMapping("/order/sendRoute") |
| | | public R<String> sendRoute(@RequestParam String appUserId, |
| | | @RequestParam String message) { |
| | | try { |
| | | if (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/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/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()); |
| | | } |
| | | } |
| | | } |