实现解绑微信id
All checks were successful
构建并部署 Spring Boot 应用 / build-and-deploy (push) Successful in 27m41s
All checks were successful
构建并部署 Spring Boot 应用 / build-and-deploy (push) Successful in 27m41s
This commit is contained in:
@@ -290,4 +290,34 @@ public class UserController {
|
|||||||
return ResponseEntity.status(500).build();
|
return ResponseEntity.status(500).build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解绑微信接口
|
||||||
|
* 清除当前用户的openid绑定,允许重新注册其他账号
|
||||||
|
* @param request HTTP请求对象,用于提取认证令牌
|
||||||
|
* @return 解绑结果
|
||||||
|
*/
|
||||||
|
@PostMapping("/unbind")
|
||||||
|
public ResponseEntity<Map<String, Object>> unbindWechat(HttpServletRequest request) {
|
||||||
|
String token = extractToken(request);
|
||||||
|
if (token == null) {
|
||||||
|
Map<String, Object> response = new HashMap<>();
|
||||||
|
response.put("success", false);
|
||||||
|
response.put("message", "Authorization token is missing");
|
||||||
|
return ResponseEntity.badRequest().body(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
userService.unbindWechat(token);
|
||||||
|
Map<String, Object> response = new HashMap<>();
|
||||||
|
response.put("success", true);
|
||||||
|
response.put("message", "微信解绑成功");
|
||||||
|
return ResponseEntity.ok(response);
|
||||||
|
} catch (Exception e) {
|
||||||
|
Map<String, Object> response = new HashMap<>();
|
||||||
|
response.put("success", false);
|
||||||
|
response.put("message", "解绑失败: " + e.getMessage());
|
||||||
|
return ResponseEntity.badRequest().body(response);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@@ -1,47 +1,78 @@
|
|||||||
package com.light.delivery.service;
|
package com.light.delivery.service;
|
||||||
|
|
||||||
|
import com.light.delivery.dto.UserInfoResponse;
|
||||||
import com.light.delivery.model.LoginResponse;
|
import com.light.delivery.model.LoginResponse;
|
||||||
import com.light.delivery.model.User;
|
import com.light.delivery.model.User;
|
||||||
import com.light.delivery.model.WxLoginRequest;
|
import com.light.delivery.model.WxLoginRequest;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户服务接口,定义用户相关的业务操作。
|
* 用户服务接口,定义用户相关业务逻辑的方法签名。
|
||||||
|
* 包括登录、注册、信息获取、签到等核心功能接口。
|
||||||
*/
|
*/
|
||||||
public interface UserService {
|
public interface UserService {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取用户信息。
|
* 根据JWT令牌获取用户信息。
|
||||||
* @param token 用户 token
|
* @param token 用户的JWT令牌
|
||||||
* @return 用户信息
|
* @return 用户实体对象
|
||||||
*/
|
*/
|
||||||
User getUserInfo(String token);
|
User getUserInfo(String token);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户登出。
|
* 根据用户ID获取用户信息。
|
||||||
* @param token 用户 token
|
* @param userId 用户ID
|
||||||
|
* @return 用户实体对象
|
||||||
|
*/
|
||||||
|
User getUserInfoById(Long userId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户登出逻辑。
|
||||||
|
* @param token 用户的JWT令牌
|
||||||
*/
|
*/
|
||||||
void logout(String token);
|
void logout(String token);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信小程序登录,接收code,返回登录响应。
|
* 处理微信登录逻辑。
|
||||||
* @param code 微信登录凭证
|
* @param code 微信授权登录返回的临时凭证
|
||||||
* @return 登录响应
|
* @return 登录响应对象
|
||||||
*/
|
*/
|
||||||
LoginResponse wxLogin(String code);
|
LoginResponse wxLogin(String code);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取当前已登录用户(从上下文或模拟实现中)
|
* 用户签到功能。
|
||||||
* @return 当前用户
|
* @param userId 用户唯一标识
|
||||||
*/
|
* @return 更新后的用户对象
|
||||||
User getCurrentUser();
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 用户签到功能
|
|
||||||
* @param userId 用户ID
|
|
||||||
* @return 签到结果
|
|
||||||
*/
|
*/
|
||||||
User signIn(Long userId);
|
User signIn(Long userId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户签退功能。
|
||||||
|
* @param userId 用户唯一标识
|
||||||
|
*/
|
||||||
|
void signOut(Long userId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户注册成为配送员。
|
||||||
|
* @param userId 用户系统内的唯一标识
|
||||||
|
* @param name 用户真实姓名
|
||||||
|
* @param phone 用户手机号码
|
||||||
|
* @return 更新后的用户对象
|
||||||
|
*/
|
||||||
|
User registerAsEmployee(Long userId, String name, String phone);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理微信登录请求对象。
|
||||||
|
* @param wxLoginRequest 微信登录请求对象
|
||||||
|
* @return 登录响应对象
|
||||||
|
*/
|
||||||
|
LoginResponse wxLogin(WxLoginRequest wxLoginRequest);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前登录用户。
|
||||||
|
* @return 当前用户对象
|
||||||
|
*/
|
||||||
|
User getCurrentUser();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查用户是否已签到
|
* 检查用户是否已签到
|
||||||
* @param userId 用户ID
|
* @param userId 用户ID
|
||||||
@@ -50,17 +81,8 @@ public interface UserService {
|
|||||||
boolean isUserSignedIn(Long userId);
|
boolean isUserSignedIn(Long userId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户签退功能
|
* 解绑微信功能
|
||||||
* @param userId 用户ID
|
* @param token 用户的JWT令牌
|
||||||
*/
|
*/
|
||||||
void signOut(Long userId);
|
void unbindWechat(String token);
|
||||||
|
|
||||||
/**
|
|
||||||
* 注册为正式员工
|
|
||||||
* @param userId 用户ID
|
|
||||||
* @param name 姓名
|
|
||||||
* @param phone 手机号
|
|
||||||
* @return 注册结果
|
|
||||||
*/
|
|
||||||
User registerAsEmployee(Long userId, String name, String phone);
|
|
||||||
}
|
}
|
@@ -389,4 +389,62 @@ public class UserServiceImpl implements UserService {
|
|||||||
}
|
}
|
||||||
return UserRole.GUEST;
|
return UserRole.GUEST;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解绑微信功能
|
||||||
|
* 清除当前用户的openid绑定,允许重新注册其他账号
|
||||||
|
* @param token 用户的JWT令牌
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void unbindWechat(String token) {
|
||||||
|
if (token == null || token.isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("Authorization token is missing");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 解析token获取用户openid
|
||||||
|
String openid = jwtUtil.extractUsername(token);
|
||||||
|
if (openid == null || openid.isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("Invalid token");
|
||||||
|
}
|
||||||
|
|
||||||
|
User user = userRepository.findByOpenid(openid);
|
||||||
|
if (user == null) {
|
||||||
|
throw new IllegalArgumentException("用户不存在");
|
||||||
|
}
|
||||||
|
|
||||||
|
Long userId = user.getId();
|
||||||
|
|
||||||
|
// 通知WebSocket处理器清理连接
|
||||||
|
try {
|
||||||
|
LocationWebSocketHandler handler = applicationContext.getBean(LocationWebSocketHandler.class);
|
||||||
|
// 清理用户状态
|
||||||
|
handler.cleanupUserConnection(userId);
|
||||||
|
} catch (Exception e) {
|
||||||
|
// 记录日志但不中断解绑流程
|
||||||
|
System.err.println("清理WebSocket连接时出错: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 清除员工表中的openid
|
||||||
|
if (user.getPhone() != null && !user.getPhone().isEmpty()) {
|
||||||
|
Optional<Employee> employeeOptional = employeeRepository.findByPhone(user.getPhone());
|
||||||
|
if (employeeOptional.isPresent()) {
|
||||||
|
Employee employee = employeeOptional.get();
|
||||||
|
if (openid.equals(employee.getOpenid())) {
|
||||||
|
employee.setOpenid(null);
|
||||||
|
employeeRepository.save(employee);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 清除用户表中的openid
|
||||||
|
user.setOpenid(null);
|
||||||
|
userRepository.save(user);
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
// 记录日志
|
||||||
|
System.err.println("解绑微信时出错: " + e.getMessage());
|
||||||
|
throw new RuntimeException("解绑失败: " + e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user