MassageRobot_Dobot/UI_next/static/js/sync_user_data.js
2025-05-27 15:46:31 +08:00

143 lines
5.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 同步用户数据功能实现
document.addEventListener('DOMContentLoaded', function() {
const syncUserDataBtn = document.getElementById('sync-user-data-btn');
const syncDataModal = document.getElementById('sync-data-modal');
const syncDataVersions = document.getElementById('sync-data-versions');
const syncConfirmBtn = document.getElementById('sync-confirm-btn');
const syncCancelBtn = document.getElementById('sync-cancel-btn');
// 获取当前版本的全局变量在setting.js中定义
let currentVtxVersion = '';
// 获取当前选择的语言
const getCurrentLang = () => localStorage.getItem("selectedLanguage") || "zh";
// 获取翻译文本
const getTranslation = (key) => {
const lang = getCurrentLang();
if (languageData[lang] && getNestedValue(languageData[lang], key)) {
return getNestedValue(languageData[lang], key);
}
return null; // 返回null表示未找到翻译
};
// 获取当前VortXDB版本
function getCurrentVtxVersion() {
return new Promise((resolve, reject) => {
fetch("/get_vtx_current_version")
.then((response) => response.json())
.then((data) => {
currentVtxVersion = data.current_version;
resolve(data.current_version);
})
.catch((error) => {
console.error("Error fetching current VortXDB version:", error);
reject(error);
});
});
}
// 加载当前版本
getCurrentVtxVersion();
// 打开同步用户数据弹窗
syncUserDataBtn.addEventListener('click', function() {
// 刷新当前版本
getCurrentVtxVersion();
// 清空之前的选项
syncDataVersions.innerHTML = '';
// 从downloaded-vtx-versions复制选项
const downloadedVersions = document.getElementById('downloaded-vtx-versions');
Array.from(downloadedVersions.options).forEach(option => {
const newOption = document.createElement('option');
newOption.value = option.value;
newOption.text = option.text;
syncDataVersions.appendChild(newOption);
});
// 显示弹窗
syncDataModal.style.display = 'flex';
});
// 确认按钮点击事件
syncConfirmBtn.addEventListener('click', function() {
const selectedVersion = syncDataVersions.value;
if (selectedVersion) {
// 发送同步请求到后端,包含当前版本信息
fetch('/sync_user_data', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
version: selectedVersion,
current_version: currentVtxVersion
})
})
.then(response => response.json())
.then(data => {
if (data.error) {
const errorMsg = getTranslation("setting.sync_fail") || '同步失败';
showPopupMessage(`${errorMsg}: ${data.error}`, false);
} else {
const successMsg = getTranslation("setting.sync_success") || data.message || '用户数据同步请求已发送';
showPopupMessage(successMsg, true);
}
})
.catch(error => {
console.error('同步用户数据出错:', error);
const errorMsg = getTranslation("setting.sync_fail") || '同步失败,请稍后重试';
showPopupMessage(errorMsg, false);
});
} else {
showPopupMessage('请选择要同步的版本', false);
}
// 关闭弹窗
syncDataModal.style.display = 'none';
});
// 取消按钮点击事件
syncCancelBtn.addEventListener('click', function() {
syncDataModal.style.display = 'none';
});
// 当用户点击弹窗外部时关闭弹窗
window.addEventListener('click', function(event) {
if (event.target === syncDataModal) {
syncDataModal.style.display = 'none';
}
});
// 显示消息提示的函数
function showPopupMessage(message, isSuccess) {
const popupModal = document.getElementById('popup-modal');
const popupMessage = document.getElementById('popup-message');
const confirmBtn = document.getElementById('confirm-btn');
const cancelBtn = document.getElementById('cancel-btn');
popupMessage.textContent = message;
// 如果是成功消息,不显示确认按钮
if (isSuccess) {
confirmBtn.style.display = 'none';
cancelBtn.style.display = 'none';
// 设置确认按钮的点击事件,关闭弹窗
confirmBtn.onclick = function() {
popupModal.style.display = 'none';
};
} else {
confirmBtn.style.display = 'block';
cancelBtn.style.display = 'none';
confirmBtn.onclick = function() {
popupModal.style.display = 'none';
};
}
popupModal.style.display = 'flex';
}
});