SysLoginController.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package com.warewms.controller;
  2. import com.warewms.common.utils.R;
  3. import com.warewms.enums.OrderStatusEnum;
  4. import com.warewms.model.SysUser;
  5. import com.warewms.service.*;
  6. import com.warewms.form.SysLoginForm;
  7. import org.apache.commons.io.IOUtils;
  8. import org.apache.shiro.crypto.hash.Sha256Hash;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.web.bind.annotation.GetMapping;
  11. import org.springframework.web.bind.annotation.PostMapping;
  12. import org.springframework.web.bind.annotation.RequestBody;
  13. import org.springframework.web.bind.annotation.RestController;
  14. import javax.imageio.ImageIO;
  15. import javax.servlet.ServletException;
  16. import javax.servlet.ServletOutputStream;
  17. import javax.servlet.http.HttpServletResponse;
  18. import java.awt.image.BufferedImage;
  19. import java.io.IOException;
  20. import java.math.BigDecimal;
  21. import java.util.List;
  22. import java.util.Map;
  23. /**
  24. * 登录相关
  25. *
  26. * @author chenshun
  27. * @email sunlightcs@gmail.com
  28. * @date 2016年11月10日 下午1:15:31
  29. */
  30. @RestController
  31. public class SysLoginController extends AbstractController {
  32. @Autowired
  33. private SysUserService sysUserService;
  34. @Autowired
  35. private SysUserTokenService sysUserTokenService;
  36. @Autowired
  37. private SysCaptchaService sysCaptchaService;
  38. @Autowired
  39. private UserService userService;
  40. /**
  41. * 验证码
  42. */
  43. @GetMapping("captcha.jpg")
  44. public void captcha(HttpServletResponse response, String uuid)throws ServletException, IOException {
  45. response.setHeader("Cache-Control", "no-store, no-cache");
  46. response.setContentType("image/jpeg");
  47. //获取图片验证码
  48. BufferedImage image = sysCaptchaService.getCaptcha(uuid);
  49. ServletOutputStream out = response.getOutputStream();
  50. ImageIO.write(image, "jpg", out);
  51. IOUtils.closeQuietly(out);
  52. }
  53. /**
  54. * 验证码
  55. */
  56. @GetMapping("/home/dash/info")
  57. public void dashInfo(){
  58. Integer totalUsers = userService.getTotalUsers();
  59. Integer newUsers = userService.getYestodayNewUsers();
  60. Integer waitingOrder = 0;
  61. R.ok()
  62. .put("newUsers", newUsers)
  63. .put("totalUsers", totalUsers)
  64. .put("waitingOrder", waitingOrder);
  65. }
  66. /**
  67. * 登录
  68. */
  69. @PostMapping("/sys/login")
  70. public Map<String, Object> login(@RequestBody SysLoginForm form)throws IOException {
  71. boolean captcha = sysCaptchaService.validate(form.getUuid(), form.getCaptcha());
  72. if(!captcha){
  73. return R.error("验证码不正确");
  74. }
  75. //用户信息
  76. SysUser user = sysUserService.queryByUserName(form.getUsername());
  77. //账号不存在、密码错误
  78. if(user == null || !user.getPassword().equals(new Sha256Hash(form.getPassword(), user.getSalt()).toHex())) {
  79. return R.error("账号或密码不正确");
  80. }
  81. //账号锁定
  82. if(user.getStatus() == 0){
  83. return R.error("账号已被锁定,请联系管理员");
  84. }
  85. //生成token,并保存到数据库
  86. R r = sysUserTokenService.createToken(user.getUserId());
  87. return r;
  88. }
  89. /**
  90. * 退出
  91. */
  92. @PostMapping("/sys/logout")
  93. public R logout() {
  94. sysUserTokenService.logout(getUserId());
  95. return R.ok();
  96. }
  97. }