Commit 1b7093f5c16b787c5f837b5a291661068691aa88

Authored by YunaiV
1 parent 75d34080

用户 app 的社交登录接口,合并到手机号 + 密码,手机号 + 验证码的 login 接口中,统一维护。

Showing 10 changed files with 79 additions and 85 deletions
yudao-framework/yudao-spring-boot-starter-biz-social/src/main/java/cn/iocoder/yudao/framework/social/core/enums/AuthExtendSource.java
... ... @@ -3,7 +3,7 @@ package cn.iocoder.yudao.framework.social.core.enums;
3 3 import me.zhyd.oauth.config.AuthSource;
4 4  
5 5 /**
6   - * 拓展JustAuth各api需要的url, 用枚举类分平台类型管理
  6 + * 拓展 JustAuth 各 api 需要的 url, 用枚举类分平台类型管理
7 7 *
8 8 * 默认配置 {@link me.zhyd.oauth.config.AuthDefaultSource}
9 9 *
... ...
yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/auth/AppAuthController.java
... ... @@ -104,16 +104,10 @@ public class AppAuthController {
104 104 return CommonResult.success(authService.getSocialAuthorizeUrl(type, redirectUri));
105 105 }
106 106  
107   - @PostMapping("/social-quick-login")
  107 + @PostMapping("/social-login")
108 108 @ApiOperation(value = "社交快捷登录,使用 code 授权码", notes = "适合未登录的用户,但是社交账号已绑定用户")
109   - public CommonResult<AppAuthLoginRespVO> socialQuickLogin(@RequestBody @Valid AppAuthSocialQuickLoginReqVO reqVO) {
110   - return success(authService.socialQuickLogin(reqVO));
111   - }
112   -
113   - @PostMapping("/social-bind-login")
114   - @ApiOperation(value = "社交绑定登录,使用 手机号 + 手机验证码", notes = "适合未登录的用户,进行登录 + 绑定")
115   - public CommonResult<AppAuthLoginRespVO> socialBindLogin(@RequestBody @Valid AppAuthSocialBindLoginReqVO reqVO) {
116   - return success(authService.socialBindLogin(reqVO));
  109 + public CommonResult<AppAuthLoginRespVO> socialLogin(@RequestBody @Valid AppAuthSocialLoginReqVO reqVO) {
  110 + return success(authService.socialLogin(reqVO));
117 111 }
118 112  
119 113 }
... ...
yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/auth/vo/AppAuthLoginReqVO.java
1 1 package cn.iocoder.yudao.module.member.controller.app.auth.vo;
2 2  
  3 +import cn.hutool.core.util.StrUtil;
  4 +import cn.iocoder.yudao.framework.common.validation.InEnum;
3 5 import cn.iocoder.yudao.framework.common.validation.Mobile;
  6 +import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
4 7 import io.swagger.annotations.ApiModel;
5 8 import io.swagger.annotations.ApiModelProperty;
6 9 import lombok.AllArgsConstructor;
... ... @@ -9,9 +12,10 @@ import lombok.Data;
9 12 import lombok.NoArgsConstructor;
10 13 import org.hibernate.validator.constraints.Length;
11 14  
  15 +import javax.validation.constraints.AssertTrue;
12 16 import javax.validation.constraints.NotEmpty;
13 17  
14   -@ApiModel("用户 APP - 手机 + 密码登录 Request VO")
  18 +@ApiModel(value = "用户 APP - 手机 + 密码登录 Request VO", description = "如果登录并绑定社交用户,需要传递 social 开头的参数")
15 19 @Data
16 20 @NoArgsConstructor
17 21 @AllArgsConstructor
... ... @@ -28,4 +32,26 @@ public class AppAuthLoginReqVO {
28 32 @Length(min = 4, max = 16, message = "密码长度为 4-16 位")
29 33 private String password;
30 34  
  35 + // ========== 绑定社交登录时,需要传递如下参数 ==========
  36 +
  37 + @ApiModelProperty(value = "社交平台的类型", required = true, example = "10", notes = "参见 SysUserSocialTypeEnum 枚举值")
  38 + @InEnum(SocialTypeEnum.class)
  39 + private Integer socialType;
  40 +
  41 + @ApiModelProperty(value = "授权码", required = true, example = "1024")
  42 + private String socialCode;
  43 +
  44 + @ApiModelProperty(value = "state", required = true, example = "9b2ffbc1-7425-4155-9894-9d5c08541d62")
  45 + private String socialState;
  46 +
  47 + @AssertTrue(message = "授权码不能为空")
  48 + public boolean isSocialCodeValid() {
  49 + return socialType == null || StrUtil.isNotEmpty(socialCode);
  50 + }
  51 +
  52 + @AssertTrue(message = "授权 state 不能为空")
  53 + public boolean isSocialState() {
  54 + return socialType == null || StrUtil.isNotEmpty(socialState);
  55 + }
  56 +
31 57 }
... ...
yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/auth/vo/AppAuthSmsLoginReqVO.java
1 1 package cn.iocoder.yudao.module.member.controller.app.auth.vo;
2 2  
  3 +import cn.hutool.core.util.StrUtil;
  4 +import cn.iocoder.yudao.framework.common.validation.InEnum;
3 5 import cn.iocoder.yudao.framework.common.validation.Mobile;
  6 +import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
4 7 import io.swagger.annotations.ApiModel;
5 8 import io.swagger.annotations.ApiModelProperty;
6 9 import lombok.AllArgsConstructor;
... ... @@ -9,10 +12,11 @@ import lombok.Data;
9 12 import lombok.NoArgsConstructor;
10 13 import org.hibernate.validator.constraints.Length;
11 14  
  15 +import javax.validation.constraints.AssertTrue;
12 16 import javax.validation.constraints.NotEmpty;
13 17 import javax.validation.constraints.Pattern;
14 18  
15   -@ApiModel("用户 APP - 手机 + 验证码登录 Request VO")
  19 +@ApiModel(value = "用户 APP - 手机 + 验证码登录 Request VO", description = "如果登录并绑定社交用户,需要传递 social 开头的参数")
16 20 @Data
17 21 @NoArgsConstructor
18 22 @AllArgsConstructor
... ... @@ -30,4 +34,26 @@ public class AppAuthSmsLoginReqVO {
30 34 @Pattern(regexp = "^[0-9]+$", message = "手机验证码必须都是数字")
31 35 private String code;
32 36  
  37 + // ========== 绑定社交登录时,需要传递如下参数 ==========
  38 +
  39 + @ApiModelProperty(value = "社交平台的类型", required = true, example = "10", notes = "参见 SysUserSocialTypeEnum 枚举值")
  40 + @InEnum(SocialTypeEnum.class)
  41 + private Integer socialType;
  42 +
  43 + @ApiModelProperty(value = "授权码", required = true, example = "1024")
  44 + private String socialCode;
  45 +
  46 + @ApiModelProperty(value = "state", required = true, example = "9b2ffbc1-7425-4155-9894-9d5c08541d62")
  47 + private String socialState;
  48 +
  49 + @AssertTrue(message = "授权码不能为空")
  50 + public boolean isSocialCodeValid() {
  51 + return socialType == null || StrUtil.isNotEmpty(socialCode);
  52 + }
  53 +
  54 + @AssertTrue(message = "授权 state 不能为空")
  55 + public boolean isSocialState() {
  56 + return socialType == null || StrUtil.isNotEmpty(socialState);
  57 + }
  58 +
33 59 }
... ...
yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/auth/vo/AppAuthSocialBindLoginReqVO.java deleted
1   -package cn.iocoder.yudao.module.member.controller.app.auth.vo;
2   -
3   -import cn.iocoder.yudao.framework.common.validation.InEnum;
4   -import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
5   -import io.swagger.annotations.ApiModel;
6   -import io.swagger.annotations.ApiModelProperty;
7   -import lombok.AllArgsConstructor;
8   -import lombok.Builder;
9   -import lombok.Data;
10   -import lombok.NoArgsConstructor;
11   -import org.hibernate.validator.constraints.Length;
12   -
13   -import javax.validation.constraints.NotEmpty;
14   -import javax.validation.constraints.NotNull;
15   -import javax.validation.constraints.Pattern;
16   -
17   -@ApiModel("用户 APP - 社交绑定登录 Request VO,使用 code 授权码 + 账号密码")
18   -@Data
19   -@NoArgsConstructor
20   -@AllArgsConstructor
21   -@Builder
22   -public class AppAuthSocialBindLoginReqVO {
23   -
24   - @ApiModelProperty(value = "社交平台的类型", required = true, example = "10", notes = "参见 SysUserSocialTypeEnum 枚举值")
25   - @InEnum(SocialTypeEnum.class)
26   - @NotNull(message = "社交平台的类型不能为空")
27   - private Integer type;
28   -
29   - @ApiModelProperty(value = "授权码", required = true, example = "1024")
30   - @NotEmpty(message = "授权码不能为空")
31   - private String code;
32   -
33   - @ApiModelProperty(value = "state", required = true, example = "9b2ffbc1-7425-4155-9894-9d5c08541d62")
34   - @NotEmpty(message = "state 不能为空")
35   - private String state;
36   -
37   - @ApiModelProperty(value = "手机号", required = true, example = "15119100000")
38   - @NotEmpty(message = "手机号不能为空")
39   - @Length(min = 11, max = 11, message = "手机号是11位数字")
40   - private String mobile;
41   -
42   - @ApiModelProperty(value = "手机验证码", required = true, example = "1024")
43   - @NotEmpty(message = "手机验证码不能为空")
44   - @Length(min = 4, max = 6, message = "手机验证码长度为 4-6 位")
45   - @Pattern(regexp = "^[0-9]+$", message = "手机验证码必须都是数字")
46   - private String smsCode;
47   -
48   -}
yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/auth/vo/AppAuthSocialQuickLoginReqVO.java renamed to yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/auth/vo/AppAuthSocialLoginReqVO.java
... ... @@ -17,7 +17,7 @@ import javax.validation.constraints.NotNull;
17 17 @NoArgsConstructor
18 18 @AllArgsConstructor
19 19 @Builder
20   -public class AppAuthSocialQuickLoginReqVO {
  20 +public class AppAuthSocialLoginReqVO {
21 21  
22 22 @ApiModelProperty(value = "社交平台的类型", required = true, example = "10", notes = "参见 SysUserSocialTypeEnum 枚举值")
23 23 @InEnum(SocialTypeEnum.class)
... ...
yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/convert/auth/AuthConvert.java
... ... @@ -16,8 +16,7 @@ public interface AuthConvert {
16 16  
17 17 AuthConvert INSTANCE = Mappers.getMapper(AuthConvert.class);
18 18  
19   - SocialUserBindReqDTO convert(Long userId, Integer userType, AppAuthSocialBindLoginReqVO reqVO);
20   - SocialUserBindReqDTO convert(Long userId, Integer userType, AppAuthSocialQuickLoginReqVO reqVO);
  19 + SocialUserBindReqDTO convert(Long userId, Integer userType, AppAuthSocialLoginReqVO reqVO);
21 20 SocialUserUnbindReqDTO convert(Long userId, Integer userType, AppSocialUserUnbindReqVO reqVO);
22 21  
23 22 SmsCodeSendReqDTO convert(AppAuthSmsSendReqVO reqVO);
... ...
yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthService.java
... ... @@ -42,15 +42,7 @@ public interface MemberAuthService {
42 42 * @param reqVO 登录信息
43 43 * @return 登录结果
44 44 */
45   - AppAuthLoginRespVO socialQuickLogin(@Valid AppAuthSocialQuickLoginReqVO reqVO);
46   -
47   - /**
48   - * 社交登录,使用 手机号 + 手机验证码
49   - *
50   - * @param reqVO 登录信息
51   - * @return 登录结果
52   - */
53   - AppAuthLoginRespVO socialBindLogin(@Valid AppAuthSocialBindLoginReqVO reqVO);
  45 + AppAuthLoginRespVO socialLogin(@Valid AppAuthSocialLoginReqVO reqVO);
54 46  
55 47 /**
56 48 * 获得社交认证 URL
... ...
yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthServiceImpl.java
... ... @@ -18,6 +18,7 @@ import cn.iocoder.yudao.module.system.api.logger.LoginLogApi;
18 18 import cn.iocoder.yudao.module.system.api.logger.dto.LoginLogCreateReqDTO;
19 19 import cn.iocoder.yudao.module.system.api.sms.SmsCodeApi;
20 20 import cn.iocoder.yudao.module.system.api.social.SocialUserApi;
  21 +import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO;
21 22 import cn.iocoder.yudao.module.system.enums.auth.OAuth2ClientConstants;
22 23 import cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum;
23 24 import cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum;
... ... @@ -65,6 +66,12 @@ public class MemberAuthServiceImpl implements MemberAuthService {
65 66 // 使用手机 + 密码,进行登录。
66 67 MemberUserDO user = login0(reqVO.getMobile(), reqVO.getPassword());
67 68  
  69 + // 如果 socialType 非空,说明需要绑定社交用户
  70 + if (reqVO.getSocialType() != null) {
  71 + socialUserApi.bindSocialUser(new SocialUserBindReqDTO(user.getId(), getUserType().getValue(),
  72 + reqVO.getSocialType(), reqVO.getSocialCode(), reqVO.getSocialState()));
  73 + }
  74 +
68 75 // 创建 Token 令牌,记录登录日志
69 76 return createTokenAfterLoginSuccess(user, reqVO.getMobile(), LoginLogTypeEnum.LOGIN_MOBILE);
70 77 }
... ... @@ -80,12 +87,18 @@ public class MemberAuthServiceImpl implements MemberAuthService {
80 87 MemberUserDO user = userService.createUserIfAbsent(reqVO.getMobile(), userIp);
81 88 Assert.notNull(user, "获取用户失败,结果为空");
82 89  
  90 + // 如果 socialType 非空,说明需要绑定社交用户
  91 + if (reqVO.getSocialType() != null) {
  92 + socialUserApi.bindSocialUser(new SocialUserBindReqDTO(user.getId(), getUserType().getValue(),
  93 + reqVO.getSocialType(), reqVO.getSocialCode(), reqVO.getSocialState()));
  94 + }
  95 +
83 96 // 创建 Token 令牌,记录登录日志
84 97 return createTokenAfterLoginSuccess(user, reqVO.getMobile(), LoginLogTypeEnum.LOGIN_SMS);
85 98 }
86 99  
87 100 @Override
88   - public AppAuthLoginRespVO socialQuickLogin(AppAuthSocialQuickLoginReqVO reqVO) {
  101 + public AppAuthLoginRespVO socialLogin(AppAuthSocialLoginReqVO reqVO) {
89 102 // 使用 code 授权码,进行登录。然后,获得到绑定的用户编号
90 103 Long userId = socialUserApi.getBindUserId(UserTypeEnum.MEMBER.getValue(), reqVO.getType(),
91 104 reqVO.getCode(), reqVO.getState());
... ... @@ -103,18 +116,6 @@ public class MemberAuthServiceImpl implements MemberAuthService {
103 116 return createTokenAfterLoginSuccess(user, user.getMobile(), LoginLogTypeEnum.LOGIN_SOCIAL);
104 117 }
105 118  
106   - @Override
107   - public AppAuthLoginRespVO socialBindLogin(AppAuthSocialBindLoginReqVO reqVO) {
108   - // 使用手机号、手机验证码登录
109   - AppAuthSmsLoginReqVO loginReqVO = AppAuthSmsLoginReqVO.builder()
110   - .mobile(reqVO.getMobile()).code(reqVO.getSmsCode()).build();
111   - AppAuthLoginRespVO token = smsLogin(loginReqVO);
112   -
113   - // 绑定社交用户
114   - socialUserApi.bindSocialUser(AuthConvert.INSTANCE.convert(token.getUserId(), getUserType().getValue(), reqVO));
115   - return token;
116   - }
117   -
118 119 private AppAuthLoginRespVO createTokenAfterLoginSuccess(MemberUserDO user, String mobile, LoginLogTypeEnum logType) {
119 120 // 插入登陆日志
120 121 createLoginLog(user.getId(), mobile, logType, LoginResultEnum.SUCCESS);
... ...
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/social/dto/SocialUserBindReqDTO.java
... ... @@ -3,7 +3,9 @@ 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;
  8 +import lombok.NoArgsConstructor;
7 9  
8 10 import javax.validation.constraints.NotEmpty;
9 11 import javax.validation.constraints.NotNull;
... ... @@ -14,6 +16,8 @@ import javax.validation.constraints.NotNull;
14 16 * @author 芋道源码
15 17 */
16 18 @Data
  19 +@NoArgsConstructor
  20 +@AllArgsConstructor
17 21 public class SocialUserBindReqDTO {
18 22  
19 23 /**
... ...