Improve price table rendering with robust error handling and logging

This commit is contained in:
wood chen 2025-02-08 02:19:25 +08:00
parent fe48d1af9e
commit fa9ac6789d

30
main.ts
View File

@ -552,10 +552,9 @@ const html = `<!DOCTYPE html>
tbody.innerHTML = '<tr><td colspan="11" class="text-center">加载中...</td></tr>'; tbody.innerHTML = '<tr><td colspan="11" class="text-center">加载中...</td></tr>';
// 直接使用相对路径 fetch(API_BASE_URL + '/api/prices')
fetch('/api/prices')
.then(response => { .then(response => {
console.log('Response status:', response.status); console.log('Response:', response);
if (!response.ok) { if (!response.ok) {
throw new Error('HTTP error! status: ' + response.status); throw new Error('HTTP error! status: ' + response.status);
} }
@ -566,11 +565,19 @@ const html = `<!DOCTYPE html>
tbody.innerHTML = ''; tbody.innerHTML = '';
if (!data || !Array.isArray(data)) { if (!data || !Array.isArray(data)) {
tbody.innerHTML = '<tr><td colspan="11" class="text-center">加载失败</td></tr>'; console.error('Invalid data format:', data);
tbody.innerHTML = '<tr><td colspan="11" class="text-center">数据格式错误</td></tr>';
return; return;
} }
data.forEach(price => { if (data.length === 0) {
tbody.innerHTML = '<tr><td colspan="11" class="text-center">暂无数据</td></tr>';
return;
}
console.log('Processing', data.length, 'price records');
data.forEach((price, index) => {
try {
const tr = document.createElement('tr'); const tr = document.createElement('tr');
const safePrice = { const safePrice = {
...price, ...price,
@ -579,8 +586,7 @@ const html = `<!DOCTYPE html>
status: price.status || 'pending' status: price.status || 'pending'
}; };
const vendorData = vendors[String(safePrice.channel_type)]; console.log('Processing price record', index + 1, ':', safePrice);
const currentUser = localStorage.getItem('username');
// 创建单元格 // 创建单元格
const modelCell = document.createElement('td'); const modelCell = document.createElement('td');
@ -593,7 +599,8 @@ const html = `<!DOCTYPE html>
billingTypeCell.appendChild(billingTypeBadge); billingTypeCell.appendChild(billingTypeBadge);
const vendorCell = document.createElement('td'); const vendorCell = document.createElement('td');
if (vendorData) { if (vendors && vendors[String(safePrice.channel_type)]) {
const vendorData = vendors[String(safePrice.channel_type)];
const vendorIcon = document.createElement('img'); const vendorIcon = document.createElement('img');
vendorIcon.src = vendorData.icon; vendorIcon.src = vendorData.icon;
vendorIcon.className = 'vendor-icon'; vendorIcon.className = 'vendor-icon';
@ -651,7 +658,7 @@ const html = `<!DOCTYPE html>
const operationCell = document.createElement('td'); const operationCell = document.createElement('td');
const approveButton = document.createElement('button'); const approveButton = document.createElement('button');
approveButton.className = 'btn btn-success btn-sm'; approveButton.className = 'btn btn-success btn-sm me-2';
approveButton.textContent = '通过'; approveButton.textContent = '通过';
approveButton.onclick = () => reviewPrice(safePrice.id || '', 'approved'); approveButton.onclick = () => reviewPrice(safePrice.id || '', 'approved');
@ -666,11 +673,16 @@ const html = `<!DOCTYPE html>
} }
tbody.appendChild(tr); tbody.appendChild(tr);
} catch (error) {
console.error('Error processing price record', index + 1, ':', error);
}
}); });
console.log('Finished loading price table');
}) })
.catch(error => { .catch(error => {
console.error('加载价格数据失败:', error); console.error('加载价格数据失败:', error);
tbody.innerHTML = '<tr><td colspan="11" class="text-center">加载失败: ' + error.message + '</td></tr>'; tbody.innerHTML = '<tr><td colspan="11" class="text-center">加载失败: ' + error.message + '</td></tr>';
showToast('加载价格数据失败', 'danger');
}); });
} }