修改登录逻辑,配合前后端联调
This commit is contained in:
@@ -2,7 +2,10 @@ package com.rj.controller.sys;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.rj.common.PasswordUtil;
|
||||
import com.rj.entity.sys.User;
|
||||
import com.rj.pojo.sys.LoginRequest;
|
||||
import com.rj.pojo.sys.LoginResponse;
|
||||
import com.rj.service.sys.IUserService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
@@ -11,6 +14,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -31,6 +35,28 @@ public class UserController {
|
||||
@Autowired
|
||||
private IUserService userService;
|
||||
|
||||
/**
|
||||
* 用户登录
|
||||
*/
|
||||
@PostMapping("/login")
|
||||
@Operation(summary = "用户登录", description = "根据用户名和密码进行登录验证")
|
||||
public ResponseEntity<Map<String, Object>> login(
|
||||
@Parameter(description = "登录信息", required = true)
|
||||
@Valid @RequestBody LoginRequest loginRequest) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
try {
|
||||
LoginResponse loginResponse = userService.login(loginRequest);
|
||||
result.put("success", true);
|
||||
result.put("message", "登录成功");
|
||||
result.put("data", loginResponse);
|
||||
return ResponseEntity.ok(result);
|
||||
} catch (Exception e) {
|
||||
result.put("success", false);
|
||||
result.put("message", "登录失败:" + e.getMessage());
|
||||
return ResponseEntity.badRequest().body(result);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增用户
|
||||
*/
|
||||
@@ -41,6 +67,23 @@ public class UserController {
|
||||
@RequestBody User user) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
try {
|
||||
// 检查用户名是否已存在
|
||||
User existingUser = userService.getUserByUserName(user.getUserName());
|
||||
if (existingUser != null) {
|
||||
result.put("success", false);
|
||||
result.put("message", "用户名已存在");
|
||||
return ResponseEntity.badRequest().body(result);
|
||||
}
|
||||
|
||||
// 保存原始密码并加密
|
||||
if (user.getPassword() != null && !user.getPassword().trim().isEmpty()) {
|
||||
// 保存原始密码
|
||||
user.setOriginalPassword(user.getPassword());
|
||||
// 对密码进行加密
|
||||
String encryptedPassword = PasswordUtil.encryptPasswordWithDefaultSalt(user.getPassword());
|
||||
user.setPassword(encryptedPassword);
|
||||
}
|
||||
|
||||
boolean success = userService.save(user);
|
||||
if (success) {
|
||||
result.put("success", true);
|
||||
@@ -176,6 +219,23 @@ public class UserController {
|
||||
return ResponseEntity.badRequest().body(result);
|
||||
}
|
||||
|
||||
// 检查用户名是否已被其他用户使用
|
||||
User existingUser = userService.getUserByUserName(user.getUserName());
|
||||
if (existingUser != null && !existingUser.getUserId().equals(user.getUserId())) {
|
||||
result.put("success", false);
|
||||
result.put("message", "用户名已被其他用户使用");
|
||||
return ResponseEntity.badRequest().body(result);
|
||||
}
|
||||
|
||||
// 如果密码不为空,则保存原始密码并加密
|
||||
if (user.getPassword() != null && !user.getPassword().trim().isEmpty()) {
|
||||
// 保存原始密码
|
||||
user.setOriginalPassword(user.getPassword());
|
||||
// 对密码进行加密
|
||||
String encryptedPassword = PasswordUtil.encryptPasswordWithDefaultSalt(user.getPassword());
|
||||
user.setPassword(encryptedPassword);
|
||||
}
|
||||
|
||||
boolean success = userService.updateById(user);
|
||||
|
||||
if (success) {
|
||||
|
||||
Reference in New Issue
Block a user