mirror of
https://github.com/woodchen-ink/aimodels-prices.git
synced 2025-07-18 13:41:59 +08:00
Improve price table rendering with robust error handling and logging
This commit is contained in:
parent
fe48d1af9e
commit
fa9ac6789d
206
main.ts
206
main.ts
@ -552,10 +552,9 @@ const html = `<!DOCTYPE html>
|
||||
|
||||
tbody.innerHTML = '<tr><td colspan="11" class="text-center">加载中...</td></tr>';
|
||||
|
||||
// 直接使用相对路径
|
||||
fetch('/api/prices')
|
||||
fetch(API_BASE_URL + '/api/prices')
|
||||
.then(response => {
|
||||
console.log('Response status:', response.status);
|
||||
console.log('Response:', response);
|
||||
if (!response.ok) {
|
||||
throw new Error('HTTP error! status: ' + response.status);
|
||||
}
|
||||
@ -566,111 +565,124 @@ const html = `<!DOCTYPE html>
|
||||
tbody.innerHTML = '';
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
data.forEach(price => {
|
||||
const tr = document.createElement('tr');
|
||||
const safePrice = {
|
||||
...price,
|
||||
input_ratio: price.input_ratio || 1,
|
||||
output_ratio: price.output_ratio || 1,
|
||||
status: price.status || 'pending'
|
||||
};
|
||||
|
||||
const vendorData = vendors[String(safePrice.channel_type)];
|
||||
const currentUser = localStorage.getItem('username');
|
||||
|
||||
// 创建单元格
|
||||
const modelCell = document.createElement('td');
|
||||
modelCell.textContent = safePrice.model;
|
||||
if (data.length === 0) {
|
||||
tbody.innerHTML = '<tr><td colspan="11" class="text-center">暂无数据</td></tr>';
|
||||
return;
|
||||
}
|
||||
|
||||
const billingTypeCell = document.createElement('td');
|
||||
const billingTypeBadge = document.createElement('span');
|
||||
billingTypeBadge.className = 'badge badge-' + safePrice.billing_type;
|
||||
billingTypeBadge.textContent = safePrice.billing_type === 'tokens' ? '按量计费' : '按次计费';
|
||||
billingTypeCell.appendChild(billingTypeBadge);
|
||||
console.log('Processing', data.length, 'price records');
|
||||
data.forEach((price, index) => {
|
||||
try {
|
||||
const tr = document.createElement('tr');
|
||||
const safePrice = {
|
||||
...price,
|
||||
input_ratio: price.input_ratio || 1,
|
||||
output_ratio: price.output_ratio || 1,
|
||||
status: price.status || 'pending'
|
||||
};
|
||||
|
||||
console.log('Processing price record', index + 1, ':', safePrice);
|
||||
|
||||
// 创建单元格
|
||||
const modelCell = document.createElement('td');
|
||||
modelCell.textContent = safePrice.model;
|
||||
|
||||
const vendorCell = document.createElement('td');
|
||||
if (vendorData) {
|
||||
const vendorIcon = document.createElement('img');
|
||||
vendorIcon.src = vendorData.icon;
|
||||
vendorIcon.className = 'vendor-icon';
|
||||
vendorIcon.alt = vendorData.name;
|
||||
vendorIcon.onerror = () => { vendorIcon.style.display = 'none'; };
|
||||
vendorCell.appendChild(vendorIcon);
|
||||
vendorCell.appendChild(document.createTextNode(vendorData.name));
|
||||
} else {
|
||||
vendorCell.textContent = '未知供应商';
|
||||
const billingTypeCell = document.createElement('td');
|
||||
const billingTypeBadge = document.createElement('span');
|
||||
billingTypeBadge.className = 'badge badge-' + safePrice.billing_type;
|
||||
billingTypeBadge.textContent = safePrice.billing_type === 'tokens' ? '按量计费' : '按次计费';
|
||||
billingTypeCell.appendChild(billingTypeBadge);
|
||||
|
||||
const vendorCell = document.createElement('td');
|
||||
if (vendors && vendors[String(safePrice.channel_type)]) {
|
||||
const vendorData = vendors[String(safePrice.channel_type)];
|
||||
const vendorIcon = document.createElement('img');
|
||||
vendorIcon.src = vendorData.icon;
|
||||
vendorIcon.className = 'vendor-icon';
|
||||
vendorIcon.alt = vendorData.name;
|
||||
vendorIcon.onerror = () => { vendorIcon.style.display = 'none'; };
|
||||
vendorCell.appendChild(vendorIcon);
|
||||
vendorCell.appendChild(document.createTextNode(vendorData.name));
|
||||
} else {
|
||||
vendorCell.textContent = '未知供应商';
|
||||
}
|
||||
|
||||
const currencyCell = document.createElement('td');
|
||||
currencyCell.textContent = safePrice.currency;
|
||||
|
||||
const inputPriceCell = document.createElement('td');
|
||||
inputPriceCell.textContent = String(safePrice.input_price);
|
||||
|
||||
const outputPriceCell = document.createElement('td');
|
||||
outputPriceCell.textContent = String(safePrice.output_price);
|
||||
|
||||
const inputRatioCell = document.createElement('td');
|
||||
inputRatioCell.textContent = safePrice.input_ratio.toFixed(4);
|
||||
|
||||
const outputRatioCell = document.createElement('td');
|
||||
outputRatioCell.textContent = safePrice.output_ratio.toFixed(4);
|
||||
|
||||
const sourceCell = document.createElement('td');
|
||||
const sourceLink = document.createElement('a');
|
||||
sourceLink.href = safePrice.price_source;
|
||||
sourceLink.className = 'source-link';
|
||||
sourceLink.target = '_blank';
|
||||
sourceLink.textContent = '查看来源';
|
||||
sourceCell.appendChild(sourceLink);
|
||||
|
||||
const statusCell = document.createElement('td');
|
||||
const statusBadge = document.createElement('span');
|
||||
statusBadge.className = 'badge badge-' + safePrice.status;
|
||||
statusBadge.textContent = getStatusText(safePrice.status);
|
||||
statusCell.appendChild(statusBadge);
|
||||
|
||||
// 添加所有单元格
|
||||
tr.appendChild(modelCell);
|
||||
tr.appendChild(billingTypeCell);
|
||||
tr.appendChild(vendorCell);
|
||||
tr.appendChild(currencyCell);
|
||||
tr.appendChild(inputPriceCell);
|
||||
tr.appendChild(outputPriceCell);
|
||||
tr.appendChild(inputRatioCell);
|
||||
tr.appendChild(outputRatioCell);
|
||||
tr.appendChild(sourceCell);
|
||||
tr.appendChild(statusCell);
|
||||
|
||||
// 只有管理员才添加操作列
|
||||
if (currentUser === 'wood' && safePrice.status === 'pending') {
|
||||
const operationCell = document.createElement('td');
|
||||
|
||||
const approveButton = document.createElement('button');
|
||||
approveButton.className = 'btn btn-success btn-sm me-2';
|
||||
approveButton.textContent = '通过';
|
||||
approveButton.onclick = () => reviewPrice(safePrice.id || '', 'approved');
|
||||
|
||||
const rejectButton = document.createElement('button');
|
||||
rejectButton.className = 'btn btn-danger btn-sm';
|
||||
rejectButton.textContent = '拒绝';
|
||||
rejectButton.onclick = () => reviewPrice(safePrice.id || '', 'rejected');
|
||||
|
||||
operationCell.appendChild(approveButton);
|
||||
operationCell.appendChild(rejectButton);
|
||||
tr.appendChild(operationCell);
|
||||
}
|
||||
|
||||
tbody.appendChild(tr);
|
||||
} catch (error) {
|
||||
console.error('Error processing price record', index + 1, ':', error);
|
||||
}
|
||||
|
||||
const currencyCell = document.createElement('td');
|
||||
currencyCell.textContent = safePrice.currency;
|
||||
|
||||
const inputPriceCell = document.createElement('td');
|
||||
inputPriceCell.textContent = String(safePrice.input_price);
|
||||
|
||||
const outputPriceCell = document.createElement('td');
|
||||
outputPriceCell.textContent = String(safePrice.output_price);
|
||||
|
||||
const inputRatioCell = document.createElement('td');
|
||||
inputRatioCell.textContent = safePrice.input_ratio.toFixed(4);
|
||||
|
||||
const outputRatioCell = document.createElement('td');
|
||||
outputRatioCell.textContent = safePrice.output_ratio.toFixed(4);
|
||||
|
||||
const sourceCell = document.createElement('td');
|
||||
const sourceLink = document.createElement('a');
|
||||
sourceLink.href = safePrice.price_source;
|
||||
sourceLink.className = 'source-link';
|
||||
sourceLink.target = '_blank';
|
||||
sourceLink.textContent = '查看来源';
|
||||
sourceCell.appendChild(sourceLink);
|
||||
|
||||
const statusCell = document.createElement('td');
|
||||
const statusBadge = document.createElement('span');
|
||||
statusBadge.className = 'badge badge-' + safePrice.status;
|
||||
statusBadge.textContent = getStatusText(safePrice.status);
|
||||
statusCell.appendChild(statusBadge);
|
||||
|
||||
// 添加所有单元格
|
||||
tr.appendChild(modelCell);
|
||||
tr.appendChild(billingTypeCell);
|
||||
tr.appendChild(vendorCell);
|
||||
tr.appendChild(currencyCell);
|
||||
tr.appendChild(inputPriceCell);
|
||||
tr.appendChild(outputPriceCell);
|
||||
tr.appendChild(inputRatioCell);
|
||||
tr.appendChild(outputRatioCell);
|
||||
tr.appendChild(sourceCell);
|
||||
tr.appendChild(statusCell);
|
||||
|
||||
// 只有管理员才添加操作列
|
||||
if (currentUser === 'wood' && safePrice.status === 'pending') {
|
||||
const operationCell = document.createElement('td');
|
||||
|
||||
const approveButton = document.createElement('button');
|
||||
approveButton.className = 'btn btn-success btn-sm';
|
||||
approveButton.textContent = '通过';
|
||||
approveButton.onclick = () => reviewPrice(safePrice.id || '', 'approved');
|
||||
|
||||
const rejectButton = document.createElement('button');
|
||||
rejectButton.className = 'btn btn-danger btn-sm';
|
||||
rejectButton.textContent = '拒绝';
|
||||
rejectButton.onclick = () => reviewPrice(safePrice.id || '', 'rejected');
|
||||
|
||||
operationCell.appendChild(approveButton);
|
||||
operationCell.appendChild(rejectButton);
|
||||
tr.appendChild(operationCell);
|
||||
}
|
||||
|
||||
tbody.appendChild(tr);
|
||||
});
|
||||
console.log('Finished loading price table');
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('加载价格数据失败:', error);
|
||||
tbody.innerHTML = '<tr><td colspan="11" class="text-center">加载失败: ' + error.message + '</td></tr>';
|
||||
showToast('加载价格数据失败', 'danger');
|
||||
});
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user