From 6e89255f1e31ee32ef44619144e1f70625215c26 Mon Sep 17 00:00:00 2001 From: Doubleyin <953994191@qq.com> Date: Sun, 19 Oct 2025 15:59:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8=E7=BA=BF=E5=88=97=E8=A1=A8=E5=88=B7?= =?UTF-8?q?=E6=96=B0=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 60 +++++++++++++++++++ .../delivery/controller/UserController.java | 20 ++++--- .../light/delivery/dto/SignInResponse.java | 42 +++++++++++++ .../light/delivery/dto/SignOutResponse.java | 26 ++++++++ .../service/LocationWebSocketHandler.java | 2 + 5 files changed, 141 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/light/delivery/dto/SignInResponse.java create mode 100644 src/main/java/com/light/delivery/dto/SignOutResponse.java diff --git a/README.md b/README.md index 79e4a55..627b75e 100644 --- a/README.md +++ b/README.md @@ -425,6 +425,66 @@ deploy.bat } ``` +## API接口响应格式 + +### 用户签到接口 `/user/signin` + +#### 请求参数 +- Method: `POST` +- Headers: `Authorization: Bearer ` +- Body: + ```json + { + "latitude": 25.0342, + "longitude": 102.7057, + "timestamp": 1634567890000 + } + ``` + +#### 成功响应 +```json +{ + "success": true, + "userInfo": { + "id": 123, + "name": "张三", + "phone": "13800138000", + "role": "DELIVERY_PERSON", + "openid": "oK4fS5ABCDEF123456" + } +} +``` + +#### 失败响应 +```json +{ + "success": false, + "message": "错误信息" +} +``` + +### 用户签退接口 `/user/signout` + +#### 请求参数 +- Method: `POST` +- Headers: `Authorization: Bearer ` + +#### 成功响应 +```json +{ + "success": true, + "message": "签退成功" +} +``` + +#### 失败响应 +```json +{ + "success": false, + "message": "错误信息" +} +``` + ## 最近更新 ### WebSocket使用方式变更(重要变更) diff --git a/src/main/java/com/light/delivery/controller/UserController.java b/src/main/java/com/light/delivery/controller/UserController.java index aa21aa7..2990170 100644 --- a/src/main/java/com/light/delivery/controller/UserController.java +++ b/src/main/java/com/light/delivery/controller/UserController.java @@ -1,6 +1,8 @@ package com.light.delivery.controller; import com.light.delivery.dto.UserInfoResponse; +import com.light.delivery.dto.SignOutResponse; +import com.light.delivery.dto.SignInResponse; import com.light.delivery.model.LoginResponse; import com.light.delivery.model.RegisterRequest; import com.light.delivery.model.User; @@ -187,23 +189,23 @@ public class UserController { public ResponseEntity signIn(HttpServletRequest request, @RequestBody LocationWebSocketHandler.LocationData initialLocation) { String token = extractToken(request); if (token == null) { - return ResponseEntity.badRequest().body("Authorization token is missing"); + return ResponseEntity.badRequest().body(new SignInResponse(false, "Authorization token is missing")); } // 检查位置信息是否为空 if (initialLocation == null) { - return ResponseEntity.badRequest().body("Initial location information is required"); + return ResponseEntity.badRequest().body(new SignInResponse(false, "Initial location information is required")); } // 检查位置信息的必要字段是否为空 if (initialLocation.getLatitude() == null || initialLocation.getLongitude() == null) { - return ResponseEntity.badRequest().body("Latitude and longitude are required in location information"); + return ResponseEntity.badRequest().body(new SignInResponse(false, "Latitude and longitude are required in location information")); } try { User user = userService.getUserInfo(token); User updatedUser = userService.signIn(user.getId()); - UserInfoResponse response = toUserInfoResponse(updatedUser); + UserInfoResponse userInfoResponse = toUserInfoResponse(updatedUser); // 通知WebSocket处理器用户已签到并自动订阅 try { @@ -213,9 +215,9 @@ public class UserController { System.err.println("通知WebSocket签到状态时出错: " + e.getMessage()); } - return ResponseEntity.ok(response); + return ResponseEntity.ok(new SignInResponse(true, userInfoResponse)); } catch (Exception e) { - return ResponseEntity.badRequest().body("Invalid token: " + e.getMessage()); + return ResponseEntity.badRequest().body(new SignInResponse(false, "Invalid token: " + e.getMessage())); } } @@ -228,7 +230,7 @@ public class UserController { public ResponseEntity signOut(HttpServletRequest request) { String token = extractToken(request); if (token == null) { - return ResponseEntity.badRequest().body("Authorization token is missing"); + return ResponseEntity.badRequest().body(new SignOutResponse(false, "Authorization token is missing")); } try { @@ -242,9 +244,9 @@ public class UserController { System.err.println("通知WebSocket签退状态时出错: " + e.getMessage()); } - return ResponseEntity.ok("签退成功"); + return ResponseEntity.ok(new SignOutResponse(true, "签退成功")); } catch (Exception e) { - return ResponseEntity.badRequest().body("Invalid token: " + e.getMessage()); + return ResponseEntity.badRequest().body(new SignOutResponse(false, "Invalid token: " + e.getMessage())); } } diff --git a/src/main/java/com/light/delivery/dto/SignInResponse.java b/src/main/java/com/light/delivery/dto/SignInResponse.java new file mode 100644 index 0000000..e5d1adc --- /dev/null +++ b/src/main/java/com/light/delivery/dto/SignInResponse.java @@ -0,0 +1,42 @@ +package com.light.delivery.dto; + +import lombok.Data; + +/** + * 签到响应 DTO,用于统一签到接口的响应格式 + */ +@Data +public class SignInResponse { + /** + * 操作是否成功 + */ + private boolean success; + + /** + * 响应消息(可选) + */ + private String message; + + /** + * 用户信息(签到成功时返回) + */ + private UserInfoResponse userInfo; + + public SignInResponse() {} + + public SignInResponse(boolean success, String message) { + this.success = success; + this.message = message; + } + + public SignInResponse(boolean success, UserInfoResponse userInfo) { + this.success = success; + this.userInfo = userInfo; + } + + public SignInResponse(boolean success, String message, UserInfoResponse userInfo) { + this.success = success; + this.message = message; + this.userInfo = userInfo; + } +} \ No newline at end of file diff --git a/src/main/java/com/light/delivery/dto/SignOutResponse.java b/src/main/java/com/light/delivery/dto/SignOutResponse.java new file mode 100644 index 0000000..5cdf3ec --- /dev/null +++ b/src/main/java/com/light/delivery/dto/SignOutResponse.java @@ -0,0 +1,26 @@ +package com.light.delivery.dto; + +import lombok.Data; + +/** + * 签退响应 DTO,用于统一签退接口的响应格式 + */ +@Data +public class SignOutResponse { + /** + * 操作是否成功 + */ + private boolean success; + + /** + * 响应消息(可选) + */ + private String message; + + public SignOutResponse() {} + + public SignOutResponse(boolean success, String message) { + this.success = success; + this.message = message; + } +} \ No newline at end of file diff --git a/src/main/java/com/light/delivery/service/LocationWebSocketHandler.java b/src/main/java/com/light/delivery/service/LocationWebSocketHandler.java index 928c0d1..f55da87 100644 --- a/src/main/java/com/light/delivery/service/LocationWebSocketHandler.java +++ b/src/main/java/com/light/delivery/service/LocationWebSocketHandler.java @@ -267,6 +267,8 @@ public class LocationWebSocketHandler extends TextWebSocketHandler { System.out.println("用户已签到,尝试自动订阅"); try { autoSubscribe(session, userId); + // 立即发送在线用户列表给刚连接的用户 + sendOnlineUserList(session); } catch (IOException e) { System.err.println("自动订阅时出错: " + e.getMessage()); }