51 lines
1.8 KiB
JavaScript
51 lines
1.8 KiB
JavaScript
// 保存账号
|
|
function saveAdmin() {
|
|
const adminId = document.getElementById('admin-id').value;
|
|
const formData = {
|
|
username: document.getElementById('username').value.trim(),
|
|
email: document.getElementById('email').value.trim(),
|
|
role: parseInt(document.getElementById('role').value),
|
|
status: parseInt(document.getElementById('status').value)
|
|
};
|
|
|
|
// 获取密码
|
|
const password = document.getElementById('password').value;
|
|
|
|
// 如果是创建账号,必须提供密码
|
|
if (!adminId) {
|
|
if (!password || password.trim() === '') {
|
|
showNotification('密码不能为空', 'warning');
|
|
return;
|
|
}
|
|
formData.password = password;
|
|
} else if (password && password.trim() !== '') {
|
|
// 如果是编辑账号且提供了密码,则更新密码
|
|
formData.password = password;
|
|
}
|
|
|
|
// 如果是创建账号
|
|
const isCreate = !adminId;
|
|
const url = isCreate ? '/api/v1/admins' : `/api/v1/admins/${adminId}`;
|
|
const method = isCreate ? 'POST' : 'PUT';
|
|
|
|
apiRequest(url, {
|
|
method: method,
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify(formData)
|
|
})
|
|
.then(data => {
|
|
if (data.success) {
|
|
showNotification(isCreate ? '账号创建成功' : '账号更新成功', 'success');
|
|
hideAdminModal();
|
|
loadAdmins(currentPage);
|
|
} else {
|
|
showNotification(isCreate ? '账号创建失败: ' + data.message : '账号更新失败: ' + data.message, 'error');
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('Failed to save admin:', error);
|
|
showNotification(isCreate ? '账号创建失败,请查看控制台了解详情' : '账号更新失败,请查看控制台了解详情', 'error');
|
|
});
|
|
} |