增加头部名称

This commit is contained in:
zrh050423 2024-12-21 16:27:21 +08:00
parent 9ee7acadd0
commit df2346bfa6
4 changed files with 20 additions and 16 deletions

View File

@ -1,14 +1,11 @@
package edu.zrh.healthsystem.controller; package edu.zrh.healthsystem.controller;
import edu.zrh.healthsystem.model.LoginResult; import edu.zrh.healthsystem.model.LoginResponse;
import edu.zrh.healthsystem.model.UserLogin; import edu.zrh.healthsystem.model.UserLogin;
import edu.zrh.healthsystem.service.LoginService; import edu.zrh.healthsystem.service.LoginService;
import io.jsonwebtoken.Jwts; import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm; import io.jsonwebtoken.SignatureAlgorithm;
import io.swagger.v3.oas.annotations.tags.Tag; 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.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -30,7 +27,7 @@ public class LoginController {
*/ */
@PostMapping(value = "/login") @PostMapping(value = "/login")
@ResponseBody @ResponseBody
public LoginResult login(@RequestBody UserLogin userLogin) { public LoginResponse login(@RequestBody UserLogin userLogin) {
return userService.login(userLogin); return userService.login(userLogin);
} }
@ -76,7 +73,7 @@ public class LoginController {
*/ */
@PostMapping(value = "/register") @PostMapping(value = "/register")
@ResponseBody @ResponseBody
public LoginResult register(@RequestBody UserLogin userLogin) { public LoginResponse register(@RequestBody UserLogin userLogin) {
return userService.register(userLogin); return userService.register(userLogin);
} }

View File

@ -39,7 +39,10 @@ public class UserMainInfo {
@Column(name = "user_email") @Column(name = "user_email")
private String email; private String email;
/**
* 权限
*/
@Column(name = "user_permission")
private String permission;
} }

View File

@ -8,12 +8,13 @@ import lombok.Data;
*/ */
@Data @Data
@AllArgsConstructor @AllArgsConstructor
public class LoginResult { public class LoginResponse {
private String data; private String data;
private Integer code; private Integer code;
private String message; private String message;
private String username; private String username;
public LoginResult() { private String permission;
public LoginResponse() {
} }
} }

View File

@ -2,15 +2,17 @@ package edu.zrh.healthsystem.service;
import edu.zrh.healthsystem.entity.UserLoginInfo; import edu.zrh.healthsystem.entity.UserLoginInfo;
import edu.zrh.healthsystem.entity.UserMainInfo; import edu.zrh.healthsystem.entity.UserMainInfo;
import edu.zrh.healthsystem.model.LoginResult; import edu.zrh.healthsystem.model.LoginResponse;
import edu.zrh.healthsystem.model.UserLogin; import edu.zrh.healthsystem.model.UserLogin;
import edu.zrh.healthsystem.repository.LoginRepository; import edu.zrh.healthsystem.repository.LoginRepository;
import edu.zrh.healthsystem.repository.UserMainInfoRepository; import edu.zrh.healthsystem.repository.UserMainInfoRepository;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import io.jsonwebtoken.Jwts; import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm; import io.jsonwebtoken.SignatureAlgorithm;
/**
* @author han
*/
@Service @Service
public class LoginService { public class LoginService {
@ -27,8 +29,8 @@ public class LoginService {
* @param userLogin 用户登录信息 * @param userLogin 用户登录信息
* @return LoginResult 登录结果 * @return LoginResult 登录结果
*/ */
public LoginResult login(UserLogin userLogin) { public LoginResponse login(UserLogin userLogin) {
LoginResult result = new LoginResult(); LoginResponse result = new LoginResponse();
UserLoginInfo storedUser = loginRepository.findByUsername(userLogin.getUsername()); UserLoginInfo storedUser = loginRepository.findByUsername(userLogin.getUsername());
String userId = String.valueOf(storedUser.getId()); String userId = String.valueOf(storedUser.getId());
UserMainInfo userMainInfo = userMainInfoRepository.findById(Integer.parseInt(userId)).orElse(null); UserMainInfo userMainInfo = userMainInfoRepository.findById(Integer.parseInt(userId)).orElse(null);
@ -39,6 +41,7 @@ public class LoginService {
// 假设 ID UserLogin 的字段 // 假设 ID UserLogin 的字段
if (userMainInfo != null) { if (userMainInfo != null) {
result.setUsername(userMainInfo.getUsername()); result.setUsername(userMainInfo.getUsername());
result.setPermission(userMainInfo.getPermission());
} }
result.setCode(200); result.setCode(200);
result.setMessage("登录成功"); result.setMessage("登录成功");
@ -57,8 +60,8 @@ public class LoginService {
* @param userLogin 用户注册信息 * @param userLogin 用户注册信息
* @return LoginResult 注册结果 * @return LoginResult 注册结果
*/ */
public LoginResult register(UserLogin userLogin) { public LoginResponse register(UserLogin userLogin) {
LoginResult result = new LoginResult(); LoginResponse result = new LoginResponse();
// 假设我们先验证用户是否已经存在 // 假设我们先验证用户是否已经存在
UserLoginInfo existingUser = loginRepository.findByUsername(userLogin.getUsername()); UserLoginInfo existingUser = loginRepository.findByUsername(userLogin.getUsername());