更新用户驱动配置功能,添加更新用户驱动的API接口,优化前端JSON编辑器的交互逻辑,确保用户驱动信息的正确更新和错误处理。

This commit is contained in:
dockermen 2025-04-10 23:50:40 +08:00
parent 18c44797b1
commit 1f86d12fad
3 changed files with 62 additions and 4 deletions

Binary file not shown.

10
main.py
View File

@ -116,7 +116,6 @@ def admin():
db = get_db() db = get_db()
providers = db.get_all_drive_providers() providers = db.get_all_drive_providers()
alluser_drives = db.get_all_user_drives() alluser_drives = db.get_all_user_drives()
print(alluser_drives)
return render_template('admin.html',providers=providers,alluser_drives=alluser_drives) return render_template('admin.html',providers=providers,alluser_drives=alluser_drives)
@ -156,7 +155,6 @@ def drive_provider(metfunc):
""" """
body = request.get_json() body = request.get_json()
print(data)
status = db.add_drive_provider(body.get("provider_name","测试网盘"),body.get("config_vars"),body.get("remarks","测试网盘")) status = db.add_drive_provider(body.get("provider_name","测试网盘"),body.get("config_vars"),body.get("remarks","测试网盘"))
if status: if status:
@ -177,6 +175,14 @@ def user_drive(metfunc):
if status: if status:
data["status"] = True data["status"] = True
data["data"] = body data["data"] = body
elif metfunc == "update":
body = request.get_json()
print(body)
print(body.get("id"),body.get("login_config"))
status = db.update_user_drive(body.get("id"),json.loads(body.get("login_config")),body.get("remarks","测试网盘"))
if status:
data["status"] = True
data["data"] = body
return data return data

View File

@ -596,9 +596,10 @@
if (selectedtid) { if (selectedtid) {
let defaultJson = {}; let defaultJson = {};
const user_drives = JSON.parse('{{ alluser_drives | tojson | safe }}'); // Use safe filter // 解析返回的JSON字符串为JavaScript对象
const allUserDrivesStr = '{{ alluser_drives | tojson | safe }}';
const user_drives = JSON.parse(allUserDrivesStr); // Use safe filter
const selectedUdrive = user_drives.find(user_drive => user_drive.id == selectedtid); const selectedUdrive = user_drives.find(user_drive => user_drive.id == selectedtid);
if (selectedUdrive && selectedUdrive.login_config) { if (selectedUdrive && selectedUdrive.login_config) {
defaultJson = selectedUdrive.login_config; defaultJson = selectedUdrive.login_config;
} else { } else {
@ -609,9 +610,60 @@
}; };
} }
editor.setValue(JSON.stringify(defaultJson, null, 2), -1); editor.setValue(JSON.stringify(defaultJson, null, 2), -1);
// Store the selected ID in a data attribute for the save button to access
document.querySelector('#editudriveModal #saveAccount').setAttribute('data-tid', selectedtid);
} }
}); });
}); });
// 编辑用户驱动保存事件
$('#editudriveModal #saveAccount').on('click', function() {
const editModalEditorArea = document.querySelector('#editudriveModal .json-editor-area');
const editorElement = editModalEditorArea.querySelector('.ace-editor');
const editor = ace.edit(editorElement);
const configJson = editor.getValue();
const tid = this.getAttribute('data-tid');
try {
// 验证JSON格式
JSON.parse(configJson);
// 发送数据到服务器
$.ajax({
url: '/admin/user_drive/update',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({
id: tid,
login_config: configJson
}),
success: function(response) {
console.log(response);
if (response.status) {
showMessage('配置更新成功', 'success');
// 清空并关闭模态框
editor.setValue(configJson, -1);
$('#editudriveModal').modal('hide');
// 如果需要,可以在这里刷新页面或表格数据
// 局部刷新表格内容
} else {
showMessage('配置更新失败: ' + response.message, 'error');
}
},
error: function(error) {
console.error('更新配置出错:', error);
showMessage('更新配置时发生错误,请查看控制台', 'error');
}
});
} catch (e) {
showMessage('无效的JSON格式: ' + e.message, 'error');
const errorDisplay = editModalEditorArea.closest('.mb-3').querySelector('.json-error');
errorDisplay.textContent = '无效的JSON格式: ' + e.message;
errorDisplay.style.display = 'block';
editorElement.classList.add('border-danger');
}
});
</script> </script>
</body> </body>
</html> </html>