完成修改密码操作
This commit is contained in:
parent
a4c80982fc
commit
4d04d1317e
@ -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<Void> 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<Void> changePassword(@RequestAttribute(Const.ATTR_USER_ID) int id,
|
||||
@RequestBody @Valid ChangePasswordVO vo){
|
||||
return this.messageHandle(() -> service.changePassword(id, vo));
|
||||
}
|
||||
|
||||
private <T> RestBean<T> messageHandle(Supplier<String> action){
|
||||
String message = action.get();
|
||||
if(message == null)
|
||||
return RestBean.success();
|
||||
else
|
||||
return RestBean.failure(400, message);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
@ -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<Account>, UserDetailsService {
|
||||
@ -16,4 +13,5 @@ public interface AccountService extends IService<Account>, UserDetailsService {
|
||||
String resetConfirm(ConfirmResetVO info);
|
||||
Account findAccountById(int id);
|
||||
String modifyEmail(int id, ModifyEmailVO vo);
|
||||
String changePassword(int id, ChangePasswordVO vo);
|
||||
}
|
||||
|
@ -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<AccountMapper, Account> 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 邮箱
|
||||
|
@ -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()
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@ -76,7 +89,8 @@ const onValidate = (prop, isValid) => valid.value = isValid
|
||||
:prefix-icon="Lock" placeholder="重复新密码" maxlength="16"/>
|
||||
</el-form-item>
|
||||
<div style="text-align: center">
|
||||
<el-button :icon="Switch" type="success" :disabled="!valid">立即重置密码</el-button>
|
||||
<el-button :icon="Switch" @click="resetPassword"
|
||||
type="success" :disabled="!valid">立即重置密码</el-button>
|
||||
</div>
|
||||
</el-form>
|
||||
</card>
|
||||
|
Loading…
x
Reference in New Issue
Block a user