LoginServiceImpl.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package com.caimei.service.impl;
  2. import com.caimei.components.RedisService;
  3. import com.caimei.mapper.AuthMapper;
  4. import com.caimei.mapper.ClubMapper;
  5. import com.caimei.mapper.LoginMapper;
  6. import com.caimei.mapper.ShopMapper;
  7. import com.caimei.model.ResponseJson;
  8. import com.caimei.model.po.ClubUserPo;
  9. import com.caimei.model.po.CmBrandAuthPo;
  10. import com.caimei.model.vo.WxClubUserVo;
  11. import com.caimei.service.LoginService;
  12. import com.caimei.utils.CodeUtil;
  13. import com.caimei.utils.SmsUtils;
  14. import com.caimei.utils.WxUtils;
  15. import lombok.extern.slf4j.Slf4j;
  16. import org.apache.commons.lang3.StringUtils;
  17. import org.springframework.stereotype.Service;
  18. import javax.annotation.Resource;
  19. import java.util.Date;
  20. import java.util.Map;
  21. /**
  22. * Description
  23. *
  24. * @author : Aslee
  25. * @date : 2021/7/15
  26. */
  27. @Slf4j
  28. @Service
  29. public class LoginServiceImpl implements LoginService {
  30. @Resource
  31. private RedisService redisService;
  32. @Resource
  33. private LoginMapper loginMapper;
  34. @Resource
  35. private ShopMapper shopMapper;
  36. @Resource
  37. private ClubMapper clubMapper;
  38. @Resource
  39. private AuthMapper authMapper;
  40. @Override
  41. public ResponseJson<WxClubUserVo> loginByAuthorization(String code, String appId) {
  42. if (StringUtils.isEmpty(code)) {
  43. return ResponseJson.error("参数异常,请输入code", null);
  44. }
  45. if (StringUtils.isEmpty(code)) {
  46. return ResponseJson.error("参数异常,请输入appId", null);
  47. }
  48. // 获取供应商公众号appId对应的appSecret
  49. String appSecret = shopMapper.getAppSecretByAppId(appId);
  50. if (StringUtils.isEmpty(appSecret)) {
  51. return ResponseJson.error("参数异常,该公众号不存在", null);
  52. }
  53. Map<String,String> infoMap;
  54. try {
  55. infoMap = WxUtils.getAccessToken(code, appId, appSecret);
  56. } catch (Exception e) {
  57. log.info("微信服务器异常", e);
  58. return ResponseJson.error(-1, "微信服务器异常", null);
  59. }
  60. if ("-1".equals(infoMap.get("code"))) {
  61. return ResponseJson.error(infoMap.get("errmsg"), null);
  62. }
  63. //解析相应内容(转换成json对象)
  64. String openId = infoMap.get("openId");
  65. String accessToken = infoMap.get("accessToken");
  66. log.info("微信授权,openId:>>>>>>>>>>>" + openId);
  67. WxClubUserVo clubUser = loginMapper.findClubUser(openId);
  68. if (null == clubUser) {
  69. clubUser = new WxClubUserVo();
  70. clubUser.setOpenId(openId);
  71. clubUser.setAccessToken(accessToken);
  72. return ResponseJson.error(-2, "未绑定微信", clubUser);
  73. }
  74. clubUser.setAccessToken(accessToken);
  75. // 登录成功redis保存token
  76. redisService.set(accessToken, openId, 60L * 60 * 4);
  77. return ResponseJson.success(clubUser);
  78. }
  79. @Override
  80. public ResponseJson sendVerifyCode(String mobile, String appId) {
  81. if (StringUtils.isEmpty(mobile)) {
  82. return ResponseJson.error("参数异常,请输入手机号");
  83. }
  84. if (StringUtils.isEmpty(appId)) {
  85. return ResponseJson.error("参数异常,请输入appId");
  86. }
  87. Integer clubUserId = clubMapper.checkMobile(mobile, appId);
  88. if (null != clubUserId) {
  89. return ResponseJson.error("该手机号已使用,请重新输入");
  90. }
  91. // 获取供应商名称
  92. String shopName = shopMapper.getShopNameByAppId(appId);
  93. if (StringUtils.isEmpty(shopName)) {
  94. return ResponseJson.error("供应商不存在");
  95. } else {
  96. if (shopName.contains("上海品辉")) {
  97. shopName = "上海品辉";
  98. }
  99. }
  100. String verifyCode = CodeUtil.generateCodeInt(6);
  101. String content = "欢迎登录" + shopName + "会员,您的短信验证码为:" + verifyCode + ",该验证码 5 分钟内有效,请勿泄漏于他人。";
  102. Boolean sendSms = SmsUtils.sendSms(11, mobile, content);
  103. if (!sendSms) {
  104. log.info("会员登录验证码发送失败,手机号:" + mobile);
  105. return ResponseJson.error("发送失败,请确认手机号无误");
  106. } else {
  107. log.info("会员登录验证码发送成功,手机号:" + mobile + ",验证码:" + verifyCode);
  108. }
  109. redisService.set("code:" + mobile, verifyCode, 300L);
  110. return ResponseJson.success("发送成功");
  111. }
  112. @Override
  113. public ResponseJson<WxClubUserVo> loginByInvitationCode(String mobile, String verifyCode, String invitationCode, String accessToken, String openId, String appId) throws Exception {
  114. if (StringUtils.isEmpty(mobile)) {
  115. return ResponseJson.error("参数异常,请输入手机号", null);
  116. }
  117. if (StringUtils.isEmpty(verifyCode)) {
  118. return ResponseJson.error("参数异常,请输入验证码", null);
  119. }
  120. if (StringUtils.isEmpty(invitationCode)) {
  121. return ResponseJson.error("参数异常,请输入邀请码", null);
  122. }
  123. if (StringUtils.isEmpty(accessToken)) {
  124. return ResponseJson.error("参数异常,请输入accessToken", null);
  125. }
  126. if (StringUtils.isEmpty(openId)) {
  127. return ResponseJson.error("参数异常,请输入openId", null);
  128. }
  129. if (StringUtils.isEmpty(appId)) {
  130. return ResponseJson.error("参数异常,请输入appId", null);
  131. }
  132. // 校验验证码是否正确
  133. String redisVerifyCode = null == redisService.get("code:" + mobile) ? null : redisService.get("code:" + mobile).toString();
  134. if (!verifyCode.equals(redisVerifyCode)) {
  135. return ResponseJson.error("验证码错误,请重新输入", null);
  136. }
  137. // 校验邀请码是否有效
  138. ClubUserPo clubUserPo = loginMapper.findByInvitationCode(invitationCode);
  139. if (null == clubUserPo || 2 == clubUserPo.getStatus() || clubUserPo.getInvitationCodeTime().compareTo(new Date()) < 0) {
  140. return ResponseJson.error("邀请码已失效", null);
  141. }
  142. // 校验机构和供应商的状态是否为已上线
  143. CmBrandAuthPo clubPo = authMapper.getAuthById(clubUserPo.getAuthId());
  144. Integer shopStatus = shopMapper.getShopStatus(clubPo.getAuthUserId());
  145. if (1 != clubPo.getStatus() || 1 != shopStatus) {
  146. return ResponseJson.error("登录失败", null);
  147. }
  148. // 校验appId是否和机构所属供应商的appId相同
  149. String dbAppId = shopMapper.getAppId(clubPo.getAuthUserId());
  150. if (StringUtils.isEmpty(dbAppId) || !appId.equals(dbAppId)) {
  151. return ResponseJson.error("登录失败", null);
  152. }
  153. Map<String, String> userInfo = WxUtils.getUserInfo(accessToken, openId);
  154. String nickName = userInfo.get("nickName");
  155. loginMapper.bindClubUser(mobile, nickName, openId, invitationCode);
  156. WxClubUserVo clubUser = new WxClubUserVo();
  157. clubUser.setClubUserId(clubUserPo.getId());
  158. clubUser.setAuthId(clubUserPo.getAuthId());
  159. clubUser.setAuthUserId(clubPo.getAuthUserId());
  160. clubUser.setMobile(mobile);
  161. clubUser.setOpenId(openId);
  162. clubUser.setAccessToken(accessToken);
  163. // 登录成功redis保存token
  164. redisService.set(accessToken, openId, 60L * 60 * 4);
  165. return ResponseJson.success(clubUser);
  166. }
  167. }