diff --git a/main.ts b/main.ts index d6d18a0..323bd57 100644 --- a/main.ts +++ b/main.ts @@ -552,10 +552,9 @@ const html = ` tbody.innerHTML = '加载中...'; - // 直接使用相对路径 - 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 = ` tbody.innerHTML = ''; if (!data || !Array.isArray(data)) { - tbody.innerHTML = '加载失败'; + console.error('Invalid data format:', data); + tbody.innerHTML = '数据格式错误'; 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 = '暂无数据'; + 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 = '加载失败: ' + error.message + ''; + showToast('加载价格数据失败', 'danger'); }); }