This commit is contained in:
@@ -251,9 +251,9 @@ public class UserController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 注册为正式员工接口。
|
* 绑定为正式员工接口。
|
||||||
* @param request HTTP请求对象,用于提取认证令牌
|
* @param request HTTP请求对象,用于提取认证令牌
|
||||||
* @param registerRequest 注册请求对象,包含姓名和手机号
|
* @param registerRequest 绑定请求对象,包含姓名和工号
|
||||||
* @return 更新后的用户信息
|
* @return 更新后的用户信息
|
||||||
*/
|
*/
|
||||||
@PostMapping("/register")
|
@PostMapping("/register")
|
||||||
@@ -262,7 +262,7 @@ public class UserController {
|
|||||||
@RequestBody RegisterRequest registerRequest) {
|
@RequestBody RegisterRequest registerRequest) {
|
||||||
try {
|
try {
|
||||||
String token = extractToken(request);
|
String token = extractToken(request);
|
||||||
System.out.println("收到注册请求,Token: " + token);
|
System.out.println("收到绑定请求,Token: " + token);
|
||||||
|
|
||||||
if (token == null || token.isEmpty()) {
|
if (token == null || token.isEmpty()) {
|
||||||
System.err.println("缺少Authorization token");
|
System.err.println("缺少Authorization token");
|
||||||
@@ -280,14 +280,14 @@ public class UserController {
|
|||||||
return ResponseEntity.ok(response);
|
return ResponseEntity.ok(response);
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
// 记录错误日志
|
// 记录错误日志
|
||||||
System.err.println("注册员工时发生错误: " + e.getMessage());
|
System.err.println("绑定员工时发生错误: " + e.getMessage());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return ResponseEntity.badRequest().body(e.getMessage());
|
return ResponseEntity.badRequest().body(e.getMessage());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// 记录未预期的错误
|
// 记录未预期的错误
|
||||||
System.err.println("注册员工时发生未预期错误: " + e.getMessage());
|
System.err.println("绑定员工时发生未预期错误: " + e.getMessage());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return ResponseEntity.status(500).body("注册过程中发生错误: " + e.getMessage());
|
return ResponseEntity.status(500).body("绑定过程中发生错误: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import lombok.Data;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 员工信息实体类,用于存储后台配置的员工数据。
|
* 员工信息实体类,用于存储后台配置的员工数据。
|
||||||
* 当用户注册为配送员或管理员时,系统会根据手机号在此表中查找匹配记录。
|
* 当用户绑定为配送员或管理员时,系统会根据工号在此表中查找匹配记录。
|
||||||
*/
|
*/
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "employee")
|
@Table(name = "employee")
|
||||||
@@ -25,7 +25,7 @@ public class Employee {
|
|||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 员工联系电话
|
* 员工工号
|
||||||
*/
|
*/
|
||||||
@Column(name = "phone", unique = true)
|
@Column(name = "phone", unique = true)
|
||||||
private String phone;
|
private String phone;
|
||||||
@@ -38,7 +38,7 @@ public class Employee {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信用户唯一标识
|
* 微信用户唯一标识
|
||||||
* 当员工注册为系统用户时,会将openid写入此字段
|
* 当员工绑定为系统用户时,会将openid写入此字段
|
||||||
*/
|
*/
|
||||||
@Column(name = "openid", unique = true)
|
@Column(name = "openid", unique = true)
|
||||||
private String openid;
|
private String openid;
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ package com.light.delivery.model;
|
|||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 骑手注册请求数据模型,用于接收用户注册为配送员时提交的信息。
|
* 骑手绑定请求数据模型,用于接收用户绑定为配送员时提交的信息。
|
||||||
* 包含姓名和手机号,用于与员工信息表进行匹配验证。
|
* 包含姓名和工号,用于与员工信息表进行匹配验证。
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
public class RegisterRequest {
|
public class RegisterRequest {
|
||||||
@@ -14,7 +14,7 @@ public class RegisterRequest {
|
|||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户手机号
|
* 用户工号
|
||||||
*/
|
*/
|
||||||
private String phone;
|
private String phone;
|
||||||
}
|
}
|
||||||
@@ -13,8 +13,8 @@ import java.util.Optional;
|
|||||||
@Repository
|
@Repository
|
||||||
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
|
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
|
||||||
/**
|
/**
|
||||||
* 根据手机号查找员工信息
|
* 根据工号查找员工信息
|
||||||
* @param phone 员工手机号
|
* @param phone 员工工号
|
||||||
* @return 员工信息Optional对象
|
* @return 员工信息Optional对象
|
||||||
*/
|
*/
|
||||||
Optional<Employee> findByPhone(String phone);
|
Optional<Employee> findByPhone(String phone);
|
||||||
|
|||||||
@@ -259,18 +259,18 @@ public class UserServiceImpl implements UserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户注册成为配送员。
|
* 用户绑定成为配送员。
|
||||||
* 验证用户提交的个人信息是否与员工数据库中的记录匹配,
|
* 验证用户提交的个人信息是否与员工数据库中的记录匹配,
|
||||||
* 如果验证成功,则更新用户角色为配送员,并建立用户与员工的关联。
|
* 如果验证成功,则更新用户角色为配送员,并建立用户与员工的关联。
|
||||||
* @param userId 用户系统内的唯一标识
|
* @param userId 用户系统内的唯一标识
|
||||||
* @param name 用户真实姓名
|
* @param name 用户真实姓名
|
||||||
* @param phone 用户手机号码
|
* @param phone 用户工号
|
||||||
* @return 更新后的用户对象
|
* @return 更新后的用户对象
|
||||||
* @throws IllegalArgumentException 当用户不存在、手机号未找到或姓名不匹配时抛出异常
|
* @throws IllegalArgumentException 当用户不存在或工号未找到时抛出异常
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public User registerAsEmployee(Long userId, String name, String phone) {
|
public User registerAsEmployee(Long userId, String name, String phone) {
|
||||||
System.out.println("尝试注册员工,用户ID: " + userId);
|
System.out.println("尝试绑定员工,用户ID: " + userId);
|
||||||
|
|
||||||
Optional<User> userOptional = userRepository.findById(userId);
|
Optional<User> userOptional = userRepository.findById(userId);
|
||||||
if (!userOptional.isPresent()) {
|
if (!userOptional.isPresent()) {
|
||||||
@@ -278,16 +278,17 @@ public class UserServiceImpl implements UserService {
|
|||||||
throw new IllegalArgumentException("用户不存在");
|
throw new IllegalArgumentException("用户不存在");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找员工信息表中是否有该手机号
|
// 查找员工信息表中是否有该工号
|
||||||
Optional<Employee> employeeOptional = employeeRepository.findByPhone(phone);
|
Optional<Employee> employeeOptional = employeeRepository.findByPhone(phone);
|
||||||
if (!employeeOptional.isPresent()) {
|
if (!employeeOptional.isPresent()) {
|
||||||
throw new IllegalArgumentException("该手机号不在员工信息表中,请确认手机号是否正确");
|
throw new IllegalArgumentException("该工号不在员工信息表中,请确认工号是否正确,未分属于本公司用户请勿绑定。(绑定不会保留个人信息)");
|
||||||
}
|
}
|
||||||
|
|
||||||
Employee employee = employeeOptional.get();
|
Employee employee = employeeOptional.get();
|
||||||
if (!employee.getName().equals(name)) {
|
// 不再验证姓名,只通过工号绑定
|
||||||
throw new IllegalArgumentException("姓名与员工信息不匹配");
|
// if (!employee.getName().equals(name)) {
|
||||||
}
|
// throw new IllegalArgumentException("姓名与员工信息不匹配");
|
||||||
|
// }
|
||||||
|
|
||||||
// 将openid写入employee表,表示该员工已成为系统用户
|
// 将openid写入employee表,表示该员工已成为系统用户
|
||||||
User user = userOptional.get();
|
User user = userOptional.get();
|
||||||
@@ -296,7 +297,7 @@ public class UserServiceImpl implements UserService {
|
|||||||
|
|
||||||
// 更新用户信息
|
// 更新用户信息
|
||||||
user.setName(name); // 设置用户姓名
|
user.setName(name); // 设置用户姓名
|
||||||
user.setPhone(phone); // 设置用户手机号
|
user.setPhone(phone); // 设置用户工号
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
|
|||||||
Reference in New Issue
Block a user