From 80530edf6b22345bc71c0ad0f357ba8e6cf57bea Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Mon, 12 Dec 2022 04:01:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BF=AE=E6=94=B9=E5=AF=86?= =?UTF-8?q?=E7=A0=81=E6=8E=A5=E5=8F=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/AccountContextController.java | 15 +++++ .../service/AccountContextService.java | 57 ++++++++++++++++--- .../service/MailAndSmsVerifyService.java | 2 +- .../command/ChangePasswordCommand.java | 11 ++++ .../ChangePasswordWithoutLoginCommand.java | 12 ++++ 5 files changed, 88 insertions(+), 9 deletions(-) create mode 100644 plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/command/ChangePasswordCommand.java create mode 100644 plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/command/ChangePasswordWithoutLoginCommand.java diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/controller/AccountContextController.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/controller/AccountContextController.java index b1aa866..b221879 100644 --- a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/controller/AccountContextController.java +++ b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/controller/AccountContextController.java @@ -3,10 +3,13 @@ package xyz.zhouxy.plusone.system.application.controller; import static xyz.zhouxy.plusone.system.constant.AuthLogic.adminAuthLogic; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import xyz.zhouxy.plusone.system.application.service.AccountContextService; +import xyz.zhouxy.plusone.system.application.service.command.ChangePasswordCommand; +import xyz.zhouxy.plusone.system.application.service.command.ChangePasswordWithoutLoginCommand; import xyz.zhouxy.plusone.util.RestfulResult; /** @@ -37,4 +40,16 @@ public class AccountContextController { var result = service.getMenuTree(); return RestfulResult.success("查询成功", result); } + + @PostMapping("changePassword") + public RestfulResult changePassword(ChangePasswordCommand command) { + service.changePassword(command); + return RestfulResult.success("修改成功,请重新登录。"); + } + + @PostMapping("changePasswordWithoutLogin") + public RestfulResult changePasswordWithoutLogin(ChangePasswordWithoutLoginCommand command) { + service.changePasswordWithoutLogin(command); + return RestfulResult.success("修改成功,请重新登录。"); + } } diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/AccountContextService.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/AccountContextService.java index e6e8996..801e0b3 100644 --- a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/AccountContextService.java +++ b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/AccountContextService.java @@ -1,14 +1,26 @@ package xyz.zhouxy.plusone.system.application.service; -import static xyz.zhouxy.plusone.system.constant.AuthLogic.adminAuthLogic; +import xyz.zhouxy.plusone.system.constant.AuthLogic; import java.util.List; -import org.springframework.stereotype.Service; +import javax.annotation.Resource; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import cn.dev33.satoken.stp.StpLogic; +import xyz.zhouxy.plusone.system.application.common.util.PrincipalUtil; import xyz.zhouxy.plusone.system.application.query.AccountQueries; import xyz.zhouxy.plusone.system.application.query.result.AccountDetails; import xyz.zhouxy.plusone.system.application.query.result.MenuViewObject; +import xyz.zhouxy.plusone.system.application.service.command.ChangePasswordCommand; +import xyz.zhouxy.plusone.system.application.service.command.ChangePasswordWithoutLoginCommand; +import xyz.zhouxy.plusone.system.domain.model.account.Account; +import xyz.zhouxy.plusone.system.domain.model.account.AccountRepository; +import xyz.zhouxy.plusone.system.domain.model.account.Email; +import xyz.zhouxy.plusone.system.domain.model.account.MobilePhone; +import xyz.zhouxy.plusone.system.domain.model.account.Principal; /** * 账号查询本身相关信息 @@ -18,21 +30,50 @@ import xyz.zhouxy.plusone.system.application.query.result.MenuViewObject; @Service public class AccountContextService { - private final AccountQueries accountQueries; - private final MenuManagementService menuManagementService; + private final static StpLogic adminAuthLogic = AuthLogic.adminAuthLogic; - public AccountContextService(AccountQueries accountQueries, MenuManagementService menuManagementService) { - this.accountQueries = accountQueries; - this.menuManagementService = menuManagementService; - } + @Resource + private AccountQueries accountQueries; + @Resource + private MenuManagementService menuManagementService; + @Resource + private AccountRepository accountRepository; + @Resource + private MailAndSmsVerifyService mailAndSmsVerifyService; public AccountDetails getAccountInfo() { + adminAuthLogic.checkLogin(); long accountId = adminAuthLogic.getLoginIdAsLong(); return accountQueries.queryAccountDetails(accountId); } public List getMenuTree() { + adminAuthLogic.checkLogin(); long accountId = adminAuthLogic.getLoginIdAsLong(); return menuManagementService.queryByAccountId(accountId); } + + @Transactional + public void changePassword(ChangePasswordCommand command) { + adminAuthLogic.checkLogin(); + Account account = accountRepository.find(adminAuthLogic.getLoginIdAsLong()); + account.checkPassword(command.getPassword()); + account.changePassword(command.getNewPassword(), command.getPasswordConfirmation()); + accountRepository.save(account); + adminAuthLogic.logout(); + } + + @Transactional + public void changePasswordWithoutLogin(ChangePasswordWithoutLoginCommand command) { + String principal = command.getAccount(); + Principal emailOrMobilePhone = PrincipalUtil.getEmailOrMobilePhone(principal); + + Account account = emailOrMobilePhone instanceof Email + ? accountRepository.findByEmail((Email) emailOrMobilePhone) + : accountRepository.findByMobilePhone((MobilePhone) emailOrMobilePhone); + account.checkPassword(command.getOldPassword()); + account.changePassword(command.getNewPassword(), command.getPasswordConfirmation()); + accountRepository.save(account); + adminAuthLogic.logout(); + } } diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/MailAndSmsVerifyService.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/MailAndSmsVerifyService.java index 0439fcb..03611e5 100644 --- a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/MailAndSmsVerifyService.java +++ b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/MailAndSmsVerifyService.java @@ -107,7 +107,7 @@ public class MailAndSmsVerifyService { } private static String generateCode() { - return RandomUtil.randomString(CODE_LENGTH); + return RandomUtil.randomString(RandomUtil.BASE_NUMBER, CODE_LENGTH); } } diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/command/ChangePasswordCommand.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/command/ChangePasswordCommand.java new file mode 100644 index 0000000..5b33d97 --- /dev/null +++ b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/command/ChangePasswordCommand.java @@ -0,0 +1,11 @@ +package xyz.zhouxy.plusone.system.application.service.command; + +import lombok.Data; +import xyz.zhouxy.plusone.domain.ICommand; + +@Data +public class ChangePasswordCommand implements ICommand { + private String password; + private String newPassword; + private String passwordConfirmation; +} diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/command/ChangePasswordWithoutLoginCommand.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/command/ChangePasswordWithoutLoginCommand.java new file mode 100644 index 0000000..d821819 --- /dev/null +++ b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/command/ChangePasswordWithoutLoginCommand.java @@ -0,0 +1,12 @@ +package xyz.zhouxy.plusone.system.application.service.command; + +import lombok.Data; +import xyz.zhouxy.plusone.domain.ICommand; + +@Data +public class ChangePasswordWithoutLoginCommand implements ICommand { + private String account; + private String oldPassword; + private String newPassword; + private String passwordConfirmation; +}