|
|
@ -6,6 +6,7 @@ import cn.hutool.json.JSONObject; |
|
|
|
import cn.hutool.json.JSONUtil; |
|
|
|
import cn.hutool.json.JSONUtil; |
|
|
|
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
|
|
|
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
|
|
|
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; |
|
|
|
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.monitor.TracerUtils; |
|
|
|
import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; |
|
|
|
import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; |
|
|
|
import cn.iocoder.yudao.framework.common.util.validation.ValidationUtils; |
|
|
|
import cn.iocoder.yudao.framework.common.util.validation.ValidationUtils; |
|
|
@ -317,57 +318,95 @@ public class AdminAuthServiceImpl implements AdminAuthService { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
@Transactional |
|
|
|
@Transactional |
|
|
|
public AdminUserDO miniAppLogin(AuthSocialLoginReqVO reqVO) { |
|
|
|
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(),// 假设用户类型为会员
|
|
|
|
|
|
|
|
reqVO.getCode(), |
|
|
|
|
|
|
|
reqVO.getState() |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()); |
|
|
|
|
|
|
|
socialUser.setOpenid(authUser.getUuid()); |
|
|
|
|
|
|
|
socialUser.setCode(reqVO.getCode()); |
|
|
|
|
|
|
|
socialUser.setAvatar(authUser.getAvatar()); |
|
|
|
|
|
|
|
socialUser.setRawTokenInfo(authUser.getToken().getRefreshToken() != null? authUser.getToken().getRefreshToken(): "default" ); |
|
|
|
|
|
|
|
socialUser.setNickname(authUser.getNickname()); |
|
|
|
|
|
|
|
socialUser.setState(reqVO.getState()); |
|
|
|
|
|
|
|
socialUser.setRawUserInfo(authUser.getRawUserInfo() != null? authUser.getRawUserInfo().toString(): "default"); |
|
|
|
|
|
|
|
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()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//给用户付一个普通用户角色可以上传图片
|
|
|
|
|
|
|
|
UserRoleDO userRoleDO = new UserRoleDO(); |
|
|
|
|
|
|
|
userRoleDO.setUserId(user.getId()); |
|
|
|
|
|
|
|
userRoleDO.setRoleId(2l); |
|
|
|
|
|
|
|
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); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
AuthUser authUser = socialClientService.getAuthUser( |
|
|
|
// // 获得用户
|
|
|
|
SocialTypeEnum.WECHAT_MINI_APP.getType(), |
|
|
|
user = userService.getUser(bind.getUserId()); |
|
|
|
UserTypeEnum.ADMIN.getValue(),// 假设用户类型为会员
|
|
|
|
|
|
|
|
reqVO.getCode(), |
|
|
|
|
|
|
|
reqVO.getState() |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final SocialUserDO socialUserDO = socialUserService.selectBySocialTypeAndOpenId(SocialTypeEnum.WECHAT_MINI_APP.getType(), authUser.getUuid()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AdminUserDO user = null; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (socialUserDO == null ) { |
|
|
|
|
|
|
|
// 未找到社交用户,插入记录
|
|
|
|
|
|
|
|
SocialUserDO socialUser = new SocialUserDO(); |
|
|
|
|
|
|
|
socialUser.setType(reqVO.getType()); |
|
|
|
|
|
|
|
socialUser.setOpenid(authUser.getUuid()); |
|
|
|
|
|
|
|
socialUser.setCode(reqVO.getCode()); |
|
|
|
|
|
|
|
socialUser.setAvatar(authUser.getAvatar()); |
|
|
|
|
|
|
|
socialUser.setRawTokenInfo(authUser.getToken().getRefreshToken() != null? authUser.getToken().getRefreshToken(): "default" ); |
|
|
|
|
|
|
|
socialUser.setNickname(authUser.getNickname()); |
|
|
|
|
|
|
|
socialUser.setState(reqVO.getState()); |
|
|
|
|
|
|
|
socialUser.setRawUserInfo(authUser.getRawUserInfo() != null? authUser.getRawUserInfo().toString(): "default"); |
|
|
|
|
|
|
|
final int insert = socialUserMapper.insert(socialUser); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (insert < 0) { |
|
|
|
|
|
|
|
throw exception(INSERT_ERROR); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
user = userService.autoRegisterUser(authUser, reqVO.getUserType()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//给用户付一个普通用户角色可以上传图片
|
|
|
|
|
|
|
|
UserRoleDO userRoleDO = new UserRoleDO(); |
|
|
|
|
|
|
|
userRoleDO.setUserId(user.getId()); |
|
|
|
|
|
|
|
userRoleDO.setRoleId(2l); |
|
|
|
|
|
|
|
userRoleMapper.insert(userRoleDO); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
user.setIsBand(false); |
|
|
|
if (user == null) { |
|
|
|
|
|
|
|
log.error("[miniAppLogin][用户不存在,userId({})]", bind.getUserId()); |
|
|
|
|
|
|
|
throw exception(USER_NOT_EXISTS); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
user.setIsBand(true); |
|
|
|
|
|
|
|
log.info("[miniAppLogin][用户登录成功,userId({}) username({})]", user.getId(), user.getUsername()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
SocialUserBindDO bind = socialUserBindMapper.selectByUserTypeAndSocialUserId(UserTypeEnum.MEMBER.getValue(), socialUserDO.getId()); |
|
|
|
return user; |
|
|
|
// // 获得用户
|
|
|
|
|
|
|
|
user = userService.getUser(bind.getUserId()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (user == null) { |
|
|
|
} catch (ServiceException e) { |
|
|
|
throw exception(USER_NOT_EXISTS); |
|
|
|
log.error("[miniAppLogin][小程序登录失败,code({}) error({})]", reqVO.getCode(), e.getMessage()); |
|
|
|
} |
|
|
|
throw e; |
|
|
|
user.setIsBand(true); |
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
log.error("[miniAppLogin][小程序登录异常,code({})]", reqVO.getCode(), e); |
|
|
|
|
|
|
|
throw exception(SOCIAL_USER_AUTH_FAILURE, e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return user; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|