mirror of
https://github.com/woodchen-ink/aimodels-prices.git
synced 2025-07-18 13:41:59 +08:00
Refactor frontend and backend price data loading with improved error handling and logging
This commit is contained in:
parent
d66cd06938
commit
fe48d1af9e
67
main.ts
67
main.ts
@ -552,16 +552,17 @@ 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', {
|
// 直接使用相对路径
|
||||||
credentials: 'include'
|
fetch('/api/prices')
|
||||||
})
|
|
||||||
.then(response => {
|
.then(response => {
|
||||||
|
console.log('Response status:', response.status);
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error('HTTP error! status: ' + response.status);
|
throw new Error('HTTP error! status: ' + response.status);
|
||||||
}
|
}
|
||||||
return response.json();
|
return response.json();
|
||||||
})
|
})
|
||||||
.then(data => {
|
.then(data => {
|
||||||
|
console.log('Received data:', data);
|
||||||
tbody.innerHTML = '';
|
tbody.innerHTML = '';
|
||||||
|
|
||||||
if (!data || !Array.isArray(data)) {
|
if (!data || !Array.isArray(data)) {
|
||||||
@ -670,7 +671,6 @@ const html = `<!DOCTYPE html>
|
|||||||
.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');
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -727,11 +727,15 @@ const html = `<!DOCTYPE html>
|
|||||||
|
|
||||||
// 初始化
|
// 初始化
|
||||||
async function init() {
|
async function init() {
|
||||||
await Promise.all([
|
console.log('Initializing...');
|
||||||
checkLoginStatus(),
|
try {
|
||||||
loadVendors()
|
await loadVendors();
|
||||||
]);
|
console.log('Vendors loaded');
|
||||||
loadPrices();
|
await loadPrices();
|
||||||
|
console.log('Prices loaded');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('初始化失败:', error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 修改 Toast 提示函数
|
// 修改 Toast 提示函数
|
||||||
@ -845,7 +849,7 @@ function validateData(data: any): string | null {
|
|||||||
// 修改处理函数
|
// 修改处理函数
|
||||||
async function handler(req: Request): Promise<Response> {
|
async function handler(req: Request): Promise<Response> {
|
||||||
const headers = {
|
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-Methods": "GET, POST, OPTIONS, PUT, DELETE",
|
||||||
"Access-Control-Allow-Headers": "Content-Type, Cookie, Authorization",
|
"Access-Control-Allow-Headers": "Content-Type, Cookie, Authorization",
|
||||||
"Access-Control-Allow-Credentials": "true",
|
"Access-Control-Allow-Credentials": "true",
|
||||||
@ -864,7 +868,8 @@ async function handler(req: Request): Promise<Response> {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const url = new URL(req.url);
|
const url = new URL(req.url);
|
||||||
|
console.log('Received request:', req.method, url.pathname);
|
||||||
|
|
||||||
// 处理预检请求
|
// 处理预检请求
|
||||||
if (req.method === "OPTIONS") {
|
if (req.method === "OPTIONS") {
|
||||||
return new Response(null, {
|
return new Response(null, {
|
||||||
@ -873,6 +878,27 @@ async function handler(req: Request): Promise<Response> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 获取价格列表
|
||||||
|
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") {
|
if (url.pathname === "/api/auth/status") {
|
||||||
try {
|
try {
|
||||||
@ -1152,25 +1178,6 @@ async function handler(req: Request): Promise<Response> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取价格列表
|
|
||||||
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") {
|
if (url.pathname === "/" || url.pathname === "/index.html") {
|
||||||
return new Response(html, {
|
return new Response(html, {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user