| | |
| | | package com.xiaopeng.pushMessage.webSocket.controller; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.fasterxml.jackson.core.JsonProcessingException; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.xiaopeng.api.dto.pushMessage.SendMessageDTO; |
| | | import com.xiaopeng.api.vo.XpTaskVO; |
| | | import com.xiaopeng.api.vo.pushMessage.UpdateEndAddStatusVo; |
| | | import com.xiaopeng.common.core.domain.R; |
| | | import com.xiaopeng.common.core.web.controller.BaseController; |
| | | import com.xiaopeng.common.redis.service.RedisService; |
| | | import com.xiaopeng.pushMessage.webSocket.service.WebSocketUserService; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | /** |
| | | * WebSocket控制器 |
| | |
| | | |
| | | @Autowired |
| | | private WebSocketUserService webSocketUserService; |
| | | |
| | | @Autowired |
| | | private ObjectMapper om; |
| | | |
| | | @Resource |
| | | private RedisService redisService; |
| | | |
| | | // ==================== 基于XpAppUser的WebSocket接口 ==================== |
| | | |
| | |
| | | return R.fail("发送消息失败: " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | @PostMapping("/base/websocket/order/sendStatusTest") |
| | | public R<String> sendStatusTest(@RequestBody SendMessageDTO dto) { |
| | |
| | | * @return 操作结果 |
| | | */ |
| | | @ApiOperation(value = "发送车辆实时定位") |
| | | @PostMapping("/logan/inner/websocket/order/sendVehicleLocation") |
| | | @PostMapping("/inner/websocket/order/sendVehicleLocation") |
| | | public R<String> sendVehicleLocation(@RequestBody SendMessageDTO dto) { |
| | | try { |
| | | String message = dto.getMessage(); |
| | |
| | | * @return 操作结果 |
| | | */ |
| | | @ApiOperation(value = "发送是否开启倒计时") |
| | | @PostMapping("/logan/inner/websocket/order/sendOpenTrunk") |
| | | @PostMapping("/inner/websocket/order/sendOpenTrunk") |
| | | public R<String> sendOpenTrunk(@RequestBody SendMessageDTO dto) { |
| | | try { |
| | | String message = dto.getMessage(); |
| | |
| | | return R.fail("获取在线用户列表失败: " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 运维工单app推送 |
| | | */ |
| | | @PostMapping("/inner/websocket/push/task") |
| | | public R<?> pushTask(@RequestBody XpTaskVO vo) throws JsonProcessingException { |
| | | |
| | | String handleUserId = vo.getHandleUserId(); |
| | | Map<String, Object> msg = new HashMap<>(); |
| | | msg.put("type", "task"); |
| | | msg.put("data", vo); |
| | | webSocketUserService.sendMessageToUser(handleUserId, om.writeValueAsString(msg)); |
| | | return R.ok(); |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 推送订单修改目的地状态消息 |
| | | * @param vo |
| | | * @return |
| | | */ |
| | | @PostMapping("/logan/inner/websocket/order/sendUpdateEndAddStatus") |
| | | public R<?> sendUpdateEndAddStatus(@RequestBody UpdateEndAddStatusVo vo){ |
| | | try { |
| | | String message = vo.getMessage(); |
| | | if (message.trim().isEmpty()) { |
| | | return R.fail("消息内容不能为空"); |
| | | } |
| | | String appUserId = vo.getAppUserId().toString(); |
| | | if (appUserId.trim().isEmpty()) { |
| | | return R.fail("用户id不能为空"); |
| | | } |
| | | redisService.setCacheObject("UPDATE_END_ADDRESS_STATUS_MSG:" + vo, System.currentTimeMillis(), 5L, TimeUnit.SECONDS); |
| | | 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()); |
| | | } |
| | | } |
| | | |
| | | } |