|
|
|
@ -1,9 +1,11 @@
|
|
|
|
|
package co.yixiang.yshop.module.member.service.auth;
|
|
|
|
|
|
|
|
|
|
import cn.binarywang.wx.miniapp.api.WxMaService;
|
|
|
|
|
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
|
|
|
|
|
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
|
|
|
|
|
import cn.hutool.core.lang.Assert;
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
|
import co.yixiang.yshop.framework.common.enums.CommonStatusEnum;
|
|
|
|
|
import co.yixiang.yshop.framework.common.enums.UserTypeEnum;
|
|
|
|
|
import co.yixiang.yshop.framework.common.util.monitor.TracerUtils;
|
|
|
|
@ -29,6 +31,7 @@ import co.yixiang.yshop.module.system.enums.sms.SmsSceneEnum;
|
|
|
|
|
import co.yixiang.yshop.module.system.enums.social.SocialTypeEnum;
|
|
|
|
|
import com.google.common.annotations.VisibleForTesting;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import me.chanjar.weixin.common.error.WxErrorException;
|
|
|
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
@ -39,6 +42,7 @@ import java.util.Objects;
|
|
|
|
|
import static co.yixiang.yshop.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
|
|
import static co.yixiang.yshop.framework.common.util.servlet.ServletUtils.getClientIP;
|
|
|
|
|
import static co.yixiang.yshop.module.member.enums.ErrorCodeConstants.*;
|
|
|
|
|
import static co.yixiang.yshop.module.member.enums.ErrorCodeConstants.MINI_AUTH_LOGIN_BAD;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 会员的认证 Service 接口
|
|
|
|
@ -130,19 +134,34 @@ public class MemberAuthServiceImpl implements MemberAuthService {
|
|
|
|
|
try {
|
|
|
|
|
phoneNumberInfo = wxMaService.getUserService().getNewPhoneNoInfo(reqVO.getPhoneCode());
|
|
|
|
|
} catch (Exception exception) {
|
|
|
|
|
log.error(exception.getMessage());
|
|
|
|
|
throw exception(AUTH_WEIXIN_MINI_APP_PHONE_CODE_ERROR);
|
|
|
|
|
}
|
|
|
|
|
// 获得获得注册用户
|
|
|
|
|
MemberUserDO user = userService.createUserIfAbsent(phoneNumberInfo.getPurePhoneNumber(), getClientIP(),
|
|
|
|
|
LoginTypeEnum.WXAPP.getValue());
|
|
|
|
|
Assert.notNull(user, "获取用户失败,结果为空");
|
|
|
|
|
// 用户已经存在
|
|
|
|
|
MemberUserDO memberUserDO = userMapper.selectByMobile(phoneNumberInfo.getPhoneNumber());
|
|
|
|
|
if (memberUserDO == null || StrUtil.isBlank(memberUserDO.getRoutineOpenid())) {
|
|
|
|
|
|
|
|
|
|
// 绑定社交用户
|
|
|
|
|
socialUserApi.bindSocialUser(new SocialUserBindReqDTO(user.getId(), getUserType().getValue(),
|
|
|
|
|
SocialTypeEnum.WECHAT_MINI_APP.getType(), reqVO.getLoginCode(), ""));
|
|
|
|
|
WxMaJscode2SessionResult session = null;
|
|
|
|
|
try {
|
|
|
|
|
session = wxMaService.getUserService().getSessionInfo(reqVO.getLoginCode());
|
|
|
|
|
} catch (WxErrorException e) {
|
|
|
|
|
log.error(e.getMessage());
|
|
|
|
|
throw exception(MINI_AUTH_LOGIN_BAD);
|
|
|
|
|
}
|
|
|
|
|
if(memberUserDO == null){
|
|
|
|
|
// 获得获得注册用户
|
|
|
|
|
memberUserDO = userService.createUserIfAbsent(phoneNumberInfo.getPhoneNumber(), getClientIP(),
|
|
|
|
|
LoginTypeEnum.WXAPP.getValue());
|
|
|
|
|
memberUserDO.setNickname("yshop用户_" + memberUserDO.getId());
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
memberUserDO.setRoutineOpenid(session.getOpenid());
|
|
|
|
|
userMapper.updateById(memberUserDO);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建 Token 令牌,记录登录日志
|
|
|
|
|
return createTokenAfterLoginSuccess(user, user.getMobile(), LoginLogTypeEnum.LOGIN_SOCIAL);
|
|
|
|
|
return createTokenAfterLoginSuccess(memberUserDO, memberUserDO.getMobile(), LoginLogTypeEnum.LOGIN_SOCIAL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private AppAuthLoginRespVO createTokenAfterLoginSuccess(MemberUserDO user, String mobile, LoginLogTypeEnum logType) {
|
|
|
|
|