RiderWebSocketController.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package com.kxmall.web.controller.websocket;
  2. import com.kxmall.common.core.domain.R;
  3. import com.kxmall.web.controller.websocket.dto.OrderNotificationDTO;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.springframework.web.bind.annotation.*;
  6. import java.util.Date;
  7. /**
  8. * 骑手WebSocket管理控制器
  9. *
  10. * @author kxmall
  11. * @date 2025-01-08
  12. */
  13. @RestController
  14. @RequestMapping("/websocket/rider")
  15. @Slf4j
  16. public class RiderWebSocketController {
  17. /**
  18. * 获取在线骑手数量
  19. */
  20. @GetMapping("/online/count")
  21. public R<Integer> getOnlineCount() {
  22. int count = RiderWebSocketServer.getOnlineCount();
  23. return R.ok(count);
  24. }
  25. /**
  26. * 获取所有在线骑手ID
  27. */
  28. @GetMapping("/online/riders")
  29. public R<String[]> getOnlineRiders() {
  30. String[] riderIds = RiderWebSocketServer.getOnlineRiderIds();
  31. return R.ok(riderIds);
  32. }
  33. /**
  34. * 检查指定骑手是否在线
  35. */
  36. @GetMapping("/online/check/{riderId}")
  37. public R<Boolean> checkRiderOnline(@PathVariable String riderId) {
  38. boolean isOnline = RiderWebSocketServer.isRiderOnline(riderId);
  39. return R.ok(isOnline);
  40. }
  41. /**
  42. * 向指定骑手发送消息(测试用)
  43. */
  44. @PostMapping("/send/{riderId}")
  45. public R<Void> sendMessageToRider(@PathVariable String riderId, @RequestBody String message) {
  46. try {
  47. RiderWebSocketServer.sendInfo(message, riderId);
  48. log.info("向师傅{}发送消息成功: {}", riderId, message);
  49. return R.ok();
  50. } catch (Exception e) {
  51. log.error("向师傅{}发送消息失败", riderId, e);
  52. return R.fail("发送消息失败: " + e.getMessage());
  53. }
  54. }
  55. /**
  56. * 向所有在线骑手广播消息
  57. */
  58. @PostMapping("/broadcast")
  59. public R<Void> broadcastMessage(@RequestBody String message) {
  60. try {
  61. RiderWebSocketServer.sendToAll(message);
  62. log.info("广播消息成功: {}", message);
  63. return R.ok();
  64. } catch (Exception e) {
  65. log.error("广播消息失败", e);
  66. return R.fail("广播消息失败: " + e.getMessage());
  67. }
  68. }
  69. /**
  70. * 发送测试订单通知
  71. */
  72. @PostMapping("/test/order/{riderId}")
  73. public R<Void> sendTestOrderNotification(@PathVariable String riderId) {
  74. try {
  75. OrderNotificationDTO notification = new OrderNotificationDTO();
  76. notification.setType(OrderNotificationDTO.Type.NEW_ORDER);
  77. notification.setOrderNo("TEST" + System.currentTimeMillis());
  78. notification.setStoreName("测试仓库");
  79. notification.setConsignee("测试用户");
  80. notification.setPhone("138****8888");
  81. notification.setAddress("测试地址");
  82. notification.setCreateTime(new Date());
  83. notification.setRemark("这是一个测试订单通知");
  84. notification.setMatchScore(85.5);
  85. RiderWebSocketServer.sendOrderNotification(riderId, notification);
  86. log.info("向师傅{}发送测试订单通知成功", riderId);
  87. return R.ok();
  88. } catch (Exception e) {
  89. log.error("向师傅{}发送测试订单通知失败", riderId, e);
  90. return R.fail("发送测试通知失败: " + e.getMessage());
  91. }
  92. }
  93. }