Files
UnitedAirforce fceb34944a Update
2025-11-28 06:54:41 +08:00

167 lines
6.2 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<title>User Information</title>
<!-- Include Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
background-color: #000000;
}
#welcome-title {
color: #ffffff;
font-family: Arial, sans-serif;
margin-top: 20px;
}
</style>
</head>
<body>
<div class="container mt-5">
<div class="text-center">
<h1 id="welcome-title">Hello, {username}</h1>
<!-- Export Data Button -->
<button id="export-button" class="btn btn-primary mt-3" onclick="openExportModal()">Export Data</button>
</div>
</div>
<!-- Export Save Data Modal -->
<div class="modal fade" id="exportModal" tabindex="-1" aria-labelledby="exportModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exportModalLabel">Export Save Data</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p>You can export your save data to back it up or transfer it to another server.</p>
<p id="next-export-time"></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" id="export-data-button" onclick="exportSaveData()">Export Data</button>
</div>
</div>
</div>
</div>
<script>
let nextSaveExportTimestamp = 0;
function getCookie(name) {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) return parts.pop().split(';').shift();
}
async function fetchUserData() {
const token = getCookie("token");
if (!token) {
console.error("Token not found in cookies.");
return;
}
try {
const response = await fetch("/usercenter/api", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
token: token,
action: "basic"
})
});
if (!response.ok) {
console.error("Failed to fetch user data:", response.statusText);
return;
}
// Parse the response data
const response_data = await response.json();
const username = response_data.data.username || "User";
nextSaveExportTimestamp = response_data.data.next_save_export || 0;
document.getElementById("welcome-title").textContent = `Hello, ${username}`;
} catch (error) {
console.error("Error fetching user data:", error);
}
}
async function openExportModal() {
const nextExportTime = new Date(nextSaveExportTimestamp * 1000); // Convert UNIX timestamp to milliseconds
const now = new Date();
// Check if the export is available
if (nextSaveExportTimestamp > Math.floor(now.getTime() / 1000)) {
const timeRemaining = Math.ceil((nextSaveExportTimestamp - now.getTime() / 1000) / 60);
document.getElementById("next-export-time").textContent = `You can export your data again in approximately ${timeRemaining} minutes.`;
document.getElementById("export-data-button").disabled = true; // Disable the button if export is not available
} else {
document.getElementById("next-export-time").textContent = "You can export your data now.";
document.getElementById("export-data-button").disabled = false; // Enable the button
}
// Show the modal
const exportModal = new bootstrap.Modal(document.getElementById("exportModal"));
exportModal.show();
}
async function exportSaveData() {
const token = getCookie("token");
if (!token) {
console.error("Token not found in cookies.");
return;
}
try {
const response = await fetch("/usercenter/export_data", {
method: "GET",
headers: {
"Content-Type": "application/json"
}
});
if (!response.ok) {
console.error("Failed to export data:", response.statusText);
return;
}
// Check if the response is JSON or a file stream
const contentType = response.headers.get("Content-Type");
if (contentType && contentType.includes("application/json")) {
const responseData = await response.json();
if (responseData.status === "failed") {
alert(responseData.message); // Show the failure message
}
} else {
// Handle file download
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "save_export.xlsx"; // Default filename
document.body.appendChild(a);
a.click();
a.remove();
const exportModal = bootstrap.Modal.getInstance(document.getElementById("exportModal"));
if (exportModal) {
exportModal.hide();
}
fetchUserData();
}
} catch (error) {
console.error("Error exporting save data:", error);
}
}
document.addEventListener("DOMContentLoaded", fetchUserData);
</script>
</body>
</html>