diff --git a/src/main/java/edu/zrh/healthsystem/controller/LoginController.java b/src/main/java/edu/zrh/healthsystem/controller/LoginController.java index ff52e2e..f139cc1 100644 --- a/src/main/java/edu/zrh/healthsystem/controller/LoginController.java +++ b/src/main/java/edu/zrh/healthsystem/controller/LoginController.java @@ -6,6 +6,7 @@ import edu.zrh.healthsystem.service.LoginService; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @@ -29,13 +30,9 @@ public class LoginController { */ @PostMapping(value = "/login") @ResponseBody - public LoginResult login(@RequestBody UserLogin userLogin, HttpSession session) { + public LoginResult login(@RequestBody UserLogin userLogin) { - - LoginResult loginResult = userService.login(userLogin); - - - return loginResult; + return userService.login(userLogin); } /** * 生成 JWT Token diff --git a/src/main/java/edu/zrh/healthsystem/model/LoginResult.java b/src/main/java/edu/zrh/healthsystem/model/LoginResult.java index cf17826..244b58f 100644 --- a/src/main/java/edu/zrh/healthsystem/model/LoginResult.java +++ b/src/main/java/edu/zrh/healthsystem/model/LoginResult.java @@ -12,7 +12,7 @@ public class LoginResult { private String data; private Integer code; private String message; - + private String username; public LoginResult() { } diff --git a/src/main/java/edu/zrh/healthsystem/repository/LoginRepository.java b/src/main/java/edu/zrh/healthsystem/repository/LoginRepository.java index a395fea..64b225c 100644 --- a/src/main/java/edu/zrh/healthsystem/repository/LoginRepository.java +++ b/src/main/java/edu/zrh/healthsystem/repository/LoginRepository.java @@ -7,7 +7,11 @@ import org.springframework.stereotype.Repository; import java.util.List; +/** + * @author han + */ @Repository public interface LoginRepository extends JpaRepository { UserLoginInfo findByUsername(String username); + } diff --git a/src/main/java/edu/zrh/healthsystem/service/LoginService.java b/src/main/java/edu/zrh/healthsystem/service/LoginService.java index eae6701..b529de7 100644 --- a/src/main/java/edu/zrh/healthsystem/service/LoginService.java +++ b/src/main/java/edu/zrh/healthsystem/service/LoginService.java @@ -1,9 +1,12 @@ package edu.zrh.healthsystem.service; import edu.zrh.healthsystem.entity.UserLoginInfo; +import edu.zrh.healthsystem.entity.UserMainInfo; import edu.zrh.healthsystem.model.LoginResult; import edu.zrh.healthsystem.model.UserLogin; import edu.zrh.healthsystem.repository.LoginRepository; +import edu.zrh.healthsystem.repository.UserMainInfoRepository; +import jakarta.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import io.jsonwebtoken.Jwts; @@ -13,9 +16,10 @@ public class LoginService { @Autowired private LoginRepository loginRepository; - - public LoginService(LoginRepository loginRepository) { + private final UserMainInfoRepository userMainInfoRepository; + public LoginService(LoginRepository loginRepository, UserMainInfoRepository userMainInfoRepository) { this.loginRepository = loginRepository; + this.userMainInfoRepository = userMainInfoRepository; } private UserLogin userLogin; /** @@ -26,13 +30,16 @@ public class LoginService { public LoginResult login(UserLogin userLogin) { LoginResult result = new LoginResult(); UserLoginInfo storedUser = loginRepository.findByUsername(userLogin.getUsername()); - - if (storedUser != null && storedUser.getPwd().equals(userLogin.getPwd())) { + String userId = String.valueOf(storedUser.getId()); + UserMainInfo userMainInfo = userMainInfoRepository.findById(Integer.parseInt(userId)).orElse(null); + if (storedUser.getPwd().equals(userLogin.getPwd())) { // 密码正确,生成 JWT Token String token = generateToken(storedUser.getId()); // 假设 ID 是 UserLogin 的字段 - + if (userMainInfo != null) { + result.setUsername(userMainInfo.getUsername()); + } result.setCode(200); result.setMessage("登录成功"); result.setData(token);