diff --git a/my-project-backend/src/main/java/com/example/controller/AccountController.java b/my-project-backend/src/main/java/com/example/controller/AccountController.java index 09f5631..e7df4bd 100644 --- a/my-project-backend/src/main/java/com/example/controller/AccountController.java +++ b/my-project-backend/src/main/java/com/example/controller/AccountController.java @@ -3,6 +3,7 @@ package com.example.controller; import com.example.entity.RestBean; import com.example.entity.dto.Account; import com.example.entity.dto.AccountDetails; +import com.example.entity.vo.request.ChangePasswordVO; import com.example.entity.vo.request.DetailsSaveVO; import com.example.entity.vo.request.ModifyEmailVO; import com.example.entity.vo.response.AccountDetailsVO; @@ -16,6 +17,7 @@ import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import java.util.Optional; +import java.util.function.Supplier; @Validated @RestController @@ -52,7 +54,20 @@ public class AccountController { @PostMapping("/modify-email") public RestBean modifyEmail(@RequestAttribute(Const.ATTR_USER_ID) int id, @RequestBody @Valid ModifyEmailVO vo){ - String result = service.modifyEmail(id, vo); - return result == null ? RestBean.success() : RestBean.failure(400, result); + return this.messageHandle(() -> service.modifyEmail(id, vo)); + } + + @PostMapping("/change-password") + public RestBean changePassword(@RequestAttribute(Const.ATTR_USER_ID) int id, + @RequestBody @Valid ChangePasswordVO vo){ + return this.messageHandle(() -> service.changePassword(id, vo)); + } + + private RestBean messageHandle(Supplier action){ + String message = action.get(); + if(message == null) + return RestBean.success(); + else + return RestBean.failure(400, message); } } diff --git a/my-project-backend/src/main/java/com/example/entity/vo/request/ChangePasswordVO.java b/my-project-backend/src/main/java/com/example/entity/vo/request/ChangePasswordVO.java new file mode 100644 index 0000000..2eaf415 --- /dev/null +++ b/my-project-backend/src/main/java/com/example/entity/vo/request/ChangePasswordVO.java @@ -0,0 +1,12 @@ +package com.example.entity.vo.request; + +import lombok.Data; +import org.hibernate.validator.constraints.Length; + +@Data +public class ChangePasswordVO { + @Length(min = 6, max = 20) + String password; + @Length(min = 6, max = 20) + String new_password; +} diff --git a/my-project-backend/src/main/java/com/example/service/AccountService.java b/my-project-backend/src/main/java/com/example/service/AccountService.java index 81ff2ee..d604883 100644 --- a/my-project-backend/src/main/java/com/example/service/AccountService.java +++ b/my-project-backend/src/main/java/com/example/service/AccountService.java @@ -2,10 +2,7 @@ package com.example.service; import com.baomidou.mybatisplus.extension.service.IService; import com.example.entity.dto.Account; -import com.example.entity.vo.request.ConfirmResetVO; -import com.example.entity.vo.request.EmailRegisterVO; -import com.example.entity.vo.request.EmailResetVO; -import com.example.entity.vo.request.ModifyEmailVO; +import com.example.entity.vo.request.*; import org.springframework.security.core.userdetails.UserDetailsService; public interface AccountService extends IService, UserDetailsService { @@ -16,4 +13,5 @@ public interface AccountService extends IService, UserDetailsService { String resetConfirm(ConfirmResetVO info); Account findAccountById(int id); String modifyEmail(int id, ModifyEmailVO vo); + String changePassword(int id, ChangePasswordVO vo); } diff --git a/my-project-backend/src/main/java/com/example/service/impl/AccountServiceImpl.java b/my-project-backend/src/main/java/com/example/service/impl/AccountServiceImpl.java index 7b00382..642f277 100644 --- a/my-project-backend/src/main/java/com/example/service/impl/AccountServiceImpl.java +++ b/my-project-backend/src/main/java/com/example/service/impl/AccountServiceImpl.java @@ -3,10 +3,7 @@ package com.example.service.impl; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.example.entity.dto.Account; -import com.example.entity.vo.request.ConfirmResetVO; -import com.example.entity.vo.request.EmailRegisterVO; -import com.example.entity.vo.request.EmailResetVO; -import com.example.entity.vo.request.ModifyEmailVO; +import com.example.entity.vo.request.*; import com.example.mapper.AccountMapper; import com.example.service.AccountService; import com.example.utils.Const; @@ -204,6 +201,18 @@ public class AccountServiceImpl extends ServiceImpl impl return null; } + @Override + public String changePassword(int id, ChangePasswordVO vo) { + String passwd = this.query().eq("id", id).one().getPassword(); + if(!passwordEncoder.matches(vo.getPassword(), passwd)) + return "原密码错误,请重新输入!"; + boolean success = this.update() + .eq("id", id) + .set("password", passwordEncoder.encode(vo.getNew_password())) + .update(); + return success ? null : "未知错误,请联系管理员"; + } + /** * 查询指定邮箱的用户是否已经存在 * @param email 邮箱 diff --git a/my-project-frontend/src/views/settings/PrivacySetting.vue b/my-project-frontend/src/views/settings/PrivacySetting.vue index 5798486..736ceca 100644 --- a/my-project-frontend/src/views/settings/PrivacySetting.vue +++ b/my-project-frontend/src/views/settings/PrivacySetting.vue @@ -2,6 +2,8 @@ import Card from "@/components/Card.vue"; import {Lock, Setting, Switch} from "@element-plus/icons-vue"; import {reactive, ref} from "vue"; +import {post} from "@/net"; +import {ElMessage} from "element-plus"; const form = reactive({ password: '', @@ -36,6 +38,17 @@ const rules = { const formRef = ref() const valid = ref(false) const onValidate = (prop, isValid) => valid.value = isValid + +function resetPassword() { + formRef.value.validate((isValid) => { + if(isValid) { + post('/api/user/change-password', form, () => { + ElMessage.success('密码修改成功!') + formRef.value.resetFields() + }) + } + }) +}