在线列表刷新逻辑
All checks were successful
构建并部署 Spring Boot 应用 / build-and-deploy (push) Successful in 15m20s
All checks were successful
构建并部署 Spring Boot 应用 / build-and-deploy (push) Successful in 15m20s
This commit is contained in:
60
README.md
60
README.md
@@ -425,6 +425,66 @@ deploy.bat
|
||||
}
|
||||
```
|
||||
|
||||
## API接口响应格式
|
||||
|
||||
### 用户签到接口 `/user/signin`
|
||||
|
||||
#### 请求参数
|
||||
- Method: `POST`
|
||||
- Headers: `Authorization: Bearer <token>`
|
||||
- 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 <token>`
|
||||
|
||||
#### 成功响应
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"message": "签退成功"
|
||||
}
|
||||
```
|
||||
|
||||
#### 失败响应
|
||||
```json
|
||||
{
|
||||
"success": false,
|
||||
"message": "错误信息"
|
||||
}
|
||||
```
|
||||
|
||||
## 最近更新
|
||||
|
||||
### WebSocket使用方式变更(重要变更)
|
||||
|
@@ -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()));
|
||||
}
|
||||
}
|
||||
|
||||
|
42
src/main/java/com/light/delivery/dto/SignInResponse.java
Normal file
42
src/main/java/com/light/delivery/dto/SignInResponse.java
Normal file
@@ -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;
|
||||
}
|
||||
}
|
26
src/main/java/com/light/delivery/dto/SignOutResponse.java
Normal file
26
src/main/java/com/light/delivery/dto/SignOutResponse.java
Normal file
@@ -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;
|
||||
}
|
||||
}
|
@@ -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());
|
||||
}
|
||||
|
Reference in New Issue
Block a user