Commit 7b482f572986bda8f3f8e6cbe4b646f2497a0c7e
1 parent
59752d43
✨ Member:增加获取单个社交用户的接口
Showing
15 changed files
with
103 additions
and
42 deletions
yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/social/AppSocialUserController.java
| ... | ... | @@ -2,11 +2,16 @@ package cn.iocoder.yudao.module.member.controller.app.social; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 5 | +import cn.iocoder.yudao.framework.common.util.object.BeanUtils; | |
| 5 | 6 | import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated; |
| 6 | 7 | import cn.iocoder.yudao.module.member.controller.app.social.vo.AppSocialUserBindReqVO; |
| 8 | +import cn.iocoder.yudao.module.member.controller.app.social.vo.AppSocialUserRespVO; | |
| 7 | 9 | import cn.iocoder.yudao.module.member.controller.app.social.vo.AppSocialUserUnbindReqVO; |
| 8 | -import cn.iocoder.yudao.module.member.convert.social.SocialUserConvert; | |
| 9 | 10 | import cn.iocoder.yudao.module.system.api.social.SocialUserApi; |
| 11 | +import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO; | |
| 12 | +import cn.iocoder.yudao.module.system.api.social.dto.SocialUserRespDTO; | |
| 13 | +import cn.iocoder.yudao.module.system.api.social.dto.SocialUserUnbindReqDTO; | |
| 14 | +import io.swagger.v3.oas.annotations.Parameter; | |
| 10 | 15 | import io.swagger.v3.oas.annotations.tags.Tag; |
| 11 | 16 | import io.swagger.v3.oas.annotations.Operation; |
| 12 | 17 | import org.springframework.validation.annotation.Validated; |
| ... | ... | @@ -15,11 +20,12 @@ import org.springframework.web.bind.annotation.*; |
| 15 | 20 | import jakarta.annotation.Resource; |
| 16 | 21 | import jakarta.validation.Valid; |
| 17 | 22 | |
| 23 | +import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; | |
| 18 | 24 | import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; |
| 19 | 25 | |
| 20 | 26 | @Tag(name = "用户 App - 社交用户") |
| 21 | 27 | @RestController |
| 22 | -@RequestMapping("/system/social-user") | |
| 28 | +@RequestMapping("/member/social-user") | |
| 23 | 29 | @Validated |
| 24 | 30 | public class AppSocialUserController { |
| 25 | 31 | |
| ... | ... | @@ -29,16 +35,29 @@ public class AppSocialUserController { |
| 29 | 35 | @PostMapping("/bind") |
| 30 | 36 | @Operation(summary = "社交绑定,使用 code 授权码") |
| 31 | 37 | public CommonResult<Boolean> socialBind(@RequestBody @Valid AppSocialUserBindReqVO reqVO) { |
| 32 | - socialUserApi.bindSocialUser(SocialUserConvert.INSTANCE.convert(getLoginUserId(), UserTypeEnum.MEMBER.getValue(), reqVO)); | |
| 33 | - return CommonResult.success(true); | |
| 38 | + SocialUserBindReqDTO reqDTO = new SocialUserBindReqDTO(getLoginUserId(), UserTypeEnum.MEMBER.getValue(), | |
| 39 | + reqVO.getType(), reqVO.getCode(), reqVO.getState()); | |
| 40 | + socialUserApi.bindSocialUser(reqDTO); | |
| 41 | + return success(true); | |
| 34 | 42 | } |
| 35 | 43 | |
| 36 | 44 | @DeleteMapping("/unbind") |
| 37 | 45 | @Operation(summary = "取消社交绑定") |
| 38 | 46 | @PreAuthenticated |
| 39 | 47 | public CommonResult<Boolean> socialUnbind(@RequestBody AppSocialUserUnbindReqVO reqVO) { |
| 40 | - socialUserApi.unbindSocialUser(SocialUserConvert.INSTANCE.convert(getLoginUserId(), UserTypeEnum.MEMBER.getValue(), reqVO)); | |
| 41 | - return CommonResult.success(true); | |
| 48 | + SocialUserUnbindReqDTO reqDTO = new SocialUserUnbindReqDTO(getLoginUserId(), UserTypeEnum.MEMBER.getValue(), | |
| 49 | + reqVO.getType(), reqVO.getOpenid()); | |
| 50 | + socialUserApi.unbindSocialUser(reqDTO); | |
| 51 | + return success(true); | |
| 52 | + } | |
| 53 | + | |
| 54 | + @GetMapping("/get") | |
| 55 | + @Operation(summary = "获得社交用户") | |
| 56 | + @Parameter(name = "type", description = "社交平台的类型,参见 SocialTypeEnum 枚举值", required = true, example = "10") | |
| 57 | + @PreAuthenticated | |
| 58 | + public CommonResult<AppSocialUserRespVO> getSocialUser(@RequestParam("type") Integer type) { | |
| 59 | + SocialUserRespDTO socialUser = socialUserApi.getSocialUserByUserId(UserTypeEnum.MEMBER.getValue(), getLoginUserId(), type); | |
| 60 | + return success(BeanUtils.toBean(socialUser, AppSocialUserRespVO.class)); | |
| 42 | 61 | } |
| 43 | 62 | |
| 44 | 63 | } | ... | ... |
yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/social/vo/AppSocialUserBindReqVO.java
| ... | ... | @@ -13,9 +13,6 @@ import jakarta.validation.constraints.NotNull; |
| 13 | 13 | |
| 14 | 14 | @Schema(description = "用户 APP - 社交绑定 Request VO,使用 code 授权码") |
| 15 | 15 | @Data |
| 16 | -@NoArgsConstructor | |
| 17 | -@AllArgsConstructor | |
| 18 | -@Builder | |
| 19 | 16 | public class AppSocialUserBindReqVO { |
| 20 | 17 | |
| 21 | 18 | @Schema(description = "社交平台的类型,参见 SocialTypeEnum 枚举值", requiredMode = Schema.RequiredMode.REQUIRED, example = "10") | ... | ... |
yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/social/vo/AppSocialUserRespVO.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.member.controller.app.social.vo; | |
| 2 | + | |
| 3 | +import io.swagger.v3.oas.annotations.media.Schema; | |
| 4 | +import lombok.Data; | |
| 5 | + | |
| 6 | +@Schema(description = "用户 APP - 社交用户 Response VO") | |
| 7 | +@Data | |
| 8 | +public class AppSocialUserRespVO { | |
| 9 | + | |
| 10 | + @Schema(description = "社交用户的 openid", requiredMode = Schema.RequiredMode.REQUIRED, example = "IPRmJ0wvBptiPIlGEZiPewGwiEiE") | |
| 11 | + private String openid; | |
| 12 | + | |
| 13 | + @Schema(description = "社交用户的昵称", requiredMode = Schema.RequiredMode.REQUIRED, example = "芋道源码") | |
| 14 | + private String nickname; | |
| 15 | + | |
| 16 | + @Schema(description = "社交用户的头像", requiredMode = Schema.RequiredMode.REQUIRED, example = "https://www.iocoder.cn/1.png") | |
| 17 | + private String avatar; | |
| 18 | + | |
| 19 | +} | ... | ... |
yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/social/vo/AppSocialUserUnbindReqVO.java
| ... | ... | @@ -13,9 +13,6 @@ import jakarta.validation.constraints.NotNull; |
| 13 | 13 | |
| 14 | 14 | @Schema(description = "用户 APP - 取消社交绑定 Request VO") |
| 15 | 15 | @Data |
| 16 | -@NoArgsConstructor | |
| 17 | -@AllArgsConstructor | |
| 18 | -@Builder | |
| 19 | 16 | public class AppSocialUserUnbindReqVO { |
| 20 | 17 | |
| 21 | 18 | @Schema(description = "社交平台的类型,参见 SocialTypeEnum 枚举值", requiredMode = Schema.RequiredMode.REQUIRED, example = "10") | ... | ... |
yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/convert/social/SocialUserConvert.java deleted
| 1 | -package cn.iocoder.yudao.module.member.convert.social; | |
| 2 | - | |
| 3 | -import cn.iocoder.yudao.module.member.controller.app.social.vo.AppSocialUserBindReqVO; | |
| 4 | -import cn.iocoder.yudao.module.member.controller.app.social.vo.AppSocialUserUnbindReqVO; | |
| 5 | -import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO; | |
| 6 | -import cn.iocoder.yudao.module.system.api.social.dto.SocialUserUnbindReqDTO; | |
| 7 | -import org.mapstruct.Mapper; | |
| 8 | -import org.mapstruct.factory.Mappers; | |
| 9 | - | |
| 10 | -@Mapper | |
| 11 | -public interface SocialUserConvert { | |
| 12 | - | |
| 13 | - SocialUserConvert INSTANCE = Mappers.getMapper(SocialUserConvert.class); | |
| 14 | - | |
| 15 | - SocialUserBindReqDTO convert(Long userId, Integer userType, AppSocialUserBindReqVO reqVO); | |
| 16 | - | |
| 17 | - SocialUserUnbindReqDTO convert(Long userId, Integer userType, AppSocialUserUnbindReqVO reqVO); | |
| 18 | - | |
| 19 | -} |
yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthServiceImpl.java
| ... | ... | @@ -103,7 +103,7 @@ public class MemberAuthServiceImpl implements MemberAuthService { |
| 103 | 103 | @Transactional |
| 104 | 104 | public AppAuthLoginRespVO socialLogin(AppAuthSocialLoginReqVO reqVO) { |
| 105 | 105 | // 使用 code 授权码,进行登录。然后,获得到绑定的用户编号 |
| 106 | - SocialUserRespDTO socialUser = socialUserApi.getSocialUser(UserTypeEnum.MEMBER.getValue(), reqVO.getType(), | |
| 106 | + SocialUserRespDTO socialUser = socialUserApi.getSocialUserByCode(UserTypeEnum.MEMBER.getValue(), reqVO.getType(), | |
| 107 | 107 | reqVO.getCode(), reqVO.getState()); |
| 108 | 108 | if (socialUser == null) { |
| 109 | 109 | throw exception(AUTH_SOCIAL_USER_NOT_FOUND); | ... | ... |
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/social/SocialUserApi.java
| ... | ... | @@ -30,6 +30,16 @@ public interface SocialUserApi { |
| 30 | 30 | void unbindSocialUser(@Valid SocialUserUnbindReqDTO reqDTO); |
| 31 | 31 | |
| 32 | 32 | /** |
| 33 | + * 获得社交用户,基于 userId | |
| 34 | + * | |
| 35 | + * @param userType 用户类型 | |
| 36 | + * @param userId 用户编号 | |
| 37 | + * @param socialType 社交平台的类型 | |
| 38 | + * @return 社交用户 | |
| 39 | + */ | |
| 40 | + SocialUserRespDTO getSocialUserByUserId(Integer userType, Long userId, Integer socialType); | |
| 41 | + | |
| 42 | + /** | |
| 33 | 43 | * 获得社交用户 |
| 34 | 44 | * |
| 35 | 45 | * 在认证信息不正确的情况下,也会抛出 {@link ServiceException} 业务异常 |
| ... | ... | @@ -40,7 +50,6 @@ public interface SocialUserApi { |
| 40 | 50 | * @param state state |
| 41 | 51 | * @return 社交用户 |
| 42 | 52 | */ |
| 43 | - SocialUserRespDTO getSocialUser(Integer userType, Integer socialType, | |
| 44 | - String code, String state); | |
| 53 | + SocialUserRespDTO getSocialUserByCode(Integer userType, Integer socialType, String code, String state); | |
| 45 | 54 | |
| 46 | 55 | } | ... | ... |
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/social/dto/SocialUserUnbindReqDTO.java
| ... | ... | @@ -3,10 +3,12 @@ package cn.iocoder.yudao.module.system.api.social.dto; |
| 3 | 3 | import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.validation.InEnum; |
| 5 | 5 | import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum; |
| 6 | +import lombok.AllArgsConstructor; | |
| 6 | 7 | import lombok.Data; |
| 7 | 8 | |
| 8 | 9 | import jakarta.validation.constraints.NotEmpty; |
| 9 | 10 | import jakarta.validation.constraints.NotNull; |
| 11 | +import lombok.NoArgsConstructor; | |
| 10 | 12 | |
| 11 | 13 | /** |
| 12 | 14 | * 社交绑定 Request DTO,使用 code 授权码 |
| ... | ... | @@ -14,6 +16,8 @@ import jakarta.validation.constraints.NotNull; |
| 14 | 16 | * @author 芋道源码 |
| 15 | 17 | */ |
| 16 | 18 | @Data |
| 19 | +@AllArgsConstructor | |
| 20 | +@NoArgsConstructor | |
| 17 | 21 | public class SocialUserUnbindReqDTO { |
| 18 | 22 | |
| 19 | 23 | /** | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/social/SocialUserApiImpl.java
| ... | ... | @@ -33,8 +33,13 @@ public class SocialUserApiImpl implements SocialUserApi { |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | @Override |
| 36 | - public SocialUserRespDTO getSocialUser(Integer userType, Integer socialType, String code, String state) { | |
| 37 | - return socialUserService.getSocialUser(userType, socialType, code, state); | |
| 36 | + public SocialUserRespDTO getSocialUserByUserId(Integer userType, Long userId, Integer socialType) { | |
| 37 | + return socialUserService.getSocialUserByUserId(userType, userId, socialType); | |
| 38 | + } | |
| 39 | + | |
| 40 | + @Override | |
| 41 | + public SocialUserRespDTO getSocialUserByCode(Integer userType, Integer socialType, String code, String state) { | |
| 42 | + return socialUserService.getSocialUserByCode(userType, socialType, code, state); | |
| 38 | 43 | } |
| 39 | 44 | |
| 40 | 45 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/social/SocialUserBindMapper.java
| ... | ... | @@ -34,4 +34,11 @@ public interface SocialUserBindMapper extends BaseMapperX<SocialUserBindDO> { |
| 34 | 34 | .eq(SocialUserBindDO::getUserType, userType)); |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | + default SocialUserBindDO selectByUserIdAndUserTypeAndSocialType(Long userId, Integer userType, Integer socialType) { | |
| 38 | + return selectOne(new LambdaQueryWrapperX<SocialUserBindDO>() | |
| 39 | + .eq(SocialUserBindDO::getUserId, userId) | |
| 40 | + .eq(SocialUserBindDO::getUserType, userType) | |
| 41 | + .eq(SocialUserBindDO::getSocialType, socialType)); | |
| 42 | + } | |
| 43 | + | |
| 37 | 44 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java
| ... | ... | @@ -156,7 +156,7 @@ public class AdminAuthServiceImpl implements AdminAuthService { |
| 156 | 156 | @Override |
| 157 | 157 | public AuthLoginRespVO socialLogin(AuthSocialLoginReqVO reqVO) { |
| 158 | 158 | // 使用 code 授权码,进行登录。然后,获得到绑定的用户编号 |
| 159 | - SocialUserRespDTO socialUser = socialUserService.getSocialUser(UserTypeEnum.ADMIN.getValue(), reqVO.getType(), | |
| 159 | + SocialUserRespDTO socialUser = socialUserService.getSocialUserByCode(UserTypeEnum.ADMIN.getValue(), reqVO.getType(), | |
| 160 | 160 | reqVO.getCode(), reqVO.getState()); |
| 161 | 161 | if (socialUser == null || socialUser.getUserId() == null) { |
| 162 | 162 | throw exception(AUTH_THIRD_LOGIN_NOT_BIND); | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialUserService.java
| ... | ... | @@ -46,6 +46,16 @@ public interface SocialUserService { |
| 46 | 46 | void unbindSocialUser(Long userId, Integer userType, Integer socialType, String openid); |
| 47 | 47 | |
| 48 | 48 | /** |
| 49 | + * 获得社交用户,基于 userId | |
| 50 | + * | |
| 51 | + * @param userType 用户类型 | |
| 52 | + * @param userId 用户编号 | |
| 53 | + * @param socialType 社交平台的类型 | |
| 54 | + * @return 社交用户 | |
| 55 | + */ | |
| 56 | + SocialUserRespDTO getSocialUserByUserId(Integer userType, Long userId, Integer socialType); | |
| 57 | + | |
| 58 | + /** | |
| 49 | 59 | * 获得社交用户 |
| 50 | 60 | * |
| 51 | 61 | * 在认证信息不正确的情况下,也会抛出 {@link ServiceException} 业务异常 |
| ... | ... | @@ -56,7 +66,7 @@ public interface SocialUserService { |
| 56 | 66 | * @param state state |
| 57 | 67 | * @return 社交用户 |
| 58 | 68 | */ |
| 59 | - SocialUserRespDTO getSocialUser(Integer userType, Integer socialType, String code, String state); | |
| 69 | + SocialUserRespDTO getSocialUserByCode(Integer userType, Integer socialType, String code, String state); | |
| 60 | 70 | |
| 61 | 71 | // ==================== 社交用户 CRUD ==================== |
| 62 | 72 | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialUserServiceImpl.java
| ... | ... | @@ -26,7 +26,6 @@ import java.util.List; |
| 26 | 26 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 27 | 27 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; |
| 28 | 28 | import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString; |
| 29 | -import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.AUTH_THIRD_LOGIN_NOT_BIND; | |
| 30 | 29 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SOCIAL_USER_NOT_FOUND; |
| 31 | 30 | |
| 32 | 31 | /** |
| ... | ... | @@ -94,7 +93,21 @@ public class SocialUserServiceImpl implements SocialUserService { |
| 94 | 93 | } |
| 95 | 94 | |
| 96 | 95 | @Override |
| 97 | - public SocialUserRespDTO getSocialUser(Integer userType, Integer socialType, String code, String state) { | |
| 96 | + public SocialUserRespDTO getSocialUserByUserId(Integer userType, Long userId, Integer socialType) { | |
| 97 | + // 获得绑定用户 | |
| 98 | + SocialUserBindDO socialUserBind = socialUserBindMapper.selectByUserIdAndUserTypeAndSocialType(userId, userType, socialType); | |
| 99 | + if (socialUserBind == null) { | |
| 100 | + return null; | |
| 101 | + } | |
| 102 | + // 获得社交用户 | |
| 103 | + SocialUserDO socialUser = socialUserMapper.selectById(socialUserBind.getSocialUserId()); | |
| 104 | + Assert.notNull(socialUser, "社交用户不能为空"); | |
| 105 | + return new SocialUserRespDTO(socialUser.getOpenid(), socialUser.getNickname(), socialUser.getAvatar(), | |
| 106 | + socialUserBind.getUserId()); | |
| 107 | + } | |
| 108 | + | |
| 109 | + @Override | |
| 110 | + public SocialUserRespDTO getSocialUserByCode(Integer userType, Integer socialType, String code, String state) { | |
| 98 | 111 | // 获得社交用户 |
| 99 | 112 | SocialUserDO socialUser = authSocialUser(socialType, userType, code, state); |
| 100 | 113 | Assert.notNull(socialUser, "社交用户不能为空"); | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImplTest.java
| ... | ... | @@ -236,7 +236,7 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest { |
| 236 | 236 | AuthSocialLoginReqVO reqVO = randomPojo(AuthSocialLoginReqVO.class); |
| 237 | 237 | // mock 方法(绑定的用户编号) |
| 238 | 238 | Long userId = 1L; |
| 239 | - when(socialUserService.getSocialUser(eq(UserTypeEnum.ADMIN.getValue()), eq(reqVO.getType()), | |
| 239 | + when(socialUserService.getSocialUserByCode(eq(UserTypeEnum.ADMIN.getValue()), eq(reqVO.getType()), | |
| 240 | 240 | eq(reqVO.getCode()), eq(reqVO.getState()))).thenReturn(new SocialUserRespDTO(randomString(), userId)); |
| 241 | 241 | // mock(用户) |
| 242 | 242 | AdminUserDO user = randomPojo(AdminUserDO.class, o -> o.setId(userId)); | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/social/SocialUserServiceImplTest.java
| ... | ... | @@ -147,7 +147,7 @@ public class SocialUserServiceImplTest extends BaseDbUnitTest { |
| 147 | 147 | socialUserBindMapper.insert(socialUserBind); |
| 148 | 148 | |
| 149 | 149 | // 调用 |
| 150 | - SocialUserRespDTO socialUser = socialUserService.getSocialUser(userType, type, code, state); | |
| 150 | + SocialUserRespDTO socialUser = socialUserService.getSocialUserByCode(userType, type, code, state); | |
| 151 | 151 | // 断言 |
| 152 | 152 | assertEquals(userId, socialUser.getUserId()); |
| 153 | 153 | assertEquals(socialUserDO.getOpenid(), socialUser.getOpenid()); | ... | ... |