mirror of
https://github.com/woodchen-ink/aimodels-prices.git
synced 2025-07-18 13:41:59 +08:00
Refactor price loading and initialization with async/await and improved error handling
This commit is contained in:
parent
97e5605991
commit
974b5d581f
61
main.ts
61
main.ts
@ -545,31 +545,38 @@ const html = `<!DOCTYPE html>
|
||||
}
|
||||
|
||||
// 修改加载价格数据函数
|
||||
function loadPrices() {
|
||||
async function loadPrices() {
|
||||
console.log('Starting to load prices...');
|
||||
const priceTable = document.getElementById('priceTable');
|
||||
const tbody = priceTable?.querySelector('tbody');
|
||||
if (!tbody) return;
|
||||
if (!tbody) {
|
||||
console.error('Price table body not found');
|
||||
return;
|
||||
}
|
||||
|
||||
tbody.innerHTML = '<tr><td colspan="11" class="text-center">加载中...</td></tr>';
|
||||
|
||||
fetch(API_BASE_URL + '/api/prices', {
|
||||
try {
|
||||
console.log('Fetching prices from:', API_BASE_URL + '/api/prices');
|
||||
const response = await fetch(API_BASE_URL + '/api/prices', {
|
||||
method: 'GET',
|
||||
mode: 'cors',
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
'Accept': 'application/json'
|
||||
'Accept': 'application/json',
|
||||
'Cache-Control': 'no-cache'
|
||||
}
|
||||
})
|
||||
.then(response => {
|
||||
console.log('Response:', response);
|
||||
});
|
||||
|
||||
console.log('Response status:', response.status);
|
||||
console.log('Response headers:', Object.fromEntries(response.headers.entries()));
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('HTTP error! status: ' + response.status);
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then(data => {
|
||||
|
||||
const data = await response.json();
|
||||
console.log('Received data:', data);
|
||||
tbody.innerHTML = '';
|
||||
|
||||
if (!data || !Array.isArray(data)) {
|
||||
console.error('Invalid data format:', data);
|
||||
@ -583,6 +590,7 @@ const html = `<!DOCTYPE html>
|
||||
}
|
||||
|
||||
console.log('Processing', data.length, 'price records');
|
||||
tbody.innerHTML = '';
|
||||
data.forEach((price, index) => {
|
||||
try {
|
||||
const tr = document.createElement('tr');
|
||||
@ -685,12 +693,11 @@ const html = `<!DOCTYPE html>
|
||||
}
|
||||
});
|
||||
console.log('Finished loading price table');
|
||||
})
|
||||
.catch(error => {
|
||||
} catch (error) {
|
||||
console.error('加载价格数据失败:', error);
|
||||
tbody.innerHTML = '<tr><td colspan="11" class="text-center">加载失败: ' + error.message + '</td></tr>';
|
||||
showToast('加载价格数据失败', 'danger');
|
||||
});
|
||||
showToast('加载价格数据失败: ' + error.message, 'danger');
|
||||
}
|
||||
}
|
||||
|
||||
// 修改提交新价格函数
|
||||
@ -744,19 +751,33 @@ const html = `<!DOCTYPE html>
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化
|
||||
// 修改初始化函数
|
||||
async function init() {
|
||||
console.log('Initializing...');
|
||||
console.log('Initializing application...');
|
||||
try {
|
||||
console.log('Loading vendors...');
|
||||
await loadVendors();
|
||||
console.log('Vendors loaded');
|
||||
console.log('Vendors loaded successfully');
|
||||
|
||||
console.log('Checking login status...');
|
||||
await checkLoginStatus();
|
||||
console.log('Login status checked');
|
||||
|
||||
console.log('Loading prices...');
|
||||
await loadPrices();
|
||||
console.log('Prices loaded');
|
||||
console.log('Prices loaded successfully');
|
||||
} catch (error) {
|
||||
console.error('初始化失败:', error);
|
||||
showToast('初始化失败', 'danger');
|
||||
}
|
||||
}
|
||||
|
||||
// 确保在页面加载完成后执行初始化
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
console.log('DOM loaded, starting initialization...');
|
||||
init();
|
||||
});
|
||||
|
||||
// 修改 Toast 提示函数
|
||||
function showToast(message, type = 'success') {
|
||||
const toast = document.querySelector('.toast');
|
||||
@ -815,8 +836,6 @@ const html = `<!DOCTYPE html>
|
||||
showToast('复制失败', 'danger');
|
||||
}
|
||||
}
|
||||
|
||||
init();
|
||||
</script>
|
||||
</body>
|
||||
</html>`;
|
||||
|
Loading…
x
Reference in New Issue
Block a user