@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
/**
* 员工登录
* @param employee
* @param request
* @return
*/
@PostMapping("/login")
public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request){
//1.页面提交的密码进行MD5加密
String password = employee.getPassword();
password = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
//2.根据提交的用户名usernmae查询数据库
LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Employee::getUsername,employee.getUsername());
Employee emp = employeeService.getOne(queryWrapper);
//3.如果没有查询到返回登录失败结果
if (emp == null){
return R.error("登录失败");
}
//4.密码比对,如果不一致返回登录失败结果
if (!emp.getPassword().equals(password)){
return R.error("密码不对");
}
//5.查看员工状态,如果为已禁用状态,则返回已禁用结果
if (emp.getStatus() == 0){
return R.error("员工被禁用");
}
//6.登录成功,将员工id存入Session并返回登录成功的结果
HttpSession session = request.getSession();
session.setAttribute("employee",emp.getId());
return R.success(emp);
}
}