首页>代码>基于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;
    }
}
最近下载更多
zolscy  LV12 3月24日
a318888331  LV13 3月10日
wanglinddad  LV55 3月7日
罗清晨  LV12 2月28日
打烊1234  LV2 1月31日
taoshen95  LV14 1月26日
亦是此间少年  LV3 1月16日
最近浏览更多
330588 前天
暂无贡献等级
yhsohh123 4月29日
暂无贡献等级
hmf1989 4月26日
暂无贡献等级
Charismatic 4月14日
暂无贡献等级
yhe107  LV3 4月7日
诺一啊啊啊啊啊  LV3 4月7日
fff2003  LV6 4月4日
zolscy  LV12 3月24日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友