package com.yanzhen.controller; import com.yanzhen.entity.Student; import com.yanzhen.entity.User; import com.yanzhen.framework.jwt.JWTUtil; import com.yanzhen.service.StudentService; import com.yanzhen.service.UserService; import com.yanzhen.utils.Result; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.Map; //登录管理的控制器 @RestController public class LoginController { //注入实例 //注入用户服务类 @Autowired private UserService userService; //注入学生服务类 @Autowired private StudentService studentService; //登录界面 @PostMapping("/login") public Result login(@RequestBody User user){ //判断登录用户类型 //判断是否是学生 if(user.getType() == 2){ //在登录管理服务类引用login方法 Student entity = studentService.login(user.getUserName(),user.getPassword()); //判断学生信息是否存在 if(entity != null){ String token = JWTUtil.signForStudent(entity); //返回数据存入Map Map map = new HashMap(); map.put(JWTUtil.token,token); //学生信息存入Map map.put("student",entity); //返回存在结果--登陆成功 return Result.ok("登陆成功",map); }else{ //返回不存在结果--登陆失败 return Result.fail("用户名或密码错误"); } }else{ //用户类型是管理员或宿管员 //在登录管理服务类引用login方法 User entity = userService.login(user.getUserName(),user.getPassword()); //判断用户信息是否存在 if(entity != null){ String token = JWTUtil.sign(entity); //返回数据存入Map Map map = new HashMap(); map.put(JWTUtil.token,token); //用户信息存入Map map.put("user",entity); //返回存在结果--登陆成功 return Result.ok("登陆成功",map); }else{ //返回不存在结果--登陆失败 return Result.fail("用户名或密码错误"); } } } }