package com.xiaopeng.pushMessage.webSocket.controller;
|
|
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.SendCouponMessageVo;
|
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 lombok.extern.slf4j.Slf4j;
|
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控制器
|
* 提供REST接口用于触发服务端主动发送消息
|
* 支持基于AppUser的消息发送
|
*/
|
@Slf4j
|
@Api(tags = "socket消息推送")
|
@RestController
|
public class WebSocketController extends BaseController {
|
|
|
@Autowired
|
private WebSocketUserService webSocketUserService;
|
|
@Autowired
|
private ObjectMapper om;
|
|
@Resource
|
private RedisService redisService;
|
|
// ==================== 基于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 dto 消息实体
|
* @return 操作结果
|
*/
|
@ApiOperation(value = "发送订单状态")
|
@PostMapping("/logan/inner/websocket/order/sendStatus")
|
public R<String> sendStatus(@RequestBody SendMessageDTO dto) {
|
try {
|
String message = dto.getMessage();
|
if (message.trim().isEmpty()) {
|
return R.fail("消息内容不能为空");
|
}
|
String appUserId = dto.getAppUserId();
|
if (appUserId.trim().isEmpty()) {
|
return R.fail("用户id不能为空");
|
}
|
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());
|
}
|
}
|
|
|
|
@PostMapping("/base/websocket/order/sendStatusTest")
|
public R<String> sendStatusTest(@RequestBody SendMessageDTO dto) {
|
try {
|
String message = dto.getMessage();
|
if (message.trim().isEmpty()) {
|
return R.fail("消息内容不能为空");
|
}
|
String appUserId = dto.getAppUserId();
|
if (appUserId.trim().isEmpty()) {
|
return R.fail("用户id不能为空");
|
}
|
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=行程线路
|
* {
|
* "appUserId":"-1",
|
* "message":"{\"type\":\"ORDER_ROUTE\",\"content\":\"{\\\"orderId\\\":\\\"1983875114637811713\\\",\\\"routeType\\\":1,\\\"route\\\":\\\"{\\\\\\\"distance\\\\\\\":200,\\\\\\\"duration\\\\\\\":300,\\\\\\\"routeID\\\\\\\":\\\\\\\"\\\\\\\",\\\\\\\"polyline\\\\\\\":[{\\\\\\\"latitude\\\\\\\":39.932109,\\\\\\\"longitude\\\\\\\":116.362743},{\\\\\\\"latitude\\\\\\\":39.93224,\\\\\\\"longitude\\\\\\\":116.362743},{\\\\\\\"latitude\\\\\\\":39.932322,\\\\\\\"longitude\\\\\\\":116.362743},{\\\\\\\"latitude\\\\\\\":39.932322,\\\\\\\"longitude\\\\\\\":116.36474},{\\\\\\\"latitude\\\\\\\":39.932331,\\\\\\\"longitude\\\\\\\":116.367188},{\\\\\\\"latitude\\\\\\\":39.932335,\\\\\\\"longitude\\\\\\\":116.367947},{\\\\\\\"latitude\\\\\\\":39.932339,\\\\\\\"longitude\\\\\\\":116.368064},{\\\\\\\"latitude\\\\\\\":39.9324,\\\\\\\"longitude\\\\\\\":116.369214},{\\\\\\\"latitude\\\\\\\":39.932413,\\\\\\\"longitude\\\\\\\":116.369614},{\\\\\\\"latitude\\\\\\\":39.932474,\\\\\\\"longitude\\\\\\\":116.370556},{\\\\\\\"latitude\\\\\\\":39.93253,\\\\\\\"longitude\\\\\\\":116.371489},{\\\\\\\"latitude\\\\\\\":39.932609,\\\\\\\"longitude\\\\\\\":116.372956},{\\\\\\\"latitude\\\\\\\":39.932609,\\\\\\\"longitude\\\\\\\":116.373056},{\\\\\\\"latitude\\\\\\\":39.932708,\\\\\\\"longitude\\\\\\\":116.375833},{\\\\\\\"latitude\\\\\\\":39.932747,\\\\\\\"longitude\\\\\\\":116.376858},{\\\\\\\"latitude\\\\\\\":39.93286,\\\\\\\"longitude\\\\\\\":116.378902},{\\\\\\\"latitude\\\\\\\":39.932904,\\\\\\\"longitude\\\\\\\":116.379544},{\\\\\\\"latitude\\\\\\\":39.93293,\\\\\\\"longitude\\\\\\\":116.380013},{\\\\\\\"latitude\\\\\\\":39.932938,\\\\\\\"longitude\\\\\\\":116.380221},{\\\\\\\"latitude\\\\\\\":39.932969,\\\\\\\"longitude\\\\\\\":116.381224},{\\\\\\\"latitude\\\\\\\":39.932995,\\\\\\\"longitude\\\\\\\":116.381949},{\\\\\\\"latitude\\\\\\\":39.933038,\\\\\\\"longitude\\\\\\\":116.382908},{\\\\\\\"latitude\\\\\\\":39.933082,\\\\\\\"longitude\\\\\\\":116.384141},{\\\\\\\"latitude\\\\\\\":39.933095,\\\\\\\"longitude\\\\\\\":116.384631},{\\\\\\\"latitude\\\\\\\":39.933155,\\\\\\\"longitude\\\\\\\":116.386315},{\\\\\\\"latitude\\\\\\\":39.933181,\\\\\\\"longitude\\\\\\\":116.387023},{\\\\\\\"latitude\\\\\\\":39.933186,\\\\\\\"longitude\\\\\\\":116.387118},{\\\\\\\"latitude\\\\\\\":39.933234,\\\\\\\"longitude\\\\\\\":116.388294},{\\\\\\\"latitude\\\\\\\":39.933277,\\\\\\\"longitude\\\\\\\":116.389158},{\\\\\\\"latitude\\\\\\\":39.933299,\\\\\\\"longitude\\\\\\\":116.389653},{\\\\\\\"latitude\\\\\\\":39.933303,\\\\\\\"longitude\\\\\\\":116.389731},{\\\\\\\"latitude\\\\\\\":39.93332,\\\\\\\"longitude\\\\\\\":116.390043},{\\\\\\\"latitude\\\\\\\":39.933333,\\\\\\\"longitude\\\\\\\":116.390174},{\\\\\\\"latitude\\\\\\\":39.933381,\\\\\\\"longitude\\\\\\\":116.391428},{\\\\\\\"latitude\\\\\\\":39.933394,\\\\\\\"longitude\\\\\\\":116.391845},{\\\\\\\"latitude\\\\\\\":39.933429,\\\\\\\"longitude\\\\\\\":116.392813},{\\\\\\\"latitude\\\\\\\":39.933429,\\\\\\\"longitude\\\\\\\":116.392895},{\\\\\\\"latitude\\\\\\\":39.933416,\\\\\\\"longitude\\\\\\\":116.393303},{\\\\\\\"latitude\\\\\\\":39.933411,\\\\\\\"longitude\\\\\\\":116.39372},{\\\\\\\"latitude\\\\\\\":39.933394,\\\\\\\"longitude\\\\\\\":116.395091},{\\\\\\\"latitude\\\\\\\":39.933385,\\\\\\\"longitude\\\\\\\":116.396254},{\\\\\\\"latitude\\\\\\\":39.933381,\\\\\\\"longitude\\\\\\\":116.396324},{\\\\\\\"latitude\\\\\\\":39.933381,\\\\\\\"longitude\\\\\\\":116.396471},{\\\\\\\"latitude\\\\\\\":39.933385,\\\\\\\"longitude\\\\\\\":116.397726},{\\\\\\\"latitude\\\\\\\":39.933394,\\\\\\\"longitude\\\\\\\":116.399635},{\\\\\\\"latitude\\\\\\\":39.933403,\\\\\\\"longitude\\\\\\\":116.400477},{\\\\\\\"latitude\\\\\\\":39.933416,\\\\\\\"longitude\\\\\\\":116.402747},{\\\\\\\"latitude\\\\\\\":39.933416,\\\\\\\"longitude\\\\\\\":116.403099},{\\\\\\\"latitude\\\\\\\":39.933424,\\\\\\\"longitude\\\\\\\":116.40428},{\\\\\\\"latitude\\\\\\\":39.933416,\\\\\\\"longitude\\\\\\\":116.405069},{\\\\\\\"latitude\\\\\\\":39.933407,\\\\\\\"longitude\\\\\\\":116.405768},{\\\\\\\"latitude\\\\\\\":39.933407,\\\\\\\"longitude\\\\\\\":116.405846},{\\\\\\\"latitude\\\\\\\":39.933403,\\\\\\\"longitude\\\\\\\":116.406328},{\\\\\\\"latitude\\\\\\\":39.933368,\\\\\\\"longitude\\\\\\\":116.407548},{\\\\\\\"latitude\\\\\\\":39.933338,\\\\\\\"longitude\\\\\\\":116.408793},{\\\\\\\"latitude\\\\\\\":39.933338,\\\\\\\"longitude\\\\\\\":116.408889},{\\\\\\\"latitude\\\\\\\":39.933342,\\\\\\\"longitude\\\\\\\":116.410056},{\\\\\\\"latitude\\\\\\\":39.933359,\\\\\\\"longitude\\\\\\\":116.411133},{\\\\\\\"latitude\\\\\\\":39.933368,\\\\\\\"longitude\\\\\\\":116.411918},{\\\\\\\"latitude\\\\\\\":39.93339,\\\\\\\"longitude\\\\\\\":116.413433},{\\\\\\\"latitude\\\\\\\":39.933411,\\\\\\\"longitude\\\\\\\":116.414796},{\\\\\\\"latitude\\\\\\\":39.933451,\\\\\\\"longitude\\\\\\\":116.415547},{\\\\\\\"latitude\\\\\\\":39.933494,\\\\\\\"longitude\\\\\\\":116.415933},{\\\\\\\"latitude\\\\\\\":39.933594,\\\\\\\"longitude\\\\\\\":116.417101},{\\\\\\\"latitude\\\\\\\":39.933594,\\\\\\\"longitude\\\\\\\":116.417192},{\\\\\\\"latitude\\\\\\\":39.93362,\\\\\\\"longitude\\\\\\\":116.41888},{\\\\\\\"latitude\\\\\\\":39.933628,\\\\\\\"longitude\\\\\\\":116.419392},{\\\\\\\"latitude\\\\\\\":39.933628,\\\\\\\"longitude\\\\\\\":116.4198},{\\\\\\\"latitude\\\\\\\":39.933633,\\\\\\\"longitude\\\\\\\":116.421033},{\\\\\\\"latitude\\\\\\\":39.933628,\\\\\\\"longitude\\\\\\\":116.421823},{\\\\\\\"latitude\\\\\\\":39.933615,\\\\\\\"longitude\\\\\\\":116.423646},{\\\\\\\"latitude\\\\\\\":39.93362,\\\\\\\"longitude\\\\\\\":116.42431},{\\\\\\\"latitude\\\\\\\":39.933637,\\\\\\\"longitude\\\\\\\":116.42579},{\\\\\\\"latitude\\\\\\\":39.933633,\\\\\\\"longitude\\\\\\\":116.425946},{\\\\\\\"latitude\\\\\\\":39.93362,\\\\\\\"longitude\\\\\\\":116.427031},{\\\\\\\"latitude\\\\\\\":39.93362,\\\\\\\"longitude\\\\\\\":116.428012},{\\\\\\\"latitude\\\\\\\":39.933628,\\\\\\\"longitude\\\\\\\":116.42855},{\\\\\\\"latitude\\\\\\\":39.933628,\\\\\\\"longitude\\\\\\\":116.428672},{\\\\\\\"latitude\\\\\\\":39.933628,\\\\\\\"longitude\\\\\\\":116.428711},{\\\\\\\"latitude\\\\\\\":39.933628,\\\\\\\"longitude\\\\\\\":116.428876},{\\\\\\\"latitude\\\\\\\":39.933633,\\\\\\\"longitude\\\\\\\":116.429609},{\\\\\\\"latitude\\\\\\\":39.933637,\\\\\\\"longitude\\\\\\\":116.430282},{\\\\\\\"latitude\\\\\\\":39.933633,\\\\\\\"longitude\\\\\\\":116.430464},{\\\\\\\"latitude\\\\\\\":39.933637,\\\\\\\"longitude\\\\\\\":116.431458},{\\\\\\\"latitude\\\\\\\":39.933637,\\\\\\\"longitude\\\\\\\":116.431641},{\\\\\\\"latitude\\\\\\\":39.933637,\\\\\\\"longitude\\\\\\\":116.432305},{\\\\\\\"latitude\\\\\\\":39.933615,\\\\\\\"longitude\\\\\\\":116.433103},{\\\\\\\"latitude\\\\\\\":39.933611,\\\\\\\"longitude\\\\\\\":116.433168},{\\\\\\\"latitude\\\\\\\":39.933594,\\\\\\\"longitude\\\\\\\":116.433255},{\\\\\\\"latitude\\\\\\\":39.933529,\\\\\\\"longitude\\\\\\\":116.43342},{\\\\\\\"latitude\\\\\\\":39.933529,\\\\\\\"longitude\\\\\\\":116.43342},{\\\\\\\"latitude\\\\\\\":39.933451,\\\\\\\"longitude\\\\\\\":116.433494},{\\\\\\\"latitude\\\\\\\":39.933333,\\\\\\\"longitude\\\\\\\":116.43365},{\\\\\\\"latitude\\\\\\\":39.933294,\\\\\\\"longitude\\\\\\\":116.433724},{\\\\\\\"latitude\\\\\\\":39.933268,\\\\\\\"longitude\\\\\\\":116.433811},{\\\\\\\"latitude\\\\\\\":39.933255,\\\\\\\"longitude\\\\\\\":116.433906},{\\\\\\\"latitude\\\\\\\":39.933264,\\\\\\\"longitude\\\\\\\":116.434371},{\\\\\\\"latitude\\\\\\\":39.933277,\\\\\\\"longitude\\\\\\\":116.434466},{\\\\\\\"latitude\\\\\\\":39.93329,\\\\\\\"longitude\\\\\\\":116.434497},{\\\\\\\"latitude\\\\\\\":39.933312,\\\\\\\"longitude\\\\\\\":116.434536},{\\\\\\\"latitude\\\\\\\":39.933381,\\\\\\\"longitude\\\\\\\":116.434644},{\\\\\\\"latitude\\\\\\\":39.933516,\\\\\\\"longitude\\\\\\\":116.434783},{\\\\\\\"latitude\\\\\\\":39.933594,\\\\\\\"longitude\\\\\\\":116.434857},{\\\\\\\"latitude\\\\\\\":39.933594,\\\\\\\"longitude\\\\\\\":116.434857},{\\\\\\\"latitude\\\\\\\":39.933628,\\\\\\\"longitude\\\\\\\":116.434948},{\\\\\\\"latitude\\\\\\\":39.933641,\\\\\\\"longitude\\\\\\\":116.435056},{\\\\\\\"latitude\\\\\\\":39.93365,\\\\\\\"longitude\\\\\\\":116.435269},{\\\\\\\"latitude\\\\\\\":39.933641,\\\\\\\"longitude\\\\\\\":116.436619},{\\\\\\\"latitude\\\\\\\":39.933641,\\\\\\\"longitude\\\\\\\":116.436619},{\\\\\\\"latitude\\\\\\\":39.93286,\\\\\\\"longitude\\\\\\\":116.436606},{\\\\\\\"latitude\\\\\\\":39.932405,\\\\\\\"longitude\\\\\\\":116.43661},{\\\\\\\"latitude\\\\\\\":39.932075,\\\\\\\"longitude\\\\\\\":116.43661},{\\\\\\\"latitude\\\\\\\":39.931866,\\\\\\\"longitude\\\\\\\":116.43661},{\\\\\\\"latitude\\\\\\\":39.931628,\\\\\\\"longitude\\\\\\\":116.436602},{\\\\\\\"latitude\\\\\\\":39.930977,\\\\\\\"longitude\\\\\\\":116.436623},{\\\\\\\"latitude\\\\\\\":39.93049,\\\\\\\"longitude\\\\\\\":116.436701},{\\\\\\\"latitude\\\\\\\":39.930278,\\\\\\\"longitude\\\\\\\":116.43678},{\\\\\\\"latitude\\\\\\\":39.930278,\\\\\\\"longitude\\\\\\\":116.43678},{\\\\\\\"latitude\\\\\\\":39.930239,\\\\\\\"longitude\\\\\\\":116.435699},{\\\\\\\"latitude\\\\\\\":39.930239,\\\\\\\"longitude\\\\\\\":116.434965},{\\\\\\\"latitude\\\\\\\":39.93023,\\\\\\\"longitude\\\\\\\":116.434497},{\\\\\\\"latitude\\\\\\\":39.93023,\\\\\\\"longitude\\\\\\\":116.434497},{\\\\\\\"latitude\\\\\\\":39.930755,\\\\\\\"longitude\\\\\\\":116.434475},{\\\\\\\"latitude\\\\\\\":39.930946,\\\\\\\"longitude\\\\\\\":116.434466},{\\\\\\\"latitude\\\\\\\":39.931124,\\\\\\\"longitude\\\\\\\":116.434457}]}\\\"}\",\"timestamp\":\"176569945845456\"}"
|
* }
|
*
|
* @param dto 消息实体
|
* @return 操作结果
|
*/
|
@ApiOperation(value = "发送车辆路线、时间")
|
@PostMapping("/logan/inner/websocket/order/sendRoute")
|
public R<String> sendRoute(@RequestBody SendMessageDTO dto) {
|
try {
|
String message = dto.getMessage();
|
if (message.trim().isEmpty()) {
|
return R.fail("消息内容不能为空");
|
}
|
String appUserId = dto.getAppUserId();
|
if (appUserId.trim().isEmpty()) {
|
return R.fail("用户id不能为空");
|
}
|
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());
|
}
|
}
|
|
/**
|
* 发送车辆实时定位
|
* {
|
* "appUserId":"-1",
|
* "message":"{\"type\":\"VEHICLE_LOCATION\",\"content\":\"{\\\"orderId\\\":\\\"1983875114637811713\\\",\\\"lon\\\":116.362743,\\\"lat\\\":39.93224}\",\"timestamp\":\"176569945845456\"}"
|
* }
|
*
|
* @param dto 消息实体
|
* @return 操作结果
|
*/
|
@ApiOperation(value = "发送车辆实时定位")
|
@PostMapping("/inner/websocket/order/sendVehicleLocation")
|
public R<String> sendVehicleLocation(@RequestBody SendMessageDTO dto) {
|
try {
|
String message = dto.getMessage();
|
if (message.trim().isEmpty()) {
|
return R.fail("消息内容不能为空");
|
}
|
String appUserId = dto.getAppUserId();
|
if (appUserId.trim().isEmpty()) {
|
return R.fail("用户id不能为空");
|
}
|
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());
|
}
|
}
|
|
/**
|
* 发送是否开启倒计时
|
* {
|
* "appUserId":"-1",
|
* {"type":"OPEN_TRUNK","content":"{\"orderId\":\"1983875114637811713\",\"isOpen\":1}","timestamp":"176569945845456"}
|
* }
|
*
|
* @param dto 消息实体
|
* @return 操作结果
|
*/
|
@ApiOperation(value = "发送是否开启倒计时")
|
@PostMapping("/inner/websocket/order/sendOpenTrunk")
|
public R<String> sendOpenTrunk(@RequestBody SendMessageDTO dto) {
|
try {
|
String message = dto.getMessage();
|
if (message.trim().isEmpty()) {
|
return R.fail("消息内容不能为空");
|
}
|
String appUserId = dto.getAppUserId();
|
if (appUserId.trim().isEmpty()) {
|
return R.fail("用户id不能为空");
|
}
|
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());
|
}
|
}
|
|
|
/**
|
* 运维工单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());
|
}
|
}
|
|
|
|
/**
|
* 推送优惠券消息
|
* @param vo
|
* @return
|
*/
|
@PostMapping("/logan/inner/websocket/coupon/sendCouponMessage")
|
public R<?> sendCouponMessage(@RequestBody SendCouponMessageVo vo){
|
String message = vo.getMessage();
|
if (message.trim().isEmpty()) {
|
return R.fail("消息内容不能为空");
|
}
|
String appUserId = vo.getAppUserId().toString();
|
if (appUserId.trim().isEmpty()) {
|
return R.fail("用户id不能为空");
|
}
|
int deviceCount = webSocketUserService.sendMessageToUser(appUserId, message);
|
if (deviceCount > 0) {
|
return R.ok("消息发送成功,发送到 " + deviceCount + " 个设备");
|
} else {
|
return R.fail("用户不在线或发送失败");
|
}
|
}
|
}
|