100 lines
2.8 KiB
JavaScript
100 lines
2.8 KiB
JavaScript
// 自定义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];
|
||
}
|
||
|
||
// API请求函数 - 添加认证支持
|
||
function apiRequest(url, options = {}) {
|
||
// 自动构建完整的API URL
|
||
let fullUrl = url;
|
||
|
||
// 检查是否配置了前端域名
|
||
const frontendDomain = window.FRONTEND_DOMAIN || '';
|
||
|
||
if (url.startsWith('/')) {
|
||
// 如果是相对路径,则使用配置的域名或当前主机
|
||
fullUrl = (frontendDomain || window.location.origin) + url;
|
||
} else if (!url.startsWith('http')) {
|
||
// 如果不是完整URL且不以http开头,则添加API前缀
|
||
fullUrl = (frontendDomain || window.location.origin) + '/api/v1/' + url;
|
||
}
|
||
|
||
// 设置默认选项
|
||
const defaultOptions = {
|
||
headers: {
|
||
'Content-Type': 'application/json',
|
||
},
|
||
credentials: 'same-origin' // 确保发送cookies进行认证
|
||
};
|
||
|
||
// 合并选项
|
||
const mergedOptions = {
|
||
...defaultOptions,
|
||
...options,
|
||
headers: {
|
||
...defaultOptions.headers,
|
||
...options.headers
|
||
}
|
||
};
|
||
|
||
// 显示加载动画
|
||
showLoading();
|
||
|
||
// 发起请求
|
||
return fetch(fullUrl, mergedOptions)
|
||
.then(response => {
|
||
// 隐藏加载动画
|
||
hideLoading();
|
||
|
||
// 检查响应状态
|
||
if (response.status === 401) {
|
||
// 未授权,重定向到登录页面
|
||
window.location.href = '/login';
|
||
throw new Error('未授权访问');
|
||
}
|
||
|
||
return response.json().catch(() => ({}));
|
||
})
|
||
.catch(error => {
|
||
// 隐藏加载动画
|
||
hideLoading();
|
||
|
||
// 处理网络错误
|
||
console.error('API请求失败:', error);
|
||
throw error;
|
||
});
|
||
} |