|
|
|
@ -6,6 +6,7 @@ import cn.hutool.json.JSONObject;
|
|
|
|
|
import cn.hutool.json.JSONUtil; |
|
|
|
|
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
|
|
|
|
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; |
|
|
|
|
import cn.iocoder.yudao.framework.common.exception.ServiceException; |
|
|
|
|
import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils; |
|
|
|
|
import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; |
|
|
|
|
import cn.iocoder.yudao.framework.common.util.validation.ValidationUtils; |
|
|
|
@ -317,7 +318,10 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
|
|
|
|
@Override |
|
|
|
|
@Transactional |
|
|
|
|
public AdminUserDO miniAppLogin(AuthSocialLoginReqVO reqVO) { |
|
|
|
|
log.info("[miniAppLogin][开始小程序登录,code({}) type({}) userType({})]", |
|
|
|
|
reqVO.getCode(), reqVO.getType(), reqVO.getUserType()); |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
AuthUser authUser = socialClientService.getAuthUser( |
|
|
|
|
SocialTypeEnum.WECHAT_MINI_APP.getType(), |
|
|
|
|
UserTypeEnum.ADMIN.getValue(),// 假设用户类型为会员
|
|
|
|
@ -325,11 +329,25 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
|
|
|
|
reqVO.getState() |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
final SocialUserDO socialUserDO = socialUserService.selectBySocialTypeAndOpenId(SocialTypeEnum.WECHAT_MINI_APP.getType(), authUser.getUuid()); |
|
|
|
|
log.info("[miniAppLogin][获取到用户信息,openId({}) nickname({})]", |
|
|
|
|
authUser.getUuid(), authUser.getNickname()); |
|
|
|
|
|
|
|
|
|
final SocialUserDO socialUserDO = socialUserService.selectBySocialTypeAndOpenId( |
|
|
|
|
SocialTypeEnum.WECHAT_MINI_APP.getType(), authUser.getUuid()); |
|
|
|
|
|
|
|
|
|
AdminUserDO user = null; |
|
|
|
|
|
|
|
|
|
if (socialUserDO == null ) { |
|
|
|
|
log.info("[miniAppLogin][新用户注册,openId({})]", authUser.getUuid()); |
|
|
|
|
|
|
|
|
|
// String userInfoUrl = "https://api.weixin.qq.com/sns/userinfo" +
|
|
|
|
|
// "?access_token=" + authUser.getToken() +
|
|
|
|
|
// "&openid=" + authUser.getUuid() +
|
|
|
|
|
// "&lang=zh_CN";
|
|
|
|
|
// String userInfoResponse = HttpUtil.get(userInfoUrl);
|
|
|
|
|
// JSONObject userInfoJson = JSONUtil.parseObj(userInfoResponse);
|
|
|
|
|
// String nickname = userInfoJson.getStr("nickname");
|
|
|
|
|
|
|
|
|
|
// 未找到社交用户,插入记录
|
|
|
|
|
SocialUserDO socialUser = new SocialUserDO(); |
|
|
|
|
socialUser.setType(reqVO.getType()); |
|
|
|
@ -343,6 +361,7 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
|
|
|
|
final int insert = socialUserMapper.insert(socialUser); |
|
|
|
|
|
|
|
|
|
if (insert < 0) { |
|
|
|
|
log.error("[miniAppLogin][插入社交用户失败,openId({})]", authUser.getUuid()); |
|
|
|
|
throw exception(INSERT_ERROR); |
|
|
|
|
} |
|
|
|
|
user = userService.autoRegisterUser(authUser, reqVO.getUserType()); |
|
|
|
@ -354,20 +373,40 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
|
|
|
|
userRoleMapper.insert(userRoleDO); |
|
|
|
|
|
|
|
|
|
user.setIsBand(false); |
|
|
|
|
log.info("[miniAppLogin][新用户注册成功,userId({}) username({})]", user.getId(), user.getUsername()); |
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
log.info("[miniAppLogin][已有用户登录,socialUserId({})]", socialUserDO.getId()); |
|
|
|
|
|
|
|
|
|
SocialUserBindDO bind = socialUserBindMapper.selectByUserTypeAndSocialUserId( |
|
|
|
|
UserTypeEnum.MEMBER.getValue(), socialUserDO.getId()); |
|
|
|
|
|
|
|
|
|
if (bind == null) { |
|
|
|
|
log.error("[miniAppLogin][未找到用户绑定信息,socialUserId({})]", socialUserDO.getId()); |
|
|
|
|
throw exception(SOCIAL_USER_NOT_FOUND); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
SocialUserBindDO bind = socialUserBindMapper.selectByUserTypeAndSocialUserId(UserTypeEnum.MEMBER.getValue(), socialUserDO.getId()); |
|
|
|
|
// // 获得用户
|
|
|
|
|
user = userService.getUser(bind.getUserId()); |
|
|
|
|
|
|
|
|
|
if (user == null) { |
|
|
|
|
log.error("[miniAppLogin][用户不存在,userId({})]", bind.getUserId()); |
|
|
|
|
throw exception(USER_NOT_EXISTS); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
user.setIsBand(true); |
|
|
|
|
log.info("[miniAppLogin][用户登录成功,userId({}) username({})]", user.getId(), user.getUsername()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return user; |
|
|
|
|
|
|
|
|
|
} catch (ServiceException e) { |
|
|
|
|
log.error("[miniAppLogin][小程序登录失败,code({}) error({})]", reqVO.getCode(), e.getMessage()); |
|
|
|
|
throw e; |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
log.error("[miniAppLogin][小程序登录异常,code({})]", reqVO.getCode(), e); |
|
|
|
|
throw exception(SOCIAL_USER_AUTH_FAILURE, e.getMessage()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|