UserServiceImpl.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package com.caimei.service.impl;
  2. import com.caimei.components.RedisService;
  3. import com.caimei.mapper.UserMapper;
  4. import com.caimei.model.ResponseJson;
  5. import com.caimei.model.dto.PasswordDto;
  6. import com.caimei.model.vo.UserLoginVo;
  7. import com.caimei.service.UserService;
  8. import com.caimei.utils.JwtUtil;
  9. import com.caimei.utils.Md5Util;
  10. import lombok.extern.slf4j.Slf4j;
  11. import org.apache.commons.lang3.StringUtils;
  12. import org.springframework.stereotype.Service;
  13. import javax.annotation.Resource;
  14. /**
  15. * Description
  16. *
  17. * @author : Aslee
  18. * @date : 2021/5/11
  19. */
  20. @Slf4j
  21. @Service
  22. public class UserServiceImpl implements UserService {
  23. @Resource
  24. private RedisService redisService;
  25. @Resource
  26. private UserMapper userMapper;
  27. /**
  28. * 登录(用户名/手机号,密码)
  29. * @param mobileOrName 手机号或用户名
  30. * @param password 密码
  31. * @return UserLoginVo
  32. */
  33. @Override
  34. public ResponseJson<UserLoginVo> passwordLogin(String mobileOrName, String password) {
  35. if (StringUtils.isEmpty(mobileOrName)) {
  36. return ResponseJson.error("请填写账户名", null);
  37. }
  38. if (StringUtils.isEmpty(password)) {
  39. return ResponseJson.error("请填写密码", null);
  40. }
  41. // 根据账户名和手机号获取管理员账号和供应商账号
  42. UserLoginVo adminUser = userMapper.getAdminUserByName(mobileOrName);
  43. UserLoginVo shopUser = userMapper.getShopUserByMobile(mobileOrName);
  44. // 密码校验
  45. Boolean passwordVerify;
  46. if (adminUser != null) {
  47. // 管理员登录
  48. // 比对密码
  49. passwordVerify = passwordVerify(password, adminUser.getPassword());
  50. if (passwordVerify) {
  51. return logonVerify(adminUser);
  52. } else if (shopUser != null) {
  53. // 管理员登录失败,尝试供应商登录
  54. passwordVerify = passwordVerify(password, shopUser.getPassword());
  55. if (passwordVerify) {
  56. return logonVerify(shopUser);
  57. }
  58. }
  59. } else if (shopUser != null) {
  60. // 供应商登录
  61. passwordVerify = passwordVerify(password, shopUser.getPassword());
  62. if (passwordVerify) {
  63. return logonVerify(shopUser);
  64. }
  65. }
  66. return ResponseJson.error("输入的密码和账户名不匹配", null);
  67. }
  68. @Override
  69. public ResponseJson updatePassword(PasswordDto passwordDto) {
  70. Integer authUserId = passwordDto.getAuthUserId();
  71. String oldPassword = passwordDto.getOldPassword();
  72. String newPassword = passwordDto.getNewPassword();
  73. String confirmPwd = passwordDto.getConfirmPwd();
  74. // 参数校验
  75. if (authUserId == null) {
  76. return ResponseJson.error("用户id不能为空");
  77. }
  78. if (StringUtils.isEmpty(oldPassword)) {
  79. return ResponseJson.error("请输入旧密码");
  80. }
  81. if (StringUtils.isEmpty(newPassword)) {
  82. return ResponseJson.error("请输入新密码");
  83. }
  84. if (StringUtils.isEmpty(confirmPwd)) {
  85. return ResponseJson.error("请确认密码");
  86. }
  87. if (!newPassword.equals(confirmPwd)) {
  88. return ResponseJson.error("新密码与确认密码不一致");
  89. }
  90. // 验证输入的旧密码与数据库中的密码是否相同
  91. String dbPassword = userMapper.getPasswordByUserId(authUserId);
  92. Boolean passwordVerify = passwordVerify(oldPassword, dbPassword);
  93. if (!passwordVerify) {
  94. return ResponseJson.error("旧密码输入不正确");
  95. }
  96. // 更新密码
  97. newPassword = Md5Util.md5(newPassword);
  98. userMapper.updatePasswordByUserId(authUserId, newPassword);
  99. return ResponseJson.success("密码修改成功");
  100. }
  101. /**
  102. * 登录校验
  103. *
  104. * @param loginUser 用户信息
  105. * @return UserLoginVo
  106. */
  107. private ResponseJson<UserLoginVo> logonVerify(UserLoginVo loginUser) {
  108. // 生成token给用户
  109. String token = JwtUtil.createToken(loginUser.getAuthUserId());
  110. // 为了过期续签,将token存入redis,并设置超时时间
  111. redisService.set(token, token, JwtUtil.getExpireTime());
  112. loginUser.setToken(token);
  113. // 供应商
  114. if (null != loginUser.getShopStatus() && null != loginUser.getUserIdentity() && 2 == loginUser.getUserIdentity()) {
  115. if (0 == loginUser.getShopStatus()) {
  116. return ResponseJson.error(-2, "您的企业账号已被冻结,请联系客服处理", null);
  117. }
  118. }
  119. // 清除密码
  120. loginUser.setPassword(null);
  121. return ResponseJson.success("登录成功", loginUser);
  122. }
  123. /**
  124. * 密码校验
  125. */
  126. private Boolean passwordVerify(String password, String dbPassword) {
  127. String md5Password = Md5Util.md5(password);
  128. return md5Password.equals(dbPassword);
  129. }
  130. }