增加修改个人信息和修改密码菜单。
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user