mirror of
https://github.com/woodchen-ink/aimodels-prices.git
synced 2025-07-19 14:11:59 +08:00
Improve frontend error handling and URL management with BASE_URL constant
This commit is contained in:
parent
2e1e7ee722
commit
45822518a6
46
main.ts
46
main.ts
@ -454,16 +454,18 @@ const html = `<!DOCTYPE html>
|
|||||||
<script>
|
<script>
|
||||||
let currentUser = null;
|
let currentUser = null;
|
||||||
let vendors = null;
|
let vendors = null;
|
||||||
|
const BASE_URL = window.location.origin;
|
||||||
|
|
||||||
// 检查登录状态
|
// 检查登录状态
|
||||||
async function checkLoginStatus() {
|
async function checkLoginStatus() {
|
||||||
try {
|
try {
|
||||||
const response = await fetch('/api/auth/status');
|
const response = await fetch(\`\${BASE_URL}/api/auth/status\`);
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
currentUser = data.user;
|
currentUser = data.user;
|
||||||
updateLoginUI();
|
updateLoginUI();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('检查登录状态失败:', error);
|
console.error('检查登录状态失败:', error);
|
||||||
|
showToast('检查登录状态失败', 'danger');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -541,8 +543,13 @@ 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/prices')
|
fetch(\`\${BASE_URL}/api/prices\`)
|
||||||
.then(response => response.json())
|
.then(response => {
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(\`HTTP error! status: \${response.status}\`);
|
||||||
|
}
|
||||||
|
return response.json();
|
||||||
|
})
|
||||||
.then(data => {
|
.then(data => {
|
||||||
tbody.innerHTML = '';
|
tbody.innerHTML = '';
|
||||||
|
|
||||||
@ -651,56 +658,57 @@ const html = `<!DOCTYPE html>
|
|||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
console.error('加载价格数据失败:', error);
|
console.error('加载价格数据失败:', error);
|
||||||
tbody.innerHTML = '<tr><td colspan="11" class="text-center">加载失败</td></tr>';
|
tbody.innerHTML = \`<tr><td colspan="11" class="text-center">加载失败: \${error.message}</td></tr>\`;
|
||||||
|
showToast('加载价格数据失败', 'danger');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 提交新价格
|
// 修改提交新价格函数
|
||||||
document.getElementById('newPriceForm').onsubmit = async (e) => {
|
document.getElementById('newPriceForm').onsubmit = async (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
const formData = new FormData(e.target);
|
const formData = new FormData(e.target);
|
||||||
const data = Object.fromEntries(formData.entries());
|
const data = Object.fromEntries(formData.entries());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch('/api/prices', {
|
const response = await fetch(\`\${BASE_URL}/api/prices\`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify(data)
|
body: JSON.stringify(data)
|
||||||
});
|
});
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
alert('提交成功,等待审核');
|
showToast('提交成功,等待审核', 'success');
|
||||||
e.target.reset();
|
e.target.reset();
|
||||||
loadPrices();
|
loadPrices();
|
||||||
} else {
|
} else {
|
||||||
const error = await response.json();
|
const error = await response.json();
|
||||||
alert(error.message || '提交失败');
|
showToast(error.message || '提交失败', 'danger');
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('提交价格失败:', error);
|
console.error('提交价格失败:', error);
|
||||||
alert('提交失败');
|
showToast('提交失败', 'danger');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 审核价格
|
// 修改审核价格函数
|
||||||
async function reviewPrice(id, status) {
|
async function reviewPrice(id, status) {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(\`/api/prices/\${id}/review\`, {
|
const response = await fetch(\`\${BASE_URL}/api/prices/\${id}/review\`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ status })
|
body: JSON.stringify({ status })
|
||||||
});
|
});
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
alert('审核成功');
|
showToast('审核成功', 'success');
|
||||||
loadPrices();
|
loadPrices();
|
||||||
} else {
|
} else {
|
||||||
const error = await response.json();
|
const error = await response.json();
|
||||||
alert(error.message || '审核失败');
|
showToast(error.message || '审核失败', 'danger');
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('审核价格失败:', error);
|
console.error('审核价格失败:', error);
|
||||||
alert('审核失败');
|
showToast('审核失败', 'danger');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -722,16 +730,16 @@ const html = `<!DOCTYPE html>
|
|||||||
bsToast.show();
|
bsToast.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 登录函数
|
// 修改登录函数
|
||||||
function login() {
|
function login() {
|
||||||
const returnUrl = \`\${window.location.origin}/auth/callback\`;
|
const returnUrl = \`\${BASE_URL}/auth/callback\`;
|
||||||
window.location.href = \`/api/auth/login?return_url=\${encodeURIComponent(returnUrl)}\`;
|
window.location.href = \`\${BASE_URL}/api/auth/login?return_url=\${encodeURIComponent(returnUrl)}\`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 登出函数
|
// 修改登出函数
|
||||||
async function logout() {
|
async function logout() {
|
||||||
try {
|
try {
|
||||||
await fetch('/api/auth/logout', { method: 'POST' });
|
await fetch(\`\${BASE_URL}/api/auth/logout\`, { method: 'POST' });
|
||||||
window.location.reload();
|
window.location.reload();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('登出失败:', error);
|
console.error('登出失败:', error);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user