feat(user): 添加用户修改密码功能
- 在前端 HomePage 组件中添加修改密码对话框 - 在 API 中添加 updatePassword 接口 - 在后端 UserController 中添加密码更新接口 - 在 UserService 中添加 updatePassword 方法 - 实现密码更新逻辑,包括旧密码验证和新密码加密
This commit is contained in:
@@ -150,3 +150,8 @@ export const toggleRegistration = (enabled) => {
|
||||
export const generateRegistrationCode = () => {
|
||||
return axiosApi.post('/system/registration/generate-code');
|
||||
};
|
||||
|
||||
// 更新密码
|
||||
export const updatePassword = (data) => {
|
||||
return axiosApi.put('/api/user/password', data);
|
||||
};
|
||||
|
||||
@@ -88,6 +88,7 @@
|
||||
<div v-if="userStore.isLoggedIn" class="user-actions">
|
||||
<span class="welcome-text">欢迎, {{ userStore.userInfo?.username }}</span>
|
||||
<el-button type="danger" @click="handleLogout">退出</el-button>
|
||||
<el-button type="primary" @click="showUpdatePasswordDialog = true">修改密码</el-button>
|
||||
<el-button type="warning" @click="showSystemSettingsDialog = true">系统管理</el-button>
|
||||
<el-button type="primary" @click="showCreateNoteDialog = true">新建笔记</el-button>
|
||||
<el-upload
|
||||
@@ -226,6 +227,25 @@
|
||||
<el-button @click="showSystemSettingsDialog = false">关闭</el-button>
|
||||
</template>
|
||||
</el-dialog>
|
||||
|
||||
<!-- 修改密码对话框 -->
|
||||
<el-dialog v-model="showUpdatePasswordDialog" title="修改密码" width="400px" @close="resetUpdatePasswordForm">
|
||||
<el-form :model="updatePasswordForm" :rules="updatePasswordFormRules" ref="updatePasswordFormRef" label-width="100px">
|
||||
<el-form-item label="旧密码" prop="oldPassword">
|
||||
<el-input v-model="updatePasswordForm.oldPassword" type="password" show-password autocomplete="off"></el-input>
|
||||
</el-form-item>
|
||||
<el-form-item label="新密码" prop="newPassword">
|
||||
<el-input v-model="updatePasswordForm.newPassword" type="password" show-password autocomplete="off"></el-input>
|
||||
</el-form-item>
|
||||
<el-form-item label="确认新密码" prop="confirmPassword">
|
||||
<el-input v-model="updatePasswordForm.confirmPassword" type="password" show-password autocomplete="off"></el-input>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
<template #footer>
|
||||
<el-button @click="showUpdatePasswordDialog = false">取消</el-button>
|
||||
<el-button type="primary" @click="handleUpdatePassword">确定</el-button>
|
||||
</template>
|
||||
</el-dialog>
|
||||
</el-main>
|
||||
</el-container>
|
||||
</el-container>
|
||||
@@ -251,7 +271,8 @@ import {
|
||||
validateToken,
|
||||
getRegistrationStatus,
|
||||
toggleRegistration,
|
||||
generateRegistrationCode
|
||||
generateRegistrationCode,
|
||||
updatePassword
|
||||
} from '@/api/CommonApi.js'
|
||||
import { Plus, Fold, Expand, Folder, Document, Search, Edit, Delete, ArrowDown, Clock } from "@element-plus/icons-vue";
|
||||
import { useUserStore } from '../stores/user';
|
||||
@@ -282,6 +303,7 @@ const currentGroupName = ref('');
|
||||
const showSystemSettingsDialog = ref(false);
|
||||
const isRegistrationEnabled = ref(true);
|
||||
const generatedCode = ref('');
|
||||
const showUpdatePasswordDialog = ref(false);
|
||||
|
||||
const groupFormRef = ref(null);
|
||||
const newGroupForm = ref({ name: '', parentId: null });
|
||||
@@ -302,6 +324,27 @@ const noteFormRules = ref({
|
||||
groupingId: [{ required: true, message: '请选择分类', trigger: 'change' }],
|
||||
});
|
||||
|
||||
const updatePasswordFormRef = ref(null);
|
||||
const updatePasswordForm = ref({
|
||||
oldPassword: '',
|
||||
newPassword: '',
|
||||
confirmPassword: ''
|
||||
});
|
||||
const validateConfirmPassword = (rule, value, callback) => {
|
||||
if (value === '') {
|
||||
callback(new Error('请再次输入新密码'));
|
||||
} else if (value !== updatePasswordForm.value.newPassword) {
|
||||
callback(new Error("两次输入的新密码不一致"));
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
};
|
||||
const updatePasswordFormRules = ref({
|
||||
oldPassword: [{ required: true, message: '请输入旧密码', trigger: 'blur' }],
|
||||
newPassword: [{ required: true, message: '请输入新密码', trigger: 'blur' }, { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }],
|
||||
confirmPassword: [{ required: true, validator: validateConfirmPassword, trigger: 'blur' }]
|
||||
});
|
||||
|
||||
const editData=ref(null)
|
||||
const imageUrls = ref([]);
|
||||
const originalImages = ref([]);
|
||||
@@ -987,6 +1030,32 @@ onMounted(async () => {
|
||||
console.error("Failed to fetch registration status:", error);
|
||||
}
|
||||
});
|
||||
const handleUpdatePassword = async () => {
|
||||
if (!updatePasswordFormRef.value) return;
|
||||
await updatePasswordFormRef.value.validate(async (valid) => {
|
||||
if (valid) {
|
||||
try {
|
||||
await updatePassword({
|
||||
oldPassword: updatePasswordForm.value.oldPassword,
|
||||
newPassword: updatePasswordForm.value.newPassword
|
||||
});
|
||||
ElMessage.success('密码修改成功,请重新登录');
|
||||
showUpdatePasswordDialog.value = false;
|
||||
await handleLogout();
|
||||
} catch (error) {
|
||||
ElMessage.error('密码修改失败: ' + error.message);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const resetUpdatePasswordForm = () => {
|
||||
updatePasswordForm.value = { oldPassword: '', newPassword: '', confirmPassword: '' };
|
||||
if (updatePasswordFormRef.value) {
|
||||
updatePasswordFormRef.value.resetFields();
|
||||
}
|
||||
};
|
||||
|
||||
</script>
|
||||
|
||||
<style>
|
||||
|
||||
Reference in New Issue
Block a user