xuhy
1 天以前 a960c432d78dfe5f0ef07295d0210ddb09340e12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package com.ruoyi.web.controller.webSocket;
 
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
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的消息发送
 */
@RestController
@RequestMapping("/open/websocket")
@Slf4j
public class WebSocketController extends BaseController {
 
 
    @Autowired
    private WebSocketUserService webSocketUserService;
 
    // ==================== 基于AppUser的WebSocket接口 ====================
 
    /**
     * 向指定用户发送消息
     * @param appUserId 用户ID
     * @param request 请求参数
     * @return 操作结果
     */
    @PostMapping("/user/send/{appUserId}")
    public R<String> sendMessageToUser(@PathVariable String appUserId, @RequestBody Map<String, String> request) {
        try {
            String message = request.get("message");
            if (message == null || 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/task-assignment")
    public R<String> sendTaskAssignment(@RequestBody Map<String, String> request) {
        try {
            String taskId = request.get("taskId");
            String taskName = request.get("taskName");
            String assignee = request.get("assignee");
            String assigneeUserId = request.get("assigneeUserId");
 
            if (taskId == null || taskName == null || assignee == null) {
                return R.fail("任务ID、任务名称和分配人不能为空");
            }
 
            int successCount = webSocketUserService.sendTaskAssignment(taskId, taskName, assignee, assigneeUserId);
            return R.ok("任务分配消息发送完成,成功发送到 " + successCount + " 个设备");
        } 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/task-status-update")
    public R<String> sendTaskStatusUpdate(@RequestBody Map<String, String> request) {
        try {
            String taskId = request.get("taskId");
            String taskStatus = request.get("taskStatus");
            String message = request.get("message");
            String targetUserId = request.get("targetUserId");
 
            if (taskId == null || taskStatus == null) {
                return R.fail("任务ID和任务状态不能为空");
            }
 
            int successCount = webSocketUserService.sendTaskStatusUpdate(taskId, taskStatus, message, 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());
        }
    }
}