LoginController.java 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package com.free.controller;
  2. import java.util.Map;
  3. import javax.validation.Valid;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.util.StringUtils;
  6. import org.springframework.web.bind.annotation.PathVariable;
  7. import org.springframework.web.bind.annotation.PostMapping;
  8. import org.springframework.web.bind.annotation.RequestBody;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RestController;
  11. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  12. import com.free.dto.LoginDTO;
  13. import com.free.entity.system.Admin;
  14. import com.free.frame.CustomizationException;
  15. import com.free.frame.ExceptionEnum;
  16. import com.free.frame.ResponseFormat;
  17. import com.free.service.system.AdminService;
  18. import com.free.utils.BcryptUtil;
  19. import com.free.utils.JwtUtil;
  20. import io.swagger.annotations.Api;
  21. import io.swagger.annotations.ApiOperation;
  22. @RestController
  23. @RequestMapping("/login")
  24. @Api(tags = "登录服务")
  25. public class LoginController {
  26. /** 管理员登录类型 */
  27. private String adminLoginType = "Admin";
  28. /** 客服人员登录类型 */
  29. private String csLoginType = "User";
  30. @Autowired
  31. private AdminService adminService;
  32. @ApiOperation("客服服务登录")
  33. @PostMapping("/{type}")
  34. public Object login(@PathVariable String type, @RequestBody @Valid LoginDTO data) {
  35. Map<String, Object> userInfo = null;
  36. if (this.adminLoginType.equals(type)) {
  37. // 管理员service
  38. QueryWrapper<Admin> qw = new QueryWrapper<>();
  39. qw.select("id", "nick_name", "account", "password", "role", "is_use", "is_super");
  40. qw.eq("account", data.getAccount());
  41. userInfo = this.adminService.getMap(qw);
  42. } else if (this.csLoginType.equals(type)) {
  43. // 客服service
  44. }
  45. // 未根据账号信息找到用户数据,抛出异常
  46. if (null == userInfo) {
  47. throw new CustomizationException(ExceptionEnum.ACCOUNT_NOT_FOUND);
  48. }
  49. // 密码验证
  50. String dbPwd = (String) userInfo.get("password");
  51. boolean isSame = BcryptUtil.matchesPassword(data.getPassword(), dbPwd);
  52. // 密码不一致,抛出异常
  53. if (!isSame) {
  54. throw new CustomizationException(ExceptionEnum.PASSWORD_ERROR);
  55. }
  56. userInfo.remove("password");
  57. // 将用户类型也放进去
  58. userInfo.put("type", type);
  59. // jwt加密,返回
  60. String code = JwtUtil.sign(userInfo);
  61. return ResponseFormat.success(code);
  62. }
  63. }