Files
KaMixitong/static/js/custom.js
2026-03-25 15:24:22 +08:00

70 lines
2.2 KiB
JavaScript
Raw Permalink 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.
// 自定义JavaScript函数
// 设置前端域名全局变量
if (typeof FRONTEND_DOMAIN !== 'undefined') {
window.FRONTEND_DOMAIN = FRONTEND_DOMAIN;
}
// 显示加载动画
function showLoading() {
const loadingElement = document.getElementById('loading');
if (loadingElement) {
loadingElement.style.display = 'block';
}
}
// 隐藏加载动画
function hideLoading() {
const loadingElement = document.getElementById('loading');
if (loadingElement) {
loadingElement.style.display = 'none';
}
}
// 格式化日期
function formatDate(dateString) {
if (!dateString) return '-';
const date = new Date(dateString);
return date.toLocaleDateString('zh-CN') + ' ' + date.toLocaleTimeString('zh-CN');
}
// 格式化文件大小
function formatFileSize(bytes) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
// 显示通知 - 统一的消息弹窗函数
function showNotification(message, type = 'info') {
// 创建通知元素
const alertDiv = document.createElement('div');
const alertType = type === 'error' ? 'danger' : type;
alertDiv.className = `alert alert-${alertType} alert-dismissible fade show`;
alertDiv.innerHTML = `
${message}
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
`;
alertDiv.style.marginBottom = '10px';
// 插入到通知容器中
const container = document.getElementById('notification-container') || document.querySelector('main');
if (container) {
container.insertBefore(alertDiv, container.firstChild);
// 自动隐藏
setTimeout(() => {
if (alertDiv.parentNode) {
alertDiv.remove();
}
}, 5000);
} else {
// 如果找不到容器使用console输出作为备选方案
console.log(`[${type.toUpperCase()}] ${message}`);
}
}
// API请求函数 - 添加认证支持 - REMOVED DUPLICATE
// This function is now defined in base.html to avoid duplication