123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- package com.caimei.service.impl;
- import com.caimei.components.RedisService;
- import com.caimei.mapper.AuthMapper;
- import com.caimei.mapper.ClubMapper;
- import com.caimei.mapper.LoginMapper;
- import com.caimei.mapper.ShopMapper;
- import com.caimei.model.ResponseJson;
- import com.caimei.model.po.ClubUserPo;
- import com.caimei.model.po.CmBrandAuthPo;
- import com.caimei.model.vo.WxClubUserVo;
- import com.caimei.service.LoginService;
- import com.caimei.utils.CodeUtil;
- import com.caimei.utils.SmsUtils;
- import com.caimei.utils.WxUtils;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.stereotype.Service;
- import javax.annotation.Resource;
- import java.util.Date;
- import java.util.Map;
- /**
- * Description
- *
- * @author : Aslee
- * @date : 2021/7/15
- */
- @Slf4j
- @Service
- public class LoginServiceImpl implements LoginService {
- @Resource
- private RedisService redisService;
- @Resource
- private LoginMapper loginMapper;
- @Resource
- private ShopMapper shopMapper;
- @Resource
- private ClubMapper clubMapper;
- @Resource
- private AuthMapper authMapper;
- @Override
- public ResponseJson<WxClubUserVo> loginByAuthorization(String code, String appId) {
- if (StringUtils.isEmpty(code)) {
- return ResponseJson.error("参数异常,请输入code", null);
- }
- if (StringUtils.isEmpty(code)) {
- return ResponseJson.error("参数异常,请输入appId", null);
- }
- // 获取供应商公众号appId对应的appSecret
- String appSecret = shopMapper.getAppSecretByAppId(appId);
- if (StringUtils.isEmpty(appSecret)) {
- return ResponseJson.error("参数异常,该公众号不存在", null);
- }
- Map<String,String> infoMap;
- try {
- infoMap = WxUtils.getAccessToken(code, appId, appSecret);
- } catch (Exception e) {
- log.info("微信服务器异常", e);
- return ResponseJson.error(-1, "微信服务器异常", null);
- }
- if ("-1".equals(infoMap.get("code"))) {
- return ResponseJson.error(infoMap.get("errmsg"), null);
- }
- //解析相应内容(转换成json对象)
- String openId = infoMap.get("openId");
- String accessToken = infoMap.get("accessToken");
- log.info("微信授权,openId:>>>>>>>>>>>" + openId);
- WxClubUserVo clubUser = loginMapper.findClubUser(openId);
- if (null == clubUser) {
- clubUser = new WxClubUserVo();
- clubUser.setOpenId(openId);
- clubUser.setAccessToken(accessToken);
- return ResponseJson.error(-2, "未绑定微信", clubUser);
- }
- clubUser.setAccessToken(accessToken);
- // 登录成功redis保存token
- redisService.set(accessToken, openId, 60L * 60 * 4);
- return ResponseJson.success(clubUser);
- }
- @Override
- public ResponseJson sendVerifyCode(String mobile, String appId) {
- if (StringUtils.isEmpty(mobile)) {
- return ResponseJson.error("参数异常,请输入手机号");
- }
- if (StringUtils.isEmpty(appId)) {
- return ResponseJson.error("参数异常,请输入appId");
- }
- Integer clubUserId = clubMapper.checkMobile(mobile, appId);
- if (null != clubUserId) {
- return ResponseJson.error("该手机号已使用,请重新输入");
- }
- // 获取供应商名称
- String shopName = shopMapper.getShopNameByAppId(appId);
- if (StringUtils.isEmpty(shopName)) {
- return ResponseJson.error("供应商不存在");
- } else {
- if (shopName.contains("上海品辉")) {
- shopName = "上海品辉";
- }
- }
- String verifyCode = CodeUtil.generateCodeInt(6);
- String content = "欢迎登录" + shopName + "会员,您的短信验证码为:" + verifyCode + ",该验证码 5 分钟内有效,请勿泄漏于他人。";
- Boolean sendSms = SmsUtils.sendSms(11, mobile, content);
- if (!sendSms) {
- log.info("会员登录验证码发送失败,手机号:" + mobile);
- return ResponseJson.error("发送失败,请确认手机号无误");
- } else {
- log.info("会员登录验证码发送成功,手机号:" + mobile + ",验证码:" + verifyCode);
- }
- redisService.set("code:" + mobile, verifyCode, 300L);
- return ResponseJson.success("发送成功");
- }
- @Override
- public ResponseJson<WxClubUserVo> loginByInvitationCode(String mobile, String verifyCode, String invitationCode, String accessToken, String openId, String appId) throws Exception {
- if (StringUtils.isEmpty(mobile)) {
- return ResponseJson.error("参数异常,请输入手机号", null);
- }
- if (StringUtils.isEmpty(verifyCode)) {
- return ResponseJson.error("参数异常,请输入验证码", null);
- }
- if (StringUtils.isEmpty(invitationCode)) {
- return ResponseJson.error("参数异常,请输入邀请码", null);
- }
- if (StringUtils.isEmpty(accessToken)) {
- return ResponseJson.error("参数异常,请输入accessToken", null);
- }
- if (StringUtils.isEmpty(openId)) {
- return ResponseJson.error("参数异常,请输入openId", null);
- }
- if (StringUtils.isEmpty(appId)) {
- return ResponseJson.error("参数异常,请输入appId", null);
- }
- // 校验验证码是否正确
- String redisVerifyCode = null == redisService.get("code:" + mobile) ? null : redisService.get("code:" + mobile).toString();
- if (!verifyCode.equals(redisVerifyCode)) {
- return ResponseJson.error("验证码错误,请重新输入", null);
- }
- // 校验邀请码是否有效
- ClubUserPo clubUserPo = loginMapper.findByInvitationCode(invitationCode);
- if (null == clubUserPo || 2 == clubUserPo.getStatus() || clubUserPo.getInvitationCodeTime().compareTo(new Date()) < 0) {
- return ResponseJson.error("邀请码已失效", null);
- }
- // 校验机构和供应商的状态是否为已上线
- CmBrandAuthPo clubPo = authMapper.getAuthById(clubUserPo.getAuthId());
- Integer shopStatus = shopMapper.getShopStatus(clubPo.getAuthUserId());
- if (1 != clubPo.getStatus() || 1 != shopStatus) {
- return ResponseJson.error("登录失败", null);
- }
- // 校验appId是否和机构所属供应商的appId相同
- String dbAppId = shopMapper.getAppId(clubPo.getAuthUserId());
- if (StringUtils.isEmpty(dbAppId) || !appId.equals(dbAppId)) {
- return ResponseJson.error("登录失败", null);
- }
- Map<String, String> userInfo = WxUtils.getUserInfo(accessToken, openId);
- String nickName = userInfo.get("nickName");
- loginMapper.bindClubUser(mobile, nickName, openId, invitationCode);
- WxClubUserVo clubUser = new WxClubUserVo();
- clubUser.setClubUserId(clubUserPo.getId());
- clubUser.setAuthId(clubUserPo.getAuthId());
- clubUser.setAuthUserId(clubPo.getAuthUserId());
- clubUser.setMobile(mobile);
- clubUser.setOpenId(openId);
- clubUser.setAccessToken(accessToken);
- // 登录成功redis保存token
- redisService.set(accessToken, openId, 60L * 60 * 4);
- return ResponseJson.success(clubUser);
- }
- }
|