From fe48d1af9ef41d9cf7d7072a2e2118b495dcd1a1 Mon Sep 17 00:00:00 2001 From: wood chen Date: Sat, 8 Feb 2025 02:16:44 +0800 Subject: [PATCH] Refactor frontend and backend price data loading with improved error handling and logging --- main.ts | 67 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/main.ts b/main.ts index 40762aa..d6d18a0 100644 --- a/main.ts +++ b/main.ts @@ -552,16 +552,17 @@ const html = ` tbody.innerHTML = '加载中...'; - fetch(API_BASE_URL + '/api/prices', { - credentials: 'include' - }) + // 直接使用相对路径 + fetch('/api/prices') .then(response => { + console.log('Response status:', response.status); if (!response.ok) { throw new Error('HTTP error! status: ' + response.status); } return response.json(); }) .then(data => { + console.log('Received data:', data); tbody.innerHTML = ''; if (!data || !Array.isArray(data)) { @@ -670,7 +671,6 @@ const html = ` .catch(error => { console.error('加载价格数据失败:', error); tbody.innerHTML = '加载失败: ' + error.message + ''; - showToast('加载价格数据失败', 'danger'); }); } @@ -727,11 +727,15 @@ const html = ` // 初始化 async function init() { - await Promise.all([ - checkLoginStatus(), - loadVendors() - ]); - loadPrices(); + console.log('Initializing...'); + try { + await loadVendors(); + console.log('Vendors loaded'); + await loadPrices(); + console.log('Prices loaded'); + } catch (error) { + console.error('初始化失败:', error); + } } // 修改 Toast 提示函数 @@ -845,7 +849,7 @@ function validateData(data: any): string | null { // 修改处理函数 async function handler(req: Request): Promise { const headers = { - "Access-Control-Allow-Origin": "https://aimodels-price.deno.dev", + "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "GET, POST, OPTIONS, PUT, DELETE", "Access-Control-Allow-Headers": "Content-Type, Cookie, Authorization", "Access-Control-Allow-Credentials": "true", @@ -864,7 +868,8 @@ async function handler(req: Request): Promise { try { const url = new URL(req.url); - + console.log('Received request:', req.method, url.pathname); + // 处理预检请求 if (req.method === "OPTIONS") { return new Response(null, { @@ -873,6 +878,27 @@ async function handler(req: Request): Promise { }); } + // 获取价格列表 + if (url.pathname === "/api/prices" && req.method === "GET") { + try { + console.log('Reading prices from KV store...'); + const prices = await readPrices(); + console.log('Prices read successfully:', prices.length); + return new Response(JSON.stringify(prices), { + headers: jsonHeaders + }); + } catch (error) { + console.error('获取价格列表失败:', error); + return new Response(JSON.stringify({ + error: "获取价格列表失败", + details: error.message + }), { + status: 500, + headers: jsonHeaders + }); + } + } + // 认证状态检查 if (url.pathname === "/api/auth/status") { try { @@ -1152,25 +1178,6 @@ async function handler(req: Request): Promise { } } - // 获取价格列表 - if (url.pathname === "/api/prices" && req.method === "GET") { - try { - const prices = await readPrices(); - return new Response(JSON.stringify(prices), { - headers: jsonHeaders - }); - } catch (error) { - console.error('获取价格列表失败:', error); - return new Response(JSON.stringify({ - error: "获取价格列表失败", - details: error.message - }), { - status: 500, - headers: jsonHeaders - }); - } - } - // 提供静态页面 if (url.pathname === "/" || url.pathname === "/index.html") { return new Response(html, {