Commit 638b8b1dd4691ad20cfe1fa7f57baaad718ab8e4
1 parent
14097b41
多模块重构 4:system 模块的调整,完成 social 的复用
Showing
7 changed files
with
114 additions
and
76 deletions
yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/convert/auth/AuthConvert.java
| @@ -26,10 +26,10 @@ public interface AuthConvert { | @@ -26,10 +26,10 @@ public interface AuthConvert { | ||
| 26 | return convert0(bean).setUserType(UserTypeEnum.MEMBER.getValue()); | 26 | return convert0(bean).setUserType(UserTypeEnum.MEMBER.getValue()); |
| 27 | } | 27 | } |
| 28 | 28 | ||
| 29 | - SocialUserBindReqDTO convert(Long userId, Integer value, AppAuthSocialBindReqVO reqVO); | ||
| 30 | - SocialUserBindReqDTO convert(Long userId, Integer value, AppAuthSocialLogin2ReqVO reqVO); | ||
| 31 | - SocialUserBindReqDTO convert(Long userId, Integer value, AppAuthSocialLoginReqVO reqVO); | ||
| 32 | - SocialUserUnbindReqDTO convert(Long userId, Integer value, AppAuthSocialUnbindReqVO reqVO); | 29 | + SocialUserBindReqDTO convert(Long userId, Integer userType, AppAuthSocialBindReqVO reqVO); |
| 30 | + SocialUserBindReqDTO convert(Long userId, Integer userType, AppAuthSocialLogin2ReqVO reqVO); | ||
| 31 | + SocialUserBindReqDTO convert(Long userId, Integer userType, AppAuthSocialLoginReqVO reqVO); | ||
| 32 | + SocialUserUnbindReqDTO convert(Long userId, Integer userType, AppAuthSocialUnbindReqVO reqVO); | ||
| 33 | 33 | ||
| 34 | SmsCodeSendReqDTO convert(AppAuthSendSmsReqVO reqVO); | 34 | SmsCodeSendReqDTO convert(AppAuthSendSmsReqVO reqVO); |
| 35 | SmsCodeUseReqDTO convert(AppAuthResetPasswordReqVO reqVO, SmsSceneEnum scene, String usedIp); | 35 | SmsCodeUseReqDTO convert(AppAuthResetPasswordReqVO reqVO, SmsSceneEnum scene, String usedIp); |
yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthServiceImpl.java
| @@ -152,6 +152,7 @@ public class MemberAuthServiceImpl implements MemberAuthService { | @@ -152,6 +152,7 @@ public class MemberAuthServiceImpl implements MemberAuthService { | ||
| 152 | 152 | ||
| 153 | @Override | 153 | @Override |
| 154 | public void socialBind(Long userId, AppAuthSocialBindReqVO reqVO) { | 154 | public void socialBind(Long userId, AppAuthSocialBindReqVO reqVO) { |
| 155 | + // 绑定社交用户(新增) | ||
| 155 | socialUserApi.bindSocialUser(AuthConvert.INSTANCE.convert(userId, getUserType().getValue(), reqVO)); | 156 | socialUserApi.bindSocialUser(AuthConvert.INSTANCE.convert(userId, getUserType().getValue(), reqVO)); |
| 156 | } | 157 | } |
| 157 | 158 |
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/api/social/SocialUserApiImpl.java
| @@ -3,15 +3,11 @@ package cn.iocoder.yudao.module.system.api.social; | @@ -3,15 +3,11 @@ package cn.iocoder.yudao.module.system.api.social; | ||
| 3 | import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO; | 3 | import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO; |
| 4 | import cn.iocoder.yudao.module.system.api.social.dto.SocialUserUnbindReqDTO; | 4 | import cn.iocoder.yudao.module.system.api.social.dto.SocialUserUnbindReqDTO; |
| 5 | import cn.iocoder.yudao.module.system.service.social.SocialUserService; | 5 | import cn.iocoder.yudao.module.system.service.social.SocialUserService; |
| 6 | -import me.zhyd.oauth.model.AuthUser; | ||
| 7 | import org.springframework.stereotype.Service; | 6 | import org.springframework.stereotype.Service; |
| 8 | import org.springframework.validation.annotation.Validated; | 7 | import org.springframework.validation.annotation.Validated; |
| 9 | 8 | ||
| 10 | import javax.annotation.Resource; | 9 | import javax.annotation.Resource; |
| 11 | 10 | ||
| 12 | -import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | ||
| 13 | -import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SOCIAL_USER_NOT_FOUND; | ||
| 14 | - | ||
| 15 | /** | 11 | /** |
| 16 | * 社交用户的 API 实现类 | 12 | * 社交用户的 API 实现类 |
| 17 | * | 13 | * |
| @@ -31,16 +27,7 @@ public class SocialUserApiImpl implements SocialUserApi { | @@ -31,16 +27,7 @@ public class SocialUserApiImpl implements SocialUserApi { | ||
| 31 | 27 | ||
| 32 | @Override | 28 | @Override |
| 33 | public void bindSocialUser(SocialUserBindReqDTO reqDTO) { | 29 | public void bindSocialUser(SocialUserBindReqDTO reqDTO) { |
| 34 | - // 使用 code 授权 | ||
| 35 | - AuthUser authUser = socialUserService.getAuthUser(reqDTO.getType(), reqDTO.getCode(), | ||
| 36 | - reqDTO.getState()); | ||
| 37 | - if (authUser == null) { | ||
| 38 | - throw exception(SOCIAL_USER_NOT_FOUND); | ||
| 39 | - } | ||
| 40 | - | ||
| 41 | - // 绑定社交用户(新增) | ||
| 42 | - socialUserService.bindSocialUser(reqDTO.getUserId(), reqDTO.getUserType(), | ||
| 43 | - reqDTO.getType(), authUser); | 30 | + socialUserService.bindSocialUser(reqDTO); |
| 44 | } | 31 | } |
| 45 | 32 | ||
| 46 | @Override | 33 | @Override |
| @@ -51,21 +38,12 @@ public class SocialUserApiImpl implements SocialUserApi { | @@ -51,21 +38,12 @@ public class SocialUserApiImpl implements SocialUserApi { | ||
| 51 | 38 | ||
| 52 | @Override | 39 | @Override |
| 53 | public void checkSocialUser(Integer type, String code, String state) { | 40 | public void checkSocialUser(Integer type, String code, String state) { |
| 54 | - AuthUser authUser = socialUserService.getAuthUser(type, code, state); | ||
| 55 | - if (authUser == null) { | ||
| 56 | - throw exception(SOCIAL_USER_NOT_FOUND); | ||
| 57 | - } | 41 | + socialUserService.checkSocialUser(type, code, state); |
| 58 | } | 42 | } |
| 59 | 43 | ||
| 60 | @Override | 44 | @Override |
| 61 | public Long getBindUserId(Integer userType, Integer type, String code, String state) { | 45 | public Long getBindUserId(Integer userType, Integer type, String code, String state) { |
| 62 | - AuthUser authUser = socialUserService.getAuthUser(type, code, state); | ||
| 63 | - if (authUser == null) { | ||
| 64 | - throw exception(SOCIAL_USER_NOT_FOUND); | ||
| 65 | - } | ||
| 66 | - | ||
| 67 | - // | ||
| 68 | - return null; | 46 | + return socialUserService.getBindUserId(userType, type, code, state); |
| 69 | } | 47 | } |
| 70 | 48 | ||
| 71 | } | 49 | } |
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/convert/auth/AuthConvert.java
| 1 | package cn.iocoder.yudao.module.system.convert.auth; | 1 | package cn.iocoder.yudao.module.system.convert.auth; |
| 2 | 2 | ||
| 3 | -import cn.iocoder.yudao.module.system.controller.admin.auth.vo.auth.AuthMenuRespVO; | ||
| 4 | -import cn.iocoder.yudao.module.system.controller.admin.auth.vo.auth.AuthPermissionInfoRespVO; | 3 | +import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; |
| 4 | +import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; | ||
| 5 | +import cn.iocoder.yudao.framework.security.core.LoginUser; | ||
| 6 | +import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO; | ||
| 7 | +import cn.iocoder.yudao.module.system.api.social.dto.SocialUserUnbindReqDTO; | ||
| 8 | +import cn.iocoder.yudao.module.system.controller.admin.auth.vo.auth.*; | ||
| 5 | import cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO; | 9 | import cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO; |
| 6 | import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO; | 10 | import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO; |
| 7 | import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; | 11 | import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; |
| 8 | import cn.iocoder.yudao.module.system.enums.permission.MenuIdEnum; | 12 | import cn.iocoder.yudao.module.system.enums.permission.MenuIdEnum; |
| 9 | -import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; | ||
| 10 | -import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; | ||
| 11 | -import cn.iocoder.yudao.framework.security.core.LoginUser; | ||
| 12 | import org.mapstruct.Mapper; | 13 | import org.mapstruct.Mapper; |
| 13 | import org.mapstruct.Mapping; | 14 | import org.mapstruct.Mapping; |
| 14 | import org.mapstruct.factory.Mappers; | 15 | import org.mapstruct.factory.Mappers; |
| @@ -71,4 +72,9 @@ public interface AuthConvert { | @@ -71,4 +72,9 @@ public interface AuthConvert { | ||
| 71 | return CollectionUtils.filterList(treeNodeMap.values(), node -> MenuIdEnum.ROOT.getId().equals(node.getParentId())); | 72 | return CollectionUtils.filterList(treeNodeMap.values(), node -> MenuIdEnum.ROOT.getId().equals(node.getParentId())); |
| 72 | } | 73 | } |
| 73 | 74 | ||
| 75 | + SocialUserBindReqDTO convert(Long userId, Integer userType, AuthSocialBindReqVO reqVO); | ||
| 76 | + SocialUserBindReqDTO convert(Long userId, Integer userType, AuthSocialLogin2ReqVO reqVO); | ||
| 77 | + SocialUserBindReqDTO convert(Long userId, Integer userType, AuthSocialLoginReqVO reqVO); | ||
| 78 | + SocialUserUnbindReqDTO convert(Long userId, Integer userType, AuthSocialUnbindReqVO reqVO); | ||
| 79 | + | ||
| 74 | } | 80 | } |
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java
| 1 | package cn.iocoder.yudao.module.system.service.auth; | 1 | package cn.iocoder.yudao.module.system.service.auth; |
| 2 | 2 | ||
| 3 | -import cn.hutool.core.collection.CollUtil; | ||
| 4 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | 3 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 5 | import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; | 4 | import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; |
| 6 | import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils; | 5 | import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils; |
| @@ -12,7 +11,6 @@ import cn.iocoder.yudao.module.system.controller.admin.auth.vo.auth.AuthSocialBi | @@ -12,7 +11,6 @@ import cn.iocoder.yudao.module.system.controller.admin.auth.vo.auth.AuthSocialBi | ||
| 12 | import cn.iocoder.yudao.module.system.controller.admin.auth.vo.auth.AuthSocialLogin2ReqVO; | 11 | import cn.iocoder.yudao.module.system.controller.admin.auth.vo.auth.AuthSocialLogin2ReqVO; |
| 13 | import cn.iocoder.yudao.module.system.controller.admin.auth.vo.auth.AuthSocialLoginReqVO; | 12 | import cn.iocoder.yudao.module.system.controller.admin.auth.vo.auth.AuthSocialLoginReqVO; |
| 14 | import cn.iocoder.yudao.module.system.convert.auth.AuthConvert; | 13 | import cn.iocoder.yudao.module.system.convert.auth.AuthConvert; |
| 15 | -import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserDO; | ||
| 16 | import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; | 14 | import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; |
| 17 | import cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum; | 15 | import cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum; |
| 18 | import cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum; | 16 | import cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum; |
| @@ -37,7 +35,6 @@ import org.springframework.stereotype.Service; | @@ -37,7 +35,6 @@ import org.springframework.stereotype.Service; | ||
| 37 | import org.springframework.util.Assert; | 35 | import org.springframework.util.Assert; |
| 38 | 36 | ||
| 39 | import javax.annotation.Resource; | 37 | import javax.annotation.Resource; |
| 40 | -import java.util.List; | ||
| 41 | import java.util.Objects; | 38 | import java.util.Objects; |
| 42 | import java.util.Set; | 39 | import java.util.Set; |
| 43 | 40 | ||
| @@ -70,7 +67,7 @@ public class AdminAuthServiceImpl implements AdminAuthService { | @@ -70,7 +67,7 @@ public class AdminAuthServiceImpl implements AdminAuthService { | ||
| 70 | @Resource | 67 | @Resource |
| 71 | private UserSessionService userSessionService; | 68 | private UserSessionService userSessionService; |
| 72 | @Resource | 69 | @Resource |
| 73 | - private SocialUserService socialService; | 70 | + private SocialUserService socialUserService; |
| 74 | 71 | ||
| 75 | @Override | 72 | @Override |
| 76 | public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { | 73 | public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { |
| @@ -192,19 +189,15 @@ public class AdminAuthServiceImpl implements AdminAuthService { | @@ -192,19 +189,15 @@ public class AdminAuthServiceImpl implements AdminAuthService { | ||
| 192 | 189 | ||
| 193 | @Override | 190 | @Override |
| 194 | public String socialLogin(AuthSocialLoginReqVO reqVO, String userIp, String userAgent) { | 191 | public String socialLogin(AuthSocialLoginReqVO reqVO, String userIp, String userAgent) { |
| 195 | - // 使用 code 授权码,进行登录 | ||
| 196 | - AuthUser authUser = socialService.getAuthUser(reqVO.getType(), reqVO.getCode(), reqVO.getState()); | ||
| 197 | - Assert.notNull(authUser, "授权用户不为空"); | ||
| 198 | - | ||
| 199 | - // 如果未绑定 SocialUserDO 用户,则无法自动登录,进行报错 | ||
| 200 | - String unionId = socialService.getAuthUserUnionId(authUser); | ||
| 201 | - List<SocialUserDO> socialUsers = socialService.getAllSocialUserList(reqVO.getType(), unionId, getUserType().getValue()); | ||
| 202 | - if (CollUtil.isEmpty(socialUsers)) { | 192 | + // 使用 code 授权码,进行登录。然后,获得到绑定的用户编号 |
| 193 | + Long userId = socialUserService.getBindUserId(UserTypeEnum.MEMBER.getValue(), reqVO.getType(), | ||
| 194 | + reqVO.getCode(), reqVO.getState()); | ||
| 195 | + if (userId == null) { | ||
| 203 | throw exception(AUTH_THIRD_LOGIN_NOT_BIND); | 196 | throw exception(AUTH_THIRD_LOGIN_NOT_BIND); |
| 204 | } | 197 | } |
| 205 | 198 | ||
| 206 | // 自动登录 | 199 | // 自动登录 |
| 207 | - AdminUserDO user = userService.getUser(socialUsers.get(0).getUserId()); | 200 | + AdminUserDO user = userService.getUser(userId); |
| 208 | if (user == null) { | 201 | if (user == null) { |
| 209 | throw exception(USER_NOT_EXISTS); | 202 | throw exception(USER_NOT_EXISTS); |
| 210 | } | 203 | } |
| @@ -214,7 +207,7 @@ public class AdminAuthServiceImpl implements AdminAuthService { | @@ -214,7 +207,7 @@ public class AdminAuthServiceImpl implements AdminAuthService { | ||
| 214 | LoginUser loginUser = this.buildLoginUser(user); | 207 | LoginUser loginUser = this.buildLoginUser(user); |
| 215 | 208 | ||
| 216 | // 绑定社交用户(更新) | 209 | // 绑定社交用户(更新) |
| 217 | - socialService.bindSocialUser(loginUser.getId(), getUserType().getValue(), reqVO.getType(), authUser); | 210 | + socialUserService.bindSocialUser(AuthConvert.INSTANCE.convert(loginUser.getId(), getUserType().getValue(), reqVO)); |
| 218 | 211 | ||
| 219 | // 缓存登录用户到 Redis 中,返回 sessionId 编号 | 212 | // 缓存登录用户到 Redis 中,返回 sessionId 编号 |
| 220 | return userSessionService.createUserSession(loginUser, userIp, userAgent); | 213 | return userSessionService.createUserSession(loginUser, userIp, userAgent); |
| @@ -223,14 +216,14 @@ public class AdminAuthServiceImpl implements AdminAuthService { | @@ -223,14 +216,14 @@ public class AdminAuthServiceImpl implements AdminAuthService { | ||
| 223 | @Override | 216 | @Override |
| 224 | public String socialLogin2(AuthSocialLogin2ReqVO reqVO, String userIp, String userAgent) { | 217 | public String socialLogin2(AuthSocialLogin2ReqVO reqVO, String userIp, String userAgent) { |
| 225 | // 使用 code 授权码,进行登录 | 218 | // 使用 code 授权码,进行登录 |
| 226 | - AuthUser authUser = socialService.getAuthUser(reqVO.getType(), reqVO.getCode(), reqVO.getState()); | 219 | + AuthUser authUser = socialUserService.getAuthUser(reqVO.getType(), reqVO.getCode(), reqVO.getState()); |
| 227 | Assert.notNull(authUser, "授权用户不为空"); | 220 | Assert.notNull(authUser, "授权用户不为空"); |
| 228 | 221 | ||
| 229 | // 使用账号密码,进行登录。 | 222 | // 使用账号密码,进行登录。 |
| 230 | LoginUser loginUser = this.login0(reqVO.getUsername(), reqVO.getPassword()); | 223 | LoginUser loginUser = this.login0(reqVO.getUsername(), reqVO.getPassword()); |
| 231 | 224 | ||
| 232 | // 绑定社交用户(新增) | 225 | // 绑定社交用户(新增) |
| 233 | - socialService.bindSocialUser(loginUser.getId(), getUserType().getValue(), reqVO.getType(), authUser); | 226 | + socialUserService.bindSocialUser(AuthConvert.INSTANCE.convert(loginUser.getId(), getUserType().getValue(), reqVO)); |
| 234 | 227 | ||
| 235 | // 缓存登录用户到 Redis 中,返回 sessionId 编号 | 228 | // 缓存登录用户到 Redis 中,返回 sessionId 编号 |
| 236 | return userSessionService.createUserSession(loginUser, userIp, userAgent); | 229 | return userSessionService.createUserSession(loginUser, userIp, userAgent); |
| @@ -238,12 +231,8 @@ public class AdminAuthServiceImpl implements AdminAuthService { | @@ -238,12 +231,8 @@ public class AdminAuthServiceImpl implements AdminAuthService { | ||
| 238 | 231 | ||
| 239 | @Override | 232 | @Override |
| 240 | public void socialBind(Long userId, AuthSocialBindReqVO reqVO) { | 233 | public void socialBind(Long userId, AuthSocialBindReqVO reqVO) { |
| 241 | - // 使用 code 授权码,进行登录 | ||
| 242 | - AuthUser authUser = socialService.getAuthUser(reqVO.getType(), reqVO.getCode(), reqVO.getState()); | ||
| 243 | - Assert.notNull(authUser, "授权用户不为空"); | ||
| 244 | - | ||
| 245 | // 绑定社交用户(新增) | 234 | // 绑定社交用户(新增) |
| 246 | - socialService.bindSocialUser(userId, getUserType().getValue(), reqVO.getType(), authUser); | 235 | + socialUserService.bindSocialUser(AuthConvert.INSTANCE.convert(userId, getUserType().getValue(), reqVO)); |
| 247 | } | 236 | } |
| 248 | 237 | ||
| 249 | @Override | 238 | @Override |
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialUserService.java
| @@ -2,10 +2,12 @@ package cn.iocoder.yudao.module.system.service.social; | @@ -2,10 +2,12 @@ package cn.iocoder.yudao.module.system.service.social; | ||
| 2 | 2 | ||
| 3 | import cn.hutool.core.util.StrUtil; | 3 | import cn.hutool.core.util.StrUtil; |
| 4 | import cn.iocoder.yudao.framework.common.exception.ServiceException; | 4 | import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| 5 | +import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO; | ||
| 5 | import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserDO; | 6 | import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserDO; |
| 6 | import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum; | 7 | import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum; |
| 7 | import me.zhyd.oauth.model.AuthUser; | 8 | import me.zhyd.oauth.model.AuthUser; |
| 8 | 9 | ||
| 10 | +import javax.validation.Valid; | ||
| 9 | import javax.validation.constraints.NotNull; | 11 | import javax.validation.constraints.NotNull; |
| 10 | import java.util.List; | 12 | import java.util.List; |
| 11 | 13 | ||
| @@ -48,17 +50,6 @@ public interface SocialUserService { | @@ -48,17 +50,6 @@ public interface SocialUserService { | ||
| 48 | } | 50 | } |
| 49 | 51 | ||
| 50 | /** | 52 | /** |
| 51 | - * 获得 unionId 对应的某个社交平台的“所有”社交用户 | ||
| 52 | - * 注意,这里的“所有”,指的是类似【微信】平台,包括了小程序、公众号、PC 网站,他们的 unionId 是一致的 | ||
| 53 | - * | ||
| 54 | - * @param type 社交平台的类型 {@link SocialTypeEnum} | ||
| 55 | - * @param unionId 社交平台的 unionId | ||
| 56 | - * @param userType 全局用户类型 | ||
| 57 | - * @return 社交用户列表 | ||
| 58 | - */ | ||
| 59 | - List<SocialUserDO> getAllSocialUserList(Integer type, String unionId, Integer userType); | ||
| 60 | - | ||
| 61 | - /** | ||
| 62 | * 获得指定用户的社交用户列表 | 53 | * 获得指定用户的社交用户列表 |
| 63 | * | 54 | * |
| 64 | * @param userId 用户编号 | 55 | * @param userId 用户编号 |
| @@ -69,12 +60,10 @@ public interface SocialUserService { | @@ -69,12 +60,10 @@ public interface SocialUserService { | ||
| 69 | 60 | ||
| 70 | /** | 61 | /** |
| 71 | * 绑定社交用户 | 62 | * 绑定社交用户 |
| 72 | - * @param userId 用户编号 | ||
| 73 | - * @param userType 用户类型 | ||
| 74 | - * @param type 社交平台的类型 {@link SocialTypeEnum} | ||
| 75 | - * @param authUser 授权用户 | 63 | + * |
| 64 | + * @param reqDTO 绑定信息 | ||
| 76 | */ | 65 | */ |
| 77 | - void bindSocialUser(Long userId, Integer userType, Integer type, AuthUser authUser); | 66 | + void bindSocialUser(@Valid SocialUserBindReqDTO reqDTO); |
| 78 | 67 | ||
| 79 | /** | 68 | /** |
| 80 | * 取消绑定社交用户 | 69 | * 取消绑定社交用户 |
| @@ -86,4 +75,27 @@ public interface SocialUserService { | @@ -86,4 +75,27 @@ public interface SocialUserService { | ||
| 86 | */ | 75 | */ |
| 87 | void unbindSocialUser(Long userId, Integer userType, Integer type, String unionId); | 76 | void unbindSocialUser(Long userId, Integer userType, Integer type, String unionId); |
| 88 | 77 | ||
| 78 | + /** | ||
| 79 | + * 校验社交用户的认证信息是否正确 | ||
| 80 | + * 如果校验不通过,则抛出 {@link ServiceException} 业务异常 | ||
| 81 | + * | ||
| 82 | + * @param type 社交平台的类型 | ||
| 83 | + * @param code 授权码 | ||
| 84 | + * @param state state | ||
| 85 | + */ | ||
| 86 | + void checkSocialUser(Integer type, String code, String state); | ||
| 87 | + | ||
| 88 | + /** | ||
| 89 | + * 获得社交用户的绑定用户编号 | ||
| 90 | + * 注意,返回的是 MemberUser 或者 AdminUser 的 id 编号! | ||
| 91 | + * 该方法会执行和 {@link #checkSocialUser(Integer, String, String)} 一样的逻辑。 | ||
| 92 | + * 所以在认证信息不正确的情况下,也会抛出 {@link ServiceException} 业务异常 | ||
| 93 | + * | ||
| 94 | + * @param userType 用户类型 | ||
| 95 | + * @param type 社交平台的类型 | ||
| 96 | + * @param code 授权码 | ||
| 97 | + * @param state state | ||
| 98 | + * @return 绑定用户编号 | ||
| 99 | + */ | ||
| 100 | + Long getBindUserId(Integer userType, Integer type, String code, String state); | ||
| 89 | } | 101 | } |
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialUserServiceImpl.java
| @@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.system.service.social; | @@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.system.service.social; | ||
| 3 | import cn.hutool.core.collection.CollUtil; | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; | 4 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; |
| 5 | import cn.iocoder.yudao.framework.common.util.http.HttpUtils; | 5 | import cn.iocoder.yudao.framework.common.util.http.HttpUtils; |
| 6 | +import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO; | ||
| 6 | import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserDO; | 7 | import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserDO; |
| 7 | import cn.iocoder.yudao.module.system.dal.mysql.social.SocialUserMapper; | 8 | import cn.iocoder.yudao.module.system.dal.mysql.social.SocialUserMapper; |
| 8 | import cn.iocoder.yudao.module.system.dal.redis.social.SocialAuthUserRedisDAO; | 9 | import cn.iocoder.yudao.module.system.dal.redis.social.SocialAuthUserRedisDAO; |
| @@ -25,8 +26,7 @@ import java.util.Objects; | @@ -25,8 +26,7 @@ import java.util.Objects; | ||
| 25 | 26 | ||
| 26 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | 27 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 27 | import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString; | 28 | import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString; |
| 28 | -import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SOCIAL_USER_AUTH_FAILURE; | ||
| 29 | -import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SOCIAL_USER_UNBIND_NOT_SELF; | 29 | +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; |
| 30 | 30 | ||
| 31 | /** | 31 | /** |
| 32 | * 社交用户 Service 实现类 | 32 | * 社交用户 Service 实现类 |
| @@ -72,8 +72,16 @@ public class SocialUserServiceImpl implements SocialUserService { | @@ -72,8 +72,16 @@ public class SocialUserServiceImpl implements SocialUserService { | ||
| 72 | return authUser; | 72 | return authUser; |
| 73 | } | 73 | } |
| 74 | 74 | ||
| 75 | - @Override | ||
| 76 | - public List<SocialUserDO> getAllSocialUserList(Integer type, String unionId, Integer userType) { | 75 | + /** |
| 76 | + * 获得 unionId 对应的某个社交平台的“所有”社交用户 | ||
| 77 | + * 注意,这里的“所有”,指的是类似【微信】平台,包括了小程序、公众号、PC 网站,他们的 unionId 是一致的 | ||
| 78 | + * | ||
| 79 | + * @param type 社交平台的类型 {@link SocialTypeEnum} | ||
| 80 | + * @param unionId 社交平台的 unionId | ||
| 81 | + * @param userType 全局用户类型 | ||
| 82 | + * @return 社交用户列表 | ||
| 83 | + */ | ||
| 84 | + private List<SocialUserDO> getAllSocialUserList(Integer type, String unionId, Integer userType) { | ||
| 77 | List<Integer> types = SocialTypeEnum.getRelationTypes(type); | 85 | List<Integer> types = SocialTypeEnum.getRelationTypes(type); |
| 78 | return socialUserMapper.selectListByTypeAndUnionId(userType, types, unionId); | 86 | return socialUserMapper.selectListByTypeAndUnionId(userType, types, unionId); |
| 79 | } | 87 | } |
| @@ -84,8 +92,28 @@ public class SocialUserServiceImpl implements SocialUserService { | @@ -84,8 +92,28 @@ public class SocialUserServiceImpl implements SocialUserService { | ||
| 84 | } | 92 | } |
| 85 | 93 | ||
| 86 | @Override | 94 | @Override |
| 95 | + public void bindSocialUser(SocialUserBindReqDTO reqDTO) { | ||
| 96 | + // 使用 code 授权 | ||
| 97 | + AuthUser authUser = getAuthUser(reqDTO.getType(), reqDTO.getCode(), | ||
| 98 | + reqDTO.getState()); | ||
| 99 | + if (authUser == null) { | ||
| 100 | + throw exception(SOCIAL_USER_NOT_FOUND); | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + // 绑定社交用户(新增) | ||
| 104 | + bindSocialUser(reqDTO.getUserId(), reqDTO.getUserType(), | ||
| 105 | + reqDTO.getType(), authUser); | ||
| 106 | + } | ||
| 107 | + | ||
| 108 | + /** | ||
| 109 | + * 绑定社交用户 | ||
| 110 | + * @param userId 用户编号 | ||
| 111 | + * @param userType 用户类型 | ||
| 112 | + * @param type 社交平台的类型 {@link SocialTypeEnum} | ||
| 113 | + * @param authUser 授权用户 | ||
| 114 | + */ | ||
| 87 | @Transactional(rollbackFor = Exception.class) | 115 | @Transactional(rollbackFor = Exception.class) |
| 88 | - public void bindSocialUser(Long userId, Integer userType, Integer type, AuthUser authUser) { | 116 | + protected void bindSocialUser(Long userId, Integer userType, Integer type, AuthUser authUser) { |
| 89 | // 获得 unionId 对应的 SocialUserDO 列表 | 117 | // 获得 unionId 对应的 SocialUserDO 列表 |
| 90 | String unionId = getAuthUserUnionId(authUser); | 118 | String unionId = getAuthUserUnionId(authUser); |
| 91 | List<SocialUserDO> socialUsers = this.getAllSocialUserList(type, unionId, userType); | 119 | List<SocialUserDO> socialUsers = this.getAllSocialUserList(type, unionId, userType); |
| @@ -137,6 +165,30 @@ public class SocialUserServiceImpl implements SocialUserService { | @@ -137,6 +165,30 @@ public class SocialUserServiceImpl implements SocialUserService { | ||
| 137 | socialUserMapper.deleteBatchIds(CollectionUtils.convertSet(socialUsers, SocialUserDO::getId)); | 165 | socialUserMapper.deleteBatchIds(CollectionUtils.convertSet(socialUsers, SocialUserDO::getId)); |
| 138 | } | 166 | } |
| 139 | 167 | ||
| 168 | + @Override | ||
| 169 | + public void checkSocialUser(Integer type, String code, String state) { | ||
| 170 | + AuthUser authUser = getAuthUser(type, code, state); | ||
| 171 | + if (authUser == null) { | ||
| 172 | + throw exception(SOCIAL_USER_NOT_FOUND); | ||
| 173 | + } | ||
| 174 | + } | ||
| 175 | + | ||
| 176 | + @Override | ||
| 177 | + public Long getBindUserId(Integer userType, Integer type, String code, String state) { | ||
| 178 | + AuthUser authUser = getAuthUser(type, code, state); | ||
| 179 | + if (authUser == null) { | ||
| 180 | + throw exception(SOCIAL_USER_NOT_FOUND); | ||
| 181 | + } | ||
| 182 | + | ||
| 183 | + // 如果未绑定 SocialUserDO 用户,则无法自动登录,进行报错 | ||
| 184 | + String unionId = getAuthUserUnionId(authUser); | ||
| 185 | + List<SocialUserDO> socialUsers = getAllSocialUserList(type, unionId, userType); | ||
| 186 | + if (CollUtil.isEmpty(socialUsers)) { | ||
| 187 | + throw exception(AUTH_THIRD_LOGIN_NOT_BIND); | ||
| 188 | + } | ||
| 189 | + return socialUsers.get(0).getUserId(); | ||
| 190 | + } | ||
| 191 | + | ||
| 140 | @VisibleForTesting | 192 | @VisibleForTesting |
| 141 | public void unbindOldSocialUser(Long userId, Integer userType, Integer type, String newUnionId) { | 193 | public void unbindOldSocialUser(Long userId, Integer userType, Integer type, String newUnionId) { |
| 142 | List<Integer> types = SocialTypeEnum.getRelationTypes(type); | 194 | List<Integer> types = SocialTypeEnum.getRelationTypes(type); |