增加修改个人信息和修改密码菜单。

This commit is contained in:
2026-08-25 23:54:42 +08:00
parent 16ba817ecf
commit 20395a79a2
5 changed files with 189 additions and 12 deletions
+39
View File
@@ -33,6 +33,45 @@ body {
.admin-header #username {
margin-left: auto;
}
.user-menu-wrapper {
position: relative;
margin-left: auto;
}
.user-menu-wrapper #username {
margin-left: 0;
}
.user-menu {
position: absolute;
top: calc(100% + 4px);
right: 0;
z-index: 1100;
display: none;
min-width: 140px;
padding: 4px 0;
background: #fff;
border: 1px solid #e8e8e8;
border-radius: 4px;
box-shadow: 0 3px 12px rgba(0,0,0,0.16);
}
.user-menu-wrapper:hover .user-menu,
.user-menu-wrapper:focus-within .user-menu {
display: block;
}
.user-menu button {
display: block;
width: 100%;
padding: 9px 14px;
border: 0;
background: transparent;
color: #333;
text-align: left;
cursor: pointer;
font-size: 13px;
}
.user-menu button:hover {
background: #f5f5f5;
color: #1890ff;
}
.admin-header span {
background: transparent;
padding: 5px 12px;
+82
View File
@@ -108,6 +108,8 @@ document.addEventListener('DOMContentLoaded', () => {
bindForm('modal-form', handleModalSubmit);
bindForm('user-form', handleUserSubmit);
bindForm('settings-form', handleSettingsSubmit);
bindForm('profile-form', handleProfileSubmit);
bindForm('password-form', handlePasswordSubmit);
// 封面图上传
const coverFileInput = document.getElementById('post-cover-file');
@@ -122,6 +124,86 @@ document.addEventListener('DOMContentLoaded', () => {
}
});
// 显示个人信息弹窗
function showProfileModal() {
document.getElementById('profile-nickname').value = currentUser?.nickname || '';
document.getElementById('profile-email').value = currentUser?.email || '';
document.getElementById('profile-avatar').value = currentUser?.avatar || '';
document.getElementById('profile-modal').classList.remove('hidden');
}
function hideProfileModal() {
document.getElementById('profile-modal').classList.add('hidden');
}
// 保存个人信息
async function handleProfileSubmit(e) {
e.preventDefault();
const body = {
nickname: document.getElementById('profile-nickname').value.trim(),
email: document.getElementById('profile-email').value.trim(),
avatar: document.getElementById('profile-avatar').value.trim()
};
try {
const res = await fetch(`${API_BASE}/auth/me`, {
method: 'PUT',
headers: getHeaders(),
body: JSON.stringify(body)
});
const data = await res.json().catch(() => ({}));
if (!res.ok) {
alert(data.error || '保存失败');
return;
}
currentUser = Object.assign({}, currentUser, body, data.data || {});
document.getElementById('username').textContent = currentUser.nickname || currentUser.username;
hideProfileModal();
alert('个人信息已保存');
} catch (err) {
alert('保存失败: ' + err.message);
}
}
// 显示修改密码弹窗
function showPasswordModal() {
document.getElementById('password-form').reset();
document.getElementById('password-modal').classList.remove('hidden');
}
function hidePasswordModal() {
document.getElementById('password-modal').classList.add('hidden');
}
// 修改密码
async function handlePasswordSubmit(e) {
e.preventDefault();
const oldPassword = document.getElementById('old-password').value;
const newPassword = document.getElementById('new-password').value;
const confirmPassword = document.getElementById('confirm-password').value;
if (newPassword !== confirmPassword) {
alert('两次输入的新密码不一致');
return;
}
try {
const res = await fetch(`${API_BASE}/auth/password`, {
method: 'PUT',
headers: getHeaders(),
body: JSON.stringify({ old_password: oldPassword, new_password: newPassword })
});
const data = await res.json().catch(() => ({}));
if (!res.ok) {
alert(data.error || '修改密码失败');
return;
}
hidePasswordModal();
alert(data.message || '密码修改成功');
} catch (err) {
alert('修改密码失败: ' + err.message);
}
}
// 安全绑定表单提交(元素不存在时忽略)
function bindForm(formId, handler) {
const form = document.getElementById(formId);