package cn.junhui.springboot.controller;
import cn.junhui.springboot.bean.*;
import cn.junhui.springboot.service.ItemsService;
import cn.junhui.springboot.service.LanguageService;
import cn.junhui.springboot.service.PaperService;
import cn.junhui.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
/**
* 军辉
* 2018-10-15 22:19
*/
@Controller("/user")
public class UserController {
@Autowired
private UserService userService;
@Autowired
private LanguageService languageService;
@Autowired
private PaperService paperService;
@Autowired
private ItemsService itemsService;
private User currentUser = null;
@GetMapping("/")
public ModelAndView index() {
return new ModelAndView("login");
}
@GetMapping("/toRegister")
public ModelAndView toRegisterPage() {
return new ModelAndView("register");
}
@GetMapping("/toLogin")
public ModelAndView toLoginPage() {
System.out.println("start --------------");
return new ModelAndView("login");
}
/*
注册
*/
@PostMapping(value = "/register")
public ModelAndView register(Model model, User user) {
System.out.println("手机号长度:" + user.getPhone().length());
System.out.println("姓名长度:" + user.getName().length());
System.out.println("密码长度:" + user.getPassword().length());
if ((user.getName().length() < 2 || user.getName().length() > 10) ||
(user.getPhone().length() != 11) ||
(user.getPassword().length() < 6 || user.getPassword().length() > 16)
) {
System.out.println("有错");
if ((user.getName().length() < 2 || user.getName().length() > 10)) {
model.addAttribute("nameError", "用户名长度在2到10个字符之间");
}
if (user.getPhone().length() != 11) {
model.addAttribute("phoneError", "请输入11位手机号");
}
if (user.getPassword().length() < 6 || user.getPassword().length() > 16) {
model.addAttribute("passwordError", "密码长度必须是6-16位");
}
return new ModelAndView("register");
} else {
System.out.println("无错");
String md5Password = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
user.setPhoto("fei.png");
user.setPassword(md5Password);
System.out.println("需要注册的信息:" + user);
User u = userService.selectByPhone(user.getPhone());
if (u == null) {
System.out.println("账号不重复,可注册");
userService.toRegister(user);
return new ModelAndView("login");
} else {
System.out.println("已存在此账号,不可注册");
return new ModelAndView("register", "msg", "手机号已存在,请更换手机号,或者联系管理员");
}
}
}
/*
登录
*/
@PostMapping(value = "/login")
public ModelAndView login(Map<String, Object> map,
HttpSession session,
Model model,
User user) {
/*
管理员特定账号
*/
if (user.getPhone().equals("000") && user.getPassword().equals("root")) {
session.setAttribute("loginUser", user);
return new ModelAndView("redirect:/admin");
}
User userdatabase = userService.selectByPhone(user.getPhone());
System.out.println(userdatabase);
if (null == userdatabase) {
//账号不存在
return new ModelAndView("login", "msg", "账号不存在");
} else if (userdatabase.getPassword().equals(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()))) {
System.out.println("数据库加密后的密码:" + userdatabase.getPassword());
System.out.println("登录时输入的密码:" + user.getPassword() + " " + DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
currentUser = userdatabase;
session.setAttribute("loginUser", userdatabase);
List<Language> languages = languageService.getall();
if (currentUser.getExamCode() != null) {
//展示考生所报名考试的信息
Language language = languageService.getLanguage(currentUser.getExamCode());
model.addAttribute("language", language);
}
model.addAttribute("languages", languages);
model.addAttribute("user", userdatabase);
return new ModelAndView("student/student");
} else {
return new ModelAndView("login", "msg", "账号或密码错误");
}
}
@PostMapping("/student")
public ModelAndView User_page() {
return new ModelAndView("student/student");
}
/*
获取所有用户信息
http://localhost:8080/getall
*/
@GetMapping("/getall")
public String getall() {
return userService.findUserList().toString();
}
/*
根据registerid查找用户
http://localhost:8080/user/3
*/
@GetMapping("user/{registerid}")
public User findbyid(@PathVariable("registerid") Integer registerid) {
User u = userService.findUserbyId(registerid);
return u;
}
/*
修改员工普通信息 PUT(底层方法就存在问题)
*/
@PutMapping("/user")
public ModelAndView updateUser(User user, Model model) {
user.setUpdateTime(new Date());
userService.dynamicStyUdp(user);
/*
学生主页需要用的数据
*/
currentUser = user;
List<Language> languages = languageService.getall();
model.addAttribute("languages", languages);
//重定向
//return new ModelAndView("redirect:/login#personal");
return new ModelAndView("student/student");
}
/*
删除员工
http://localhost:8080/user/9
*/
@DeleteMapping("user/{registerid}")
public boolean deleteUser(@PathVariable("registerid") Integer registerid) {
boolean b = userService.deleteUser(registerid);
return b;
}
/*
报名考试
*/
@GetMapping("/signup")
public ModelAndView toSignUp(@RequestParam("lanCode") String lanCode, Model model) {
System.out.println("考试报名处理方法:");
System.out.println("lanColde:" + lanCode + "当前用户:" + currentUser.getName());
currentUser.setExamCode(lanCode);
userService.dynamicStyUdp(currentUser);
Language language = languageService.getLanguage(lanCode);
//展示考生所报名考试的信息
model.addAttribute("language", language);
model.addAttribute("user", currentUser);
return new ModelAndView("student/student");
}
/*
跳转到考试系统页面
*/
@GetMapping("/exam")
public ModelAndView toexam(Model model) {
if (currentUser.getNote() == null) {
//List<Paper> papers = paperService.getall();
List<Paper> papers = new ArrayList<>();
papers = paperService.getallByPaperCode(currentUser.getExamCode());
if (papers.size() == 0) {
//若所选语言类型的试卷张数为0,则从�
好家伙VCC
- 粉丝: 2313
- 资源: 9142
最新资源
- QT天气预报的布局文件
- 取模软件,可用于OLED屏幕取字模
- CFA知识点梳理系列:CFA Level II, Reading 5 Currency Exchange Rates: Understanding Equilibrium Value
- 在线考试与学习交流-JAVA-基于springBoot语言的在线考试与学习交流网页平台(毕业论文)
- 常规应急物资-JAVA-基于SpringBoot+Vue的常规应急物资管理系统(毕业论文+开题)
- 线上辅导班系统-JAVA-基于springboot的线上辅导班系统的开发与设计(毕业论文)
- 学生信息管理(文档+视频+源码).zip
- 社区管理新篇章:系统设计与技术实现
- VBA视频教程 0003
- 医院资源管理-JAVA-基于springboot的医院资源管理系统设计与实现(毕业论文)
- 图书管管理系统(视频+源码).zip
- 水果检测12-YOLO(v5至v11)、COCO、CreateML、Paligemma数据集合集.rar
- 景区民宿预约系统-JAVA-基于springboot框架开发的景区民宿预约系统的设计与实现(毕业论文)
- Java招聘系统:打造企业人才招聘新工具
- 社区智慧养老监护-JAVA-基于springBoot社区智慧养老监护管理平台设计与实现(毕业论文)
- 美食推荐商城-JAVA-基于springBoot美食推荐商城的设计与实现(毕业论文)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈