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 34dc0ea14c..9fdf2b83a5 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 @@ -8,10 +8,10 @@ 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; -import io.swagger.v3.oas.annotations.tags.Tag; +import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameters; -import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; import lombok.extern.slf4j.Slf4j; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; @@ -92,14 +92,6 @@ public class AppAuthController { return success(true); } - @PostMapping("/update-password") - @Operation(summary = "修改用户密码", description = "用户修改密码时使用") - @PreAuthenticated - public CommonResult updatePassword(@RequestBody @Valid AppAuthUpdatePasswordReqVO reqVO) { - authService.updatePassword(getLoginUserId(), 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 8dd2160342..0a4aa5b63a 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 @@ -3,8 +3,9 @@ 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.AppUserUpdateMobileReqVO; +import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserUpdateMobileReqVO; 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; @@ -49,10 +50,18 @@ public class AppMemberUserController { @PutMapping("/update-mobile") @Operation(summary = "修改用户手机") @PreAuthenticated - public CommonResult updateUserMobile(@RequestBody @Valid AppUserUpdateMobileReqVO reqVO) { + public CommonResult updateUserMobile(@RequestBody @Valid AppMemberUserUpdateMobileReqVO reqVO) { userService.updateUserMobile(getLoginUserId(), reqVO); return success(true); } + @PutMapping("/update-password") + @Operation(summary = "修改用户密码", description = "用户修改密码时使用") + @PreAuthenticated + public CommonResult updatePassword(@RequestBody @Valid AppMemberUserUpdatePasswordReqVO reqVO) { + userService.updateUserPassword(getLoginUserId(), reqVO); + return success(true); + } + } diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/user/vo/AppUserUpdateMobileReqVO.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/user/vo/AppMemberUserUpdateMobileReqVO.java similarity index 97% rename from yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/user/vo/AppUserUpdateMobileReqVO.java rename to yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/user/vo/AppMemberUserUpdateMobileReqVO.java index ade0afab47..6653506fc4 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/user/vo/AppUserUpdateMobileReqVO.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/user/vo/AppMemberUserUpdateMobileReqVO.java @@ -17,7 +17,7 @@ import javax.validation.constraints.Pattern; @NoArgsConstructor @AllArgsConstructor @Builder -public class AppUserUpdateMobileReqVO { +public class AppMemberUserUpdateMobileReqVO { @Schema(description = "手机验证码", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024") @NotEmpty(message = "手机验证码不能为空") diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/auth/vo/AppAuthUpdatePasswordReqVO.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/user/vo/AppMemberUserUpdatePasswordReqVO.java similarity index 55% rename from yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/auth/vo/AppAuthUpdatePasswordReqVO.java rename to yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/user/vo/AppMemberUserUpdatePasswordReqVO.java index 62e21cb23f..cc78ca8325 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/auth/vo/AppAuthUpdatePasswordReqVO.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/user/vo/AppMemberUserUpdatePasswordReqVO.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 io.swagger.v3.oas.annotations.media.Schema; import lombok.AllArgsConstructor; @@ -7,24 +7,25 @@ import lombok.Data; import lombok.NoArgsConstructor; import org.hibernate.validator.constraints.Length; -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 AppAuthUpdatePasswordReqVO { - - @Schema(description = "用户旧密码", requiredMode = Schema.RequiredMode.REQUIRED, example = "123456") - @NotBlank(message = "旧密码不能为空") - @Length(min = 4, max = 16, message = "密码长度为 4-16 位") - private String oldPassword; +public class AppMemberUserUpdatePasswordReqVO { @Schema(description = "新密码", requiredMode = Schema.RequiredMode.REQUIRED, example = "buzhidao") @NotEmpty(message = "新密码不能为空") @Length(min = 4, max = 16, message = "密码长度为 4-16 位") private String password; + + @Schema(description = "手机验证码", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024") + @NotEmpty(message = "手机验证码不能为空") + @Length(min = 4, max = 6, message = "手机验证码长度为 4-6 位") + @Pattern(regexp = "^[0-9]+$", message = "手机验证码必须都是数字") + private String code; + } 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 17e231da8c..7c3bc5da78 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,13 +61,6 @@ public interface MemberAuthService { */ String getSocialAuthorizeUrl(Integer type, String redirectUri); - /** - * 修改用户密码 - * @param userId 用户id - * @param userReqVO 用户请求实体类 - */ - void updatePassword(Long userId, AppAuthUpdatePasswordReqVO userReqVO); - /** * 忘记密码 * @param 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 a481b2dc6e..83a4bae656 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 @@ -26,7 +26,6 @@ import cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum; 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 com.google.common.annotations.VisibleForTesting; import lombok.extern.slf4j.Slf4j; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; @@ -209,17 +208,6 @@ public class MemberAuthServiceImpl implements MemberAuthService { createLogoutLog(accessTokenRespDTO.getUserId()); } - @Override - public void updatePassword(Long userId, AppAuthUpdatePasswordReqVO reqVO) { - // 检验旧密码 - MemberUserDO userDO = checkOldPassword(userId, reqVO.getOldPassword()); - - // 更新用户密码 - // TODO 芋艿:需要重构到用户模块 - userMapper.updateById(MemberUserDO.builder().id(userDO.getId()) - .password(passwordEncoder.encode(reqVO.getPassword())).build()); - } - @Override public void resetPassword(AppAuthResetPasswordReqVO reqVO) { // 检验用户是否存在 @@ -260,26 +248,6 @@ public class MemberAuthServiceImpl implements MemberAuthService { return AuthConvert.INSTANCE.convert(accessTokenDO); } - /** - * 校验旧密码 - * - * @param id 用户 id - * @param oldPassword 旧密码 - * @return MemberUserDO 用户实体 - */ - @VisibleForTesting - public MemberUserDO checkOldPassword(Long id, String oldPassword) { - MemberUserDO user = userMapper.selectById(id); - if (user == null) { - throw exception(USER_NOT_EXISTS); - } - // 参数:未加密密码,编码后的密码 - if (!passwordEncoder.matches(oldPassword,user.getPassword())) { - throw exception(USER_PASSWORD_FAILED); - } - return user; - } - public MemberUserDO checkUserIfExists(String mobile) { MemberUserDO user = userMapper.selectByMobile(mobile); if (user == null) { 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 d644d35860..891a514cf0 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,8 +4,9 @@ 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.AppMemberUserUpdatePasswordReqVO; import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserUpdateReqVO; -import cn.iocoder.yudao.module.member.controller.app.user.vo.AppUserUpdateMobileReqVO; +import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserUpdateMobileReqVO; import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO; import javax.validation.Valid; @@ -81,9 +82,17 @@ public interface MemberUserService { * 【会员】修改手机 * * @param userId 用户编号 - * @param reqVO 请求实体 + * @param reqVO 请求信息 */ - void updateUserMobile(Long userId, AppUserUpdateMobileReqVO reqVO); + void updateUserMobile(Long userId, AppMemberUserUpdateMobileReqVO reqVO); + + /** + * 【会员】修改密码 + * + * @param userId 用户编号 + * @param reqVO 请求信息 + */ + void updateUserPassword(Long userId, AppMemberUserUpdatePasswordReqVO 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 9d551e23ab..7ac1bd840d 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,8 +7,9 @@ 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.AppMemberUserUpdatePasswordReqVO; import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserUpdateReqVO; -import cn.iocoder.yudao.module.member.controller.app.user.vo.AppUserUpdateMobileReqVO; +import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserUpdateMobileReqVO; 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; @@ -110,7 +111,7 @@ public class MemberUserServiceImpl implements MemberUserService { @Override @Transactional(rollbackFor = Exception.class) - public void updateUserMobile(Long userId, AppUserUpdateMobileReqVO reqVO) { + public void updateUserMobile(Long userId, AppMemberUserUpdateMobileReqVO reqVO) { // 检测用户是否存在 MemberUserDO user = validateUserExists(userId); // 校验新手机是否已经被绑定 @@ -127,6 +128,19 @@ public class MemberUserServiceImpl implements MemberUserService { memberUserMapper.updateById(MemberUserDO.builder().id(userId).mobile(reqVO.getMobile()).build()); } + @Override + public void updateUserPassword(Long userId, AppMemberUserUpdatePasswordReqVO reqVO) { + // 检测用户是否存在 + MemberUserDO user = validateUserExists(userId); + // 校验验证码 + smsCodeApi.useSmsCode(new SmsCodeUseReqDTO().setMobile(user.getMobile()).setCode(reqVO.getCode()) + .setScene(SmsSceneEnum.MEMBER_UPDATE_PASSWORD.getScene()).setUsedIp(getClientIP())); + + // 更新用户密码 + memberUserMapper.updateById(MemberUserDO.builder().id(userId) + .password(passwordEncoder.encode(reqVO.getPassword())).build()); + } + @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 9fd67c9014..2003ce9b50 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 @@ -6,7 +6,7 @@ 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.auth.vo.AppAuthUpdatePasswordReqVO; +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; import cn.iocoder.yudao.module.member.service.user.MemberUserService; @@ -71,7 +71,7 @@ public class MemberAuthServiceTest extends BaseDbAndRedisUnitTest { String newPassword = randomString(); // 请求实体 - AppAuthUpdatePasswordReqVO reqVO = AppAuthUpdatePasswordReqVO.builder() + AppMemberUserUpdatePasswordReqVO reqVO = AppMemberUserUpdatePasswordReqVO.builder() .oldPassword(userDO.getPassword()) .password(newPassword) .build(); diff --git a/yudao-module-member/yudao-module-member-biz/src/test/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImplTest.java b/yudao-module-member/yudao-module-member-biz/src/test/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImplTest.java index dcc081697b..e12c1594e0 100644 --- a/yudao-module-member/yudao-module-member-biz/src/test/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImplTest.java +++ b/yudao-module-member/yudao-module-member-biz/src/test/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImplTest.java @@ -6,7 +6,7 @@ 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.infra.api.file.FileApi; -import cn.iocoder.yudao.module.member.controller.app.user.vo.AppUserUpdateMobileReqVO; +import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserUpdateMobileReqVO; import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO; import cn.iocoder.yudao.module.member.dal.mysql.user.MemberUserMapper; import cn.iocoder.yudao.module.member.service.auth.MemberAuthServiceImpl; @@ -114,7 +114,7 @@ public class MemberUserServiceImplTest extends BaseDbAndRedisUnitTest { // 更新手机号 String newMobile = randomNumbers(11); String newCode = randomNumbers(4); - AppUserUpdateMobileReqVO reqVO = new AppUserUpdateMobileReqVO(); + AppMemberUserUpdateMobileReqVO reqVO = new AppMemberUserUpdateMobileReqVO(); reqVO.setMobile(newMobile); reqVO.setCode(newCode); reqVO.setOldMobile(oldMobile); 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 fb1fc574a6..5d11c0d7bd 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 @@ -17,8 +17,9 @@ import java.util.Arrays; public enum SmsSceneEnum implements IntArrayValuable { MEMBER_LOGIN(1, "user-sms-login", "会员用户 - 手机号登陆"), - MEMBER_UPDATE_MOBILE(2, "user-sms-update-mobile", "会员用户 - 修改手机"), - MEMBER_FORGET_PASSWORD(3, "user-sms-forget-password", "会员用户 - 忘记密码"), + MEMBER_UPDATE_MOBILE(2, "user-update-mobile", "会员用户 - 修改手机"), + MEMBER_UPDATE_PASSWORD(3, "user-update-mobile", "会员用户 - 修改密码"), + MEMBER_FORGET_PASSWORD(4, "user-forget-password", "会员用户 - 忘记密码"), ADMIN_MEMBER_LOGIN(21, "admin-sms-login", "后台用户 - 手机号登录");