实现解绑微信id
All checks were successful
构建并部署 Spring Boot 应用 / build-and-deploy (push) Successful in 27m41s

This commit is contained in:
2025-10-21 00:32:22 +08:00
parent 6e89255f1e
commit 1a53ab2f53
3 changed files with 141 additions and 31 deletions

View File

@@ -290,4 +290,34 @@ public class UserController {
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);
}
}
}

View File

@@ -1,47 +1,78 @@
package com.light.delivery.service;
import com.light.delivery.dto.UserInfoResponse;
import com.light.delivery.model.LoginResponse;
import com.light.delivery.model.User;
import com.light.delivery.model.WxLoginRequest;
/**
* 用户服务接口,定义用户相关业务操作
* 用户服务接口,定义用户相关业务逻辑的方法签名
* 包括登录、注册、信息获取、签到等核心功能接口。
*/
public interface UserService {
/**
* 获取用户信息。
* @param token 用户 token
* @return 用户信息
* 根据JWT令牌获取用户信息。
* @param token 用户的JWT令牌
* @return 用户实体对象
*/
User getUserInfo(String token);
/**
* 用户登出
* @param token 用户 token
* 根据用户ID获取用户信息
* @param userId 用户ID
* @return 用户实体对象
*/
User getUserInfoById(Long userId);
/**
* 用户登出逻辑。
* @param token 用户的JWT令牌
*/
void logout(String token);
/**
* 微信小程序登录接收code返回登录响应
* @param code 微信登录凭证
* @return 登录响应
* 处理微信登录逻辑
* @param code 微信授权登录返回的临时凭证
* @return 登录响应对象
*/
LoginResponse wxLogin(String code);
/**
* 获取当前已登录用户(从上下文或模拟实现中)
* @return 当前用户
*/
User getCurrentUser();
/**
* 用户签到功能
* @param userId 用户ID
* @return 签到结果
* 用户签到功能。
* @param userId 用户唯一标识
* @return 更新后的用户对象
*/
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
@@ -50,17 +81,8 @@ public interface UserService {
boolean isUserSignedIn(Long userId);
/**
* 用户签退功能
* @param userId 用户ID
* 解绑微信功能
* @param token 用户的JWT令牌
*/
void signOut(Long userId);
/**
* 注册为正式员工
* @param userId 用户ID
* @param name 姓名
* @param phone 手机号
* @return 注册结果
*/
User registerAsEmployee(Long userId, String name, String phone);
void unbindWechat(String token);
}

View File

@@ -389,4 +389,62 @@ public class UserServiceImpl implements UserService {
}
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);
}
}
}