From 95254341a1169fb9af29ad5a3576cdef7721a23f Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 19 Aug 2023 23:12:34 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E4=BC=9A=E5=91=98=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=9A=84=E6=89=BE=E5=9B=9E=E5=AF=86=E7=A0=81=20API=20=E7=9A=84?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../member/enums/ErrorCodeConstants.java | 2 +- .../app/auth/AppAuthController.java | 9 ----- .../app/user/AppMemberUserController.java | 12 ++++--- .../vo/AppMemberUserResetPasswordReqVO.java} | 5 ++- .../member/convert/auth/AuthConvert.java | 3 +- .../service/auth/MemberAuthService.java | 6 ---- .../service/auth/MemberAuthServiceImpl.java | 36 +++++-------------- .../service/user/MemberUserService.java | 8 +++++ .../service/user/MemberUserServiceImpl.java | 24 +++++++++++++ .../service/auth/MemberAuthServiceTest.java | 4 +-- .../module/system/enums/sms/SmsSceneEnum.java | 2 +- 11 files changed, 57 insertions(+), 54 deletions(-) rename yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/{auth/vo/AppAuthResetPasswordReqVO.java => user/vo/AppMemberUserResetPasswordReqVO.java} (90%) diff --git a/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/ErrorCodeConstants.java b/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/ErrorCodeConstants.java index c72e5a2da..d726e01c2 100644 --- a/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/ErrorCodeConstants.java +++ b/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/ErrorCodeConstants.java @@ -11,7 +11,7 @@ public interface ErrorCodeConstants { // ========== 用户相关 1004001000============ ErrorCode USER_NOT_EXISTS = new ErrorCode(1004001000, "用户不存在"); - ErrorCode USER_PASSWORD_FAILED = new ErrorCode(1004001001, "密码校验失败"); + ErrorCode USER_MOBILE_NOT_EXISTS = new ErrorCode(1004001001, "手机号未注册用户"); ErrorCode USER_MOBILE_USED = new ErrorCode(1004001002, "修改手机失败,该手机号({})已经被使用"); // ========== AUTH 模块 1004003000 ========== diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/auth/AppAuthController.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/auth/AppAuthController.java index 9fdf2b83a..e55cbc0f0 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/auth/AppAuthController.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/auth/AppAuthController.java @@ -4,7 +4,6 @@ import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog; import cn.iocoder.yudao.framework.security.config.SecurityProperties; -import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated; import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; import cn.iocoder.yudao.module.member.controller.app.auth.vo.*; import cn.iocoder.yudao.module.member.service.auth.MemberAuthService; @@ -84,14 +83,6 @@ public class AppAuthController { return success(true); } - @PostMapping("/reset-password") - @Operation(summary = "重置密码", description = "用户忘记密码时使用") - @PreAuthenticated - public CommonResult resetPassword(@RequestBody @Valid AppAuthResetPasswordReqVO reqVO) { - authService.resetPassword(reqVO); - return success(true); - } - // ========== 社交登录相关 ========== @GetMapping("/social-auth-redirect") diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/user/AppMemberUserController.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/user/AppMemberUserController.java index 0a4aa5b63..84bcc7d2e 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/user/AppMemberUserController.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/user/AppMemberUserController.java @@ -2,10 +2,7 @@ package cn.iocoder.yudao.module.member.controller.app.user; import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated; -import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserInfoRespVO; -import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserUpdatePasswordReqVO; -import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserUpdateReqVO; -import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserUpdateMobileReqVO; +import cn.iocoder.yudao.module.member.controller.app.user.vo.*; import cn.iocoder.yudao.module.member.convert.user.MemberUserConvert; import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO; import cn.iocoder.yudao.module.member.service.user.MemberUserService; @@ -63,5 +60,12 @@ public class AppMemberUserController { return success(true); } + @PutMapping("/reset-password") + @Operation(summary = "重置密码", description = "用户忘记密码时使用") + public CommonResult resetPassword(@RequestBody @Valid AppMemberUserResetPasswordReqVO reqVO) { + userService.resetUserPassword(reqVO); + return success(true); + } + } diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/auth/vo/AppAuthResetPasswordReqVO.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/user/vo/AppMemberUserResetPasswordReqVO.java similarity index 90% rename from yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/auth/vo/AppAuthResetPasswordReqVO.java rename to yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/user/vo/AppMemberUserResetPasswordReqVO.java index 9543714d9..22cbf55ee 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/auth/vo/AppAuthResetPasswordReqVO.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/user/vo/AppMemberUserResetPasswordReqVO.java @@ -1,4 +1,4 @@ -package cn.iocoder.yudao.module.member.controller.app.auth.vo; +package cn.iocoder.yudao.module.member.controller.app.user.vo; import cn.iocoder.yudao.framework.common.validation.Mobile; import io.swagger.v3.oas.annotations.media.Schema; @@ -12,13 +12,12 @@ import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotEmpty; import javax.validation.constraints.Pattern; -// TODO 芋艿:code review 相关逻辑 @Schema(description = "用户 APP - 重置密码 Request VO") @Data @NoArgsConstructor @AllArgsConstructor @Builder -public class AppAuthResetPasswordReqVO { +public class AppMemberUserResetPasswordReqVO { @Schema(description = "新密码", requiredMode = Schema.RequiredMode.REQUIRED, example = "buzhidao") @NotEmpty(message = "新密码不能为空") diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/convert/auth/AuthConvert.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/convert/auth/AuthConvert.java index 359e3b904..16a8e6d4c 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/convert/auth/AuthConvert.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/convert/auth/AuthConvert.java @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.member.convert.auth; import cn.iocoder.yudao.module.member.controller.app.auth.vo.*; import cn.iocoder.yudao.module.member.controller.app.social.vo.AppSocialUserUnbindReqVO; +import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserResetPasswordReqVO; import cn.iocoder.yudao.module.system.api.oauth2.dto.OAuth2AccessTokenRespDTO; import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeSendReqDTO; import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeUseReqDTO; @@ -21,7 +22,7 @@ public interface AuthConvert { SocialUserUnbindReqDTO convert(Long userId, Integer userType, AppSocialUserUnbindReqVO reqVO); SmsCodeSendReqDTO convert(AppAuthSmsSendReqVO reqVO); - SmsCodeUseReqDTO convert(AppAuthResetPasswordReqVO reqVO, SmsSceneEnum scene, String usedIp); + SmsCodeUseReqDTO convert(AppMemberUserResetPasswordReqVO reqVO, SmsSceneEnum scene, String usedIp); SmsCodeUseReqDTO convert(AppAuthSmsLoginReqVO reqVO, Integer scene, String usedIp); AppAuthLoginRespVO convert(OAuth2AccessTokenRespDTO bean); diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthService.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthService.java index 7c3bc5da7..c35754e7d 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthService.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthService.java @@ -61,12 +61,6 @@ public interface MemberAuthService { */ String getSocialAuthorizeUrl(Integer type, String redirectUri); - /** - * 忘记密码 - * @param userReqVO 用户请求实体类 - */ - void resetPassword(AppAuthResetPasswordReqVO userReqVO); - /** * 给用户发送短信验证码 * diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthServiceImpl.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthServiceImpl.java index 83a4bae65..b8cae96ff 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthServiceImpl.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthServiceImpl.java @@ -27,7 +27,6 @@ import cn.iocoder.yudao.module.system.enums.oauth2.OAuth2ClientConstants; import cn.iocoder.yudao.module.system.enums.sms.SmsSceneEnum; import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum; import lombok.extern.slf4j.Slf4j; -import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -61,8 +60,6 @@ public class MemberAuthServiceImpl implements MemberAuthService { @Resource private WxMaService wxMaService; - @Resource - private PasswordEncoder passwordEncoder; @Resource private MemberUserMapper userMapper; @@ -208,29 +205,22 @@ public class MemberAuthServiceImpl implements MemberAuthService { createLogoutLog(accessTokenRespDTO.getUserId()); } - @Override - public void resetPassword(AppAuthResetPasswordReqVO reqVO) { - // 检验用户是否存在 - MemberUserDO userDO = checkUserIfExists(reqVO.getMobile()); - - // 使用验证码 - smsCodeApi.useSmsCode(AuthConvert.INSTANCE.convert(reqVO, SmsSceneEnum.MEMBER_FORGET_PASSWORD, - getClientIP())); - - // 更新密码 - userMapper.updateById(MemberUserDO.builder().id(userDO.getId()) - .password(passwordEncoder.encode(reqVO.getPassword())).build()); - } - @Override public void sendSmsCode(Long userId, AppAuthSmsSendReqVO reqVO) { - // 如果是修改手机场景,需要校验新手机号是否已经注册,说明不能使用该手机了 + // 情况 1:如果是修改手机场景,需要校验新手机号是否已经注册,说明不能使用该手机了 if (Objects.equals(reqVO.getScene(), SmsSceneEnum.MEMBER_UPDATE_MOBILE.getScene())) { - MemberUserDO user = userMapper.selectByMobile(reqVO.getMobile()); + MemberUserDO user = userService.getUserByMobile(reqVO.getMobile()); if (user != null && !Objects.equals(user.getId(), userId)) { throw exception(AUTH_MOBILE_USED); } } + // 情况 2:如果是重置密码场景,需要校验手机号是存在的 + if (Objects.equals(reqVO.getScene(), SmsSceneEnum.MEMBER_RESET_PASSWORD.getScene())) { + MemberUserDO user= userService.getUserByMobile(reqVO.getMobile()); + if (user == null) { + throw exception(USER_MOBILE_NOT_EXISTS); + } + } // 执行发送 smsCodeApi.sendSmsCode(AuthConvert.INSTANCE.convert(reqVO).setCreateIp(getClientIP())); @@ -248,14 +238,6 @@ public class MemberAuthServiceImpl implements MemberAuthService { return AuthConvert.INSTANCE.convert(accessTokenDO); } - public MemberUserDO checkUserIfExists(String mobile) { - MemberUserDO user = userMapper.selectByMobile(mobile); - if (user == null) { - throw exception(USER_NOT_EXISTS); - } - return user; - } - private void createLogoutLog(Long userId) { LoginLogCreateReqDTO reqDTO = new LoginLogCreateReqDTO(); reqDTO.setLogType(LoginLogTypeEnum.LOGOUT_SELF.getType()); diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserService.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserService.java index 891a514cf..b48acc0d1 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserService.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserService.java @@ -4,6 +4,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.validation.Mobile; import cn.iocoder.yudao.module.member.controller.admin.user.vo.MemberUserPageReqVO; import cn.iocoder.yudao.module.member.controller.admin.user.vo.MemberUserUpdateReqVO; +import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserResetPasswordReqVO; import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserUpdatePasswordReqVO; import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserUpdateReqVO; import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserUpdateMobileReqVO; @@ -94,6 +95,13 @@ public interface MemberUserService { */ void updateUserPassword(Long userId, AppMemberUserUpdatePasswordReqVO reqVO); + /** + * 【会员】忘记密码 + * + * @param reqVO 请求信息 + */ + void resetUserPassword(AppMemberUserResetPasswordReqVO reqVO); + /** * 判断密码是否匹配 * diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImpl.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImpl.java index 7ac1bd840..b1ab98ac2 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImpl.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImpl.java @@ -7,9 +7,11 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.module.infra.api.file.FileApi; import cn.iocoder.yudao.module.member.controller.admin.user.vo.MemberUserPageReqVO; import cn.iocoder.yudao.module.member.controller.admin.user.vo.MemberUserUpdateReqVO; +import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserResetPasswordReqVO; import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserUpdatePasswordReqVO; import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserUpdateReqVO; import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserUpdateMobileReqVO; +import cn.iocoder.yudao.module.member.convert.auth.AuthConvert; import cn.iocoder.yudao.module.member.convert.user.MemberUserConvert; import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO; import cn.iocoder.yudao.module.member.dal.mysql.user.MemberUserMapper; @@ -141,6 +143,28 @@ public class MemberUserServiceImpl implements MemberUserService { .password(passwordEncoder.encode(reqVO.getPassword())).build()); } + @Override + public void resetUserPassword(AppMemberUserResetPasswordReqVO reqVO) { + // 检验用户是否存在 + MemberUserDO user = validateUserExists(reqVO.getMobile()); + + // 使用验证码 + smsCodeApi.useSmsCode(AuthConvert.INSTANCE.convert(reqVO, SmsSceneEnum.MEMBER_RESET_PASSWORD, + getClientIP())); + + // 更新密码 + memberUserMapper.updateById(MemberUserDO.builder().id(user.getId()) + .password(passwordEncoder.encode(reqVO.getPassword())).build()); + } + + private MemberUserDO validateUserExists(String mobile) { + MemberUserDO user = memberUserMapper.selectByMobile(mobile); + if (user == null) { + throw exception(USER_MOBILE_NOT_EXISTS); + } + return user; + } + @Override public boolean isPasswordMatch(String rawPassword, String encodedPassword) { return passwordEncoder.matches(rawPassword, encodedPassword); diff --git a/yudao-module-member/yudao-module-member-biz/src/test/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthServiceTest.java b/yudao-module-member/yudao-module-member-biz/src/test/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthServiceTest.java index 2003ce9b5..48bb0f539 100644 --- a/yudao-module-member/yudao-module-member-biz/src/test/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthServiceTest.java +++ b/yudao-module-member/yudao-module-member-biz/src/test/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthServiceTest.java @@ -5,7 +5,7 @@ import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils; import cn.iocoder.yudao.framework.redis.config.YudaoRedisAutoConfiguration; import cn.iocoder.yudao.framework.test.core.ut.BaseDbAndRedisUnitTest; -import cn.iocoder.yudao.module.member.controller.app.auth.vo.AppAuthResetPasswordReqVO; +import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserResetPasswordReqVO; import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserUpdatePasswordReqVO; import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO; import cn.iocoder.yudao.module.member.dal.mysql.user.MemberUserMapper; @@ -101,7 +101,7 @@ public class MemberAuthServiceTest extends BaseDbAndRedisUnitTest { when(passwordEncoder.encode(password)).thenReturn(password); // 更新用户密码 - AppAuthResetPasswordReqVO reqVO = new AppAuthResetPasswordReqVO(); + AppMemberUserResetPasswordReqVO reqVO = new AppMemberUserResetPasswordReqVO(); reqVO.setMobile(userDO.getMobile()); reqVO.setPassword(password); reqVO.setCode(code); diff --git a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/sms/SmsSceneEnum.java b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/sms/SmsSceneEnum.java index 5d11c0d7b..fc3a0f3ee 100644 --- a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/sms/SmsSceneEnum.java +++ b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/sms/SmsSceneEnum.java @@ -19,7 +19,7 @@ public enum SmsSceneEnum implements IntArrayValuable { MEMBER_LOGIN(1, "user-sms-login", "会员用户 - 手机号登陆"), MEMBER_UPDATE_MOBILE(2, "user-update-mobile", "会员用户 - 修改手机"), MEMBER_UPDATE_PASSWORD(3, "user-update-mobile", "会员用户 - 修改密码"), - MEMBER_FORGET_PASSWORD(4, "user-forget-password", "会员用户 - 忘记密码"), + MEMBER_RESET_PASSWORD(4, "user-reset-password", "会员用户 - 忘记密码"), ADMIN_MEMBER_LOGIN(21, "admin-sms-login", "后台用户 - 手机号登录");