package edu.hebeu.controller;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import edu.hebeu.entity.Department;
import edu.hebeu.entity.Employee;
import edu.hebeu.entity.History;
import edu.hebeu.entity.Position;
import edu.hebeu.security.RoleSign;
import edu.hebeu.service.DepartmentService;
import edu.hebeu.service.EmployeeService;
import edu.hebeu.service.HistoryService;
import edu.hebeu.service.PositionService;
import edu.hebeu.util.CaptchaUtil;
import edu.hebeu.util.MTimeUtil;
@Controller
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@Autowired
private DepartmentService departmentService;
@Autowired
private PositionService positionService;
@Autowired
private HistoryService historyService;
@RequestMapping("/login.do")
public String toLogin(){
return "login";
}
@RequestMapping("/checkLogin.do")
public String checkLogin(HttpSession session,HttpServletRequest request, String username,
String password,String captcha, @RequestParam(value="isRememberMe", defaultValue="0") Integer isRememberMe) throws Exception{
String error = null;
System.out.println("username:" + username + "----" + "password:"
+ password+"captcha:"+captcha);
if (username == null || username.isEmpty() || password==null || password.isEmpty())
{
return "login";
}
if (captcha==null ||captcha.isEmpty()){
error = "验证码必须填写";
request.setAttribute("error", error);
return "login";
}
if (!session.getAttribute("randomString").equals(captcha.toUpperCase()))
{
error = "验证码错误";
request.setAttribute("error", error);
return "login";
}
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken(username,
password);
//记住我
if (isRememberMe == 1) {
token.setRememberMe(true);
}
try {
subject.login(token);
} catch (UnknownAccountException e) {
error = "用户名/密码错误";
} catch (IncorrectCredentialsException e) {
error = "用户名/密码错误";
} catch (ExcessiveAttemptsException e) {
// TODO: handle exception
error = "登录失败多次,账户锁定10分钟";
}
if (error != null) {// 出错了,返回登录页面
request.setAttribute("error", error);
return "login";
} else {// 登录成功
Md5Hash md5Hash = new Md5Hash(password);
Employee employee = employeeService.checkLogin(Integer.parseInt(username),md5Hash.toString());
session.setAttribute("loged", employee);
String level = employee.getPosition().getLevel();
if (level.equals("人事部主任")) {
return "admin/index1";
}else if (level.equals("人事部员工")) {
return "admin/index2";
}else if (level.equals("部门主任")) {
return "admin/index3";
}else {
return "admin/index4";
}
}
}
/*
@RequestMapping("/checkLogin1.do")
public String checkLogin1(HttpSession session, Employee employee){
Employee employee2 = employeeService.checkLogin(employee.getEmployeeNumber(),
employee.getPassword());
if (employee2 != null) {
session.setAttribute("loged", employee2);
String level = employee2.getPosition().getLevel();
if (level.equals("人事部主任")) {
return "admin/index1";
}else if (level.equals("人事部员工")) {
return "admin/index2";
}else if (level.equals("部门主任")) {
return "admin/index3";
}else {
return "admin/index4";
}
}else{
return "login";
}
}
*/
@RequestMapping("/welcome.do")
public String toWelcome(){
return "welcome";
}
/*
* 无权限时返回的页面
* */
@RequestMapping("/unauthorized.do")
public String toUnauthorized(){
return "unauthorized";
}
@RequestMapping("/listPage.do")
@RequiresRoles(value = RoleSign.ADMIN)
public String selectList(Model model, int pageNo){
Page<Employee> page = employeeService.selectListByPage(pageNo);
model.addAttribute("page", page);
return "admin/employee_list";
}
@RequestMapping("/{id}/detial.do")
public String selectEmployee(@PathVariable Integer id, Model model){
Employee employee = employeeService.selectEmployee(id);
model.addAttribute("employee", employee);
return "admin/employee_detail";
}
@RequestMapping("/toAdd.do")
@RequiresRoles(value = RoleSign.ADMIN)
public String toAdd(Model model){
List<History> eList = historyService.selectList(new EntityWrapper<History>()
.orderBy("employee_number", false));
model.addAttribute("employeeNumber",eList.get(0).getEmployeeNumber()+1);
List<Department> dList = departmentService.selectList(new EntityWrapper<Department>());
model.addAttribute("dList", dList);
List<Position> pList = positionService.selectList(new EntityWrapper<Position>());
model.addAttribute("pList", pList);
return "admin/employee_add";
}
@RequestMapping("/add.do")
@RequiresRoles(value = RoleSign.ADMIN)
public String add(Employee employee, String date) {
employee.setBirthday(MTimeUtil.stringParse(date));
employeeService.addEmployee(employee);
return "forward:/employee/listPage.do?pageNo=1";
}
@RequestMapping("/{id}/toUpdate.do")
@RequiresRoles(value = RoleSign.ADMIN)
public String toUpdate(Model model, @PathVariable Integer id){
Employee employee = employeeService.selectById(id);
model.addAttribute("employee", employee);
List<Department> dList = departmentService.selectList(new EntityWrapper<Department>());
model.addAttribute("dList", dList);
List<Position> pList = positionService.selectList(new EntityWrapper<Position>());
model.addAttribute("pList", pList);
return "admin/employee_update";
}
@RequestMapping("/{id}/update.do")
@RequiresRoles(value = RoleSign.ADMIN)
public String updateById(@PathVariable Integer id, Employee employee, String date, String status,
HttpSession session){
employee.setId(id);
employee.setBirthday(MTimeUtil.stringParse(date));
//得到操作人员的名字
Employee employee2 = (Employee) session.getAttribute("loged");
employeeService.updateEmployee(employee, status, employee2.getName());
return "forward:/employee/listPage.do?pageNo=1";
}
@RequestMapping("/{id}/delete.do")
@RequiresRoles(value = RoleSign.ADMIN)
public String deleteById(@PathVariable Integer id){
employeeService.deleteEmployee(id);
return "forward

大山源码
- 粉丝: 43
最新资源
- (源码)基于Python Tkinter GUI库的随机选择器.zip
- (源码)基于 PHP 的宝塔服务器状态监控系统.zip
- (源码)基于Arduino的BeeBot机器人控制系统.zip
- (源码)基于Atmel8266MCU的闹钟系统.zip
- 一个flask+jQuery的项目,实现文本相似度查询.作为Python必修课和Python选修课大作业
- (源码)基于Nodered和Arduino的气象站监测系统.zip
- (源码)基于Python和Flutter的智能家居自动化管理系统.zip
- (源码)基于Python的微信聊天机器人.zip
- 北上广成沈五城市PM2.5分析 中国农业大学大数据(二学位)Python程序设计课程作业
- 北京大学暑期学校:Python语言基础及应用(Python Programming and Application)小组作业
- 大三上,编译原理大作业,函数绘图语言解释器,Function Mapping Language Interpreter,Python实现
- Confluence实战指南:提升团队协作效能
- 南开大学《数据库原理》课程大作业,基于mysql和python实现的选课系统
- 多媒体大作业,一个基于 Electron-vue + Python 的图像转动画应用
- Python大作业,KTV点歌系统,支持歌曲增删改查,歌词显示
- 数据库的大作业 因为c++太麻烦了 所以使用Python实现
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


