修改用户角色,添加管理员相关逻辑
Some checks failed
构建并部署 Spring Boot 应用 / build-and-deploy (push) Has been cancelled

This commit is contained in:
2025-10-17 02:15:49 +08:00
parent c83db8d927
commit 1540fe6dab
5 changed files with 100 additions and 43 deletions

View File

@@ -1,13 +1,13 @@
package com.light.delivery.controller;
import com.light.delivery.dto.UserInfoResponse;
import com.light.delivery.dto.UserResponse;
import com.light.delivery.model.LoginResponse;
import com.light.delivery.model.RegisterRequest;
import com.light.delivery.model.User;
import com.light.delivery.model.UserRole;
import com.light.delivery.model.WxLoginRequest;
import com.light.delivery.service.UserService;
import com.light.delivery.service.impl.UserServiceImpl;
import com.light.delivery.util.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
@@ -39,6 +39,12 @@ public class UserController {
*/
@Autowired
private JwtUtil jwtUtil;
/**
* UserServiceImpl依赖注入用于获取用户角色。
*/
@Autowired
private UserServiceImpl userServiceImpl;
/**
* 获取当前用户状态接口。
@@ -60,7 +66,8 @@ public class UserController {
// 根据用户角色和信息完整性判断状态
String status;
if (user.getRole() == null || user.getRole() == UserRole.GUEST) {
UserRole userRole = userServiceImpl.getUserRole(user);
if (userRole == null || userRole == UserRole.GUEST) {
// 未注册用户
status = "unregistered";
} else if ((user.getName() == null || user.getName().isEmpty()) &&
@@ -118,7 +125,9 @@ public class UserController {
dto.setId(user.getId());
dto.setName(user.getName());
dto.setPhone(user.getPhone());
dto.setRole(user.getRole() != null ? user.getRole().getCode() : null);
// 从员工表获取角色信息
UserRole userRole = userServiceImpl.getUserRole(user);
dto.setRole(userRole != null ? userRole.getCode() : null);
dto.setOpenid(user.getOpenid());
return dto;
}
@@ -173,7 +182,6 @@ public class UserController {
}
try {
String username = jwtUtil.getUsernameFromToken(token);
User user = userService.getUserInfo(token);
User updatedUser = userService.signIn(user.getId());
UserInfoResponse response = toUserInfoResponse(updatedUser);

View File

@@ -29,12 +29,6 @@ public class User {
*/
@Column(name = "phone")
private String phone;
/**
* 用户角色(如 ADMIN, DELIVERY_PERSON, GUEST
*/
@Enumerated(EnumType.STRING)
private UserRole role;
/**
* 微信用户唯一标识

View File

@@ -96,6 +96,7 @@ public class UserServiceImpl implements UserService {
if (user == null) {
throw new IllegalArgumentException("用户不存在");
}
return user;
} catch (Exception e) {
// 捕获JWT解析异常等
@@ -127,12 +128,10 @@ public class UserServiceImpl implements UserService {
}
// 如果用户是配送员通知WebSocket处理器清理连接
if (UserRole.DELIVERY_PERSON.equals(user.getRole())) {
// 注意:这里需要根据实际业务逻辑获取配送员ID
// 可能需要通过其他方式关联用户ID和配送员ID
// 这里假设用户ID和配送员ID相同根据项目实际情况调整
// locationWebSocketHandler.removeUserConnection(user.getId());
}
// 注意这里需要根据实际业务逻辑获取配送员ID
// 可能需要通过其他方式关联用户ID和配送员ID
// 这里假设用户ID和配送员ID相同根据项目实际情况调整
// locationWebSocketHandler.removeUserConnection(user.getId());
} catch (Exception e) {
// 记录日志但不中断登出流程
System.err.println("清理WebSocket连接时出错: " + e.getMessage());
@@ -160,24 +159,15 @@ public class UserServiceImpl implements UserService {
if (user == null) {
// 首次登录,创建新用户,默认为游客角色
user = new User();
user.setRole(UserRole.GUEST); // 默认为游客角色
user.setOpenid(openid);
userRepository.save(user);
} else {
// 检查并修复用户角色
if (user.getRole() == null) {
// 如果用户角色为空,设置为游客角色
user.setRole(UserRole.GUEST);
userRepository.save(user);
}
}
// 获取用户角色
UserRole userRole = getUserRole(user);
// 确保用户角色不为空防止在生成token时出现空指针异常
if (user.getRole() == null) {
user.setRole(UserRole.GUEST);
}
String token = jwtUtil.generateToken(user.getOpenid(), user.getRole().getCode());
String token = jwtUtil.generateToken(user.getOpenid(),
userRole != null ? userRole.getCode() : UserRole.GUEST.getCode());
LoginResponse response = new LoginResponse();
response.setToken(token);
response.setUser(toUserInfoResponse(user));
@@ -270,14 +260,13 @@ public class UserServiceImpl implements UserService {
}
// 将openid写入employee表表示该员工已成为系统用户
employee.setOpenid(userOptional.get().getOpenid());
User user = userOptional.get();
employee.setOpenid(user.getOpenid());
employeeRepository.save(employee);
// 更新用户信息
User user = userOptional.get();
user.setName(name); // 设置用户姓名
user.setPhone(phone); // 设置用户手机号
user.setRole(UserRole.fromCode(employee.getRole())); // 设置为员工对应的权限角色
userRepository.save(user);
return user;
@@ -319,7 +308,17 @@ public class UserServiceImpl implements UserService {
dto.setId(user.getId());
dto.setName(user.getName());
dto.setPhone(user.getPhone());
dto.setRole(user.getRole() != null ? user.getRole().getCode() : null);
// 从员工表获取角色信息
if (user.getPhone() != null && !user.getPhone().isEmpty()) {
Optional<Employee> employee = employeeRepository.findByPhone(user.getPhone());
if (employee.isPresent()) {
dto.setRole(employee.get().getRole());
} else {
dto.setRole(UserRole.GUEST.getCode());
}
} else {
dto.setRole(UserRole.GUEST.getCode());
}
dto.setOpenid(user.getOpenid());
return dto;
}
@@ -338,7 +337,9 @@ public class UserServiceImpl implements UserService {
}
User user = userOptional.get();
if (user.getRole() != UserRole.DELIVERY_PERSON) {
UserRole userRole = getUserRole(user);
if (userRole != UserRole.DELIVERY_PERSON) {
// 非配送员角色没有签到状态概念
return false;
}
@@ -346,4 +347,20 @@ public class UserServiceImpl implements UserService {
// 检查WebSocket中的签到状态
return locationWebSocketHandler.isDeliveryPersonSignedIn(userId);
}
/**
* 获取用户角色
* @param user 用户对象
* @return 用户角色
*/
public UserRole getUserRole(User user) {
// 从员工表获取角色信息
if (user.getPhone() != null && !user.getPhone().isEmpty()) {
Optional<Employee> employee = employeeRepository.findByPhone(user.getPhone());
if (employee.isPresent()) {
return UserRole.fromCode(employee.get().getRole());
}
}
return UserRole.GUEST;
}
}

View File

@@ -1,9 +1,8 @@
spring.application.name=Light
server.port=443
server.ssl.key-store-type=JKS
server.ssl.key-store=/etc/ssl/certs/www.doubleyin.cn.jks
server.ssl.key-store-password=${KEY_STORE_PASSWORD}
# 默认禁用SSL通过profile启用
server.ssl.enabled=false
spring.datasource.url=jdbc:mysql://115.190.121.151:3306/light_delivery?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
spring.datasource.username=double