首页>代码>基于SpringBoot+thymeleaf实现的大学生自习室座位预定系统>/seat-subscribe-sys/src/main/java/com/cxs/controller/AuthController.java
package com.cxs.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cxs.dto.LoginDTO;
import com.cxs.model.Admin;
import com.cxs.model.Student;
import com.cxs.service.AdminService;
import com.cxs.service.ClazzService;
import com.cxs.service.StudentService;
import com.cxs.sourceBo.ClazzBo;
import com.cxs.vo.AdminLoginVO;
import com.cxs.vo.StudentLoginVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
/*
* @Project:seat-subscribe-sys
* @Author:cxs
* @Motto:放下杂念,只为迎接明天更好的自己
* */
@Controller
@RequestMapping("/auth")
public class AuthController {
@Autowired
private StudentService studentService;
@Autowired
private AdminService adminService;
@Autowired
private ClazzService clazzService;
@RequestMapping("/login")
public ModelAndView authLogin(ModelAndView mv, HttpServletRequest request){
mv.setViewName("login");
return mv;
}
@RequestMapping("/executeLogin")
public String authExecuteLogin(LoginDTO req, Model mv, HttpServletRequest request){
HttpSession session = request.getSession();
if (!authExecuteLoginCheck(req, mv)) {
return "forward:/auth/login";
}
Integer type = req.getType();
if (type == 1) {
// 管理员
LambdaQueryWrapper<Admin> adminLambdaQueryWrapper = new LambdaQueryWrapper<>();
adminLambdaQueryWrapper.eq(Admin::getUsername, req.getUsername())
.eq(Admin::getPassword, req.getPassword());
Admin admin = adminService.getOne(adminLambdaQueryWrapper);
if (null == admin) {
mv.addAttribute("errorMsg", "用户名或密码错误");
return "forward:/auth/login";
}
AdminLoginVO vo = new AdminLoginVO();
BeanUtils.copyProperties(admin, vo);
vo.setRole("管理员");
session.setAttribute("loginUser", vo);
return "index";
} else {
// 学生
LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
studentLambdaQueryWrapper.eq(Student::getSno, Integer.parseInt(req.getUsername()))
.eq(Student::getPassword, req.getPassword());
Student student = studentService.getOne(studentLambdaQueryWrapper);
if (null == student) {
mv.addAttribute("errorMsg", "用户名或密码错误");
return "forward:/auth/login";
}
StudentLoginVO vo = new StudentLoginVO();
BeanUtils.copyProperties(student, vo);
vo.setRole("学生");
ClazzBo clazzBo = clazzService.getClazzInfoByClazzId(student.getClazzId());
if (null != clazzBo) {
vo.setGradeName(clazzBo.getGradeName());
vo.setClazzName(clazzBo.getClazzName());
}
session.setAttribute("loginUser", vo);
return "index";
}
}
@RequestMapping("/logout")
public ModelAndView logout(ModelAndView mv, HttpSession session){
Object loginUser = session.getAttribute("loginUser");
if (null != loginUser) {
session.invalidate();
}
mv.setViewName("login");
return mv;
}
private boolean authExecuteLoginCheck(LoginDTO req, Model mv) {
Integer type = req.getType();
String username = req.getUsername();
String password = req.getPassword();
if (null == type || !Arrays.asList(1,2).contains(type)) {
mv.addAttribute("errorMsg", "登录类型必传且只能为[1,2]");
return false;
}
if (!StringUtils.hasLength(password)) {
mv.addAttribute("errorMsg", "登录密码岂能为空");
return false;
}
if (type == 1) {
if (!StringUtils.hasLength(username)) {
mv.addAttribute("errorMsg", "管理员用户名岂能为空");
return false;
}
} else {
if (!StringUtils.hasLength(username)) {
mv.addAttribute("errorMsg", "学生学号岂能为空");
return false;
} else {
try {
int i = Integer.parseInt(username);
} catch (NumberFormatException e) {
e.printStackTrace();
mv.addAttribute("errorMsg", "学生学号格式错误");
return false;
}
}
}
return true;
}
}
最近下载更多
wlax99 LV12
2024年11月1日
yayacui LV2
2024年10月28日
lijianMark LV6
2024年9月23日
Peny_ZH LV5
2024年9月22日
ma406805131 LV19
2024年6月15日
thsgli LV9
2024年6月13日
来一杯西瓜冰咩 LV6
2024年5月11日
微信网友_6927932988952576 LV12
2024年3月30日
zolscy LV24
2024年3月24日
a318888331 LV13
2024年3月10日

最近浏览