| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- package com.kxmall.web.controller.websocket;
- import com.kxmall.common.core.domain.R;
- import com.kxmall.web.controller.websocket.dto.OrderNotificationDTO;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.web.bind.annotation.*;
- import java.util.Date;
- /**
- * 骑手WebSocket管理控制器
- *
- * @author kxmall
- * @date 2025-01-08
- */
- @RestController
- @RequestMapping("/websocket/rider")
- @Slf4j
- public class RiderWebSocketController {
- /**
- * 获取在线骑手数量
- */
- @GetMapping("/online/count")
- public R<Integer> getOnlineCount() {
- int count = RiderWebSocketServer.getOnlineCount();
- return R.ok(count);
- }
- /**
- * 获取所有在线骑手ID
- */
- @GetMapping("/online/riders")
- public R<String[]> getOnlineRiders() {
- String[] riderIds = RiderWebSocketServer.getOnlineRiderIds();
- return R.ok(riderIds);
- }
- /**
- * 检查指定骑手是否在线
- */
- @GetMapping("/online/check/{riderId}")
- public R<Boolean> checkRiderOnline(@PathVariable String riderId) {
- boolean isOnline = RiderWebSocketServer.isRiderOnline(riderId);
- return R.ok(isOnline);
- }
- /**
- * 向指定骑手发送消息(测试用)
- */
- @PostMapping("/send/{riderId}")
- public R<Void> sendMessageToRider(@PathVariable String riderId, @RequestBody String message) {
- try {
- RiderWebSocketServer.sendInfo(message, riderId);
- log.info("向师傅{}发送消息成功: {}", riderId, message);
- return R.ok();
- } catch (Exception e) {
- log.error("向师傅{}发送消息失败", riderId, e);
- return R.fail("发送消息失败: " + e.getMessage());
- }
- }
- /**
- * 向所有在线骑手广播消息
- */
- @PostMapping("/broadcast")
- public R<Void> broadcastMessage(@RequestBody String message) {
- try {
- RiderWebSocketServer.sendToAll(message);
- log.info("广播消息成功: {}", message);
- return R.ok();
- } catch (Exception e) {
- log.error("广播消息失败", e);
- return R.fail("广播消息失败: " + e.getMessage());
- }
- }
- /**
- * 发送测试订单通知
- */
- @PostMapping("/test/order/{riderId}")
- public R<Void> sendTestOrderNotification(@PathVariable String riderId) {
- try {
- OrderNotificationDTO notification = new OrderNotificationDTO();
- notification.setType(OrderNotificationDTO.Type.NEW_ORDER);
- notification.setOrderNo("TEST" + System.currentTimeMillis());
- notification.setStoreName("测试仓库");
- notification.setConsignee("测试用户");
- notification.setPhone("138****8888");
- notification.setAddress("测试地址");
- notification.setCreateTime(new Date());
- notification.setRemark("这是一个测试订单通知");
- notification.setMatchScore(85.5);
- RiderWebSocketServer.sendOrderNotification(riderId, notification);
- log.info("向师傅{}发送测试订单通知成功", riderId);
- return R.ok();
- } catch (Exception e) {
- log.error("向师傅{}发送测试订单通知失败", riderId, e);
- return R.fail("发送测试通知失败: " + e.getMessage());
- }
- }
- }
|