Remove BASE_URL constant and simplify URL handling in frontend JavaScript

This commit is contained in:
wood chen 2025-02-08 02:07:53 +08:00
parent 9e151b7663
commit 46fef56e8d

19
main.ts
View File

@ -454,12 +454,11 @@ 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(`${BASE_URL}/api/auth/status`, { const response = await fetch('/api/auth/status', {
credentials: 'include' credentials: 'include'
}); });
const data = await response.json(); const data = await response.json();
@ -544,12 +543,12 @@ 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(`${BASE_URL}/api/prices`, { fetch('/api/prices', {
credentials: 'include' credentials: 'include'
}) })
.then(response => { .then(response => {
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();
}) })
@ -661,7 +660,7 @@ 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 + '</tr>';
showToast('加载价格数据失败', 'danger'); showToast('加载价格数据失败', 'danger');
}); });
} }
@ -673,7 +672,7 @@ const html = `<!DOCTYPE html>
const data = Object.fromEntries(formData.entries()); const data = Object.fromEntries(formData.entries());
try { try {
const response = await fetch(`${BASE_URL}/api/prices`, { const response = await fetch('/api/prices', {
method: 'POST', method: 'POST',
credentials: 'include', credentials: 'include',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
@ -697,7 +696,7 @@ const html = `<!DOCTYPE html>
// 修改审核价格函数 // 修改审核价格函数
async function reviewPrice(id, status) { async function reviewPrice(id, status) {
try { try {
const response = await fetch(`${BASE_URL}/api/prices/${id}/review`, { const response = await fetch('/api/prices/' + id + '/review', {
method: 'POST', method: 'POST',
credentials: 'include', credentials: 'include',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
@ -737,14 +736,14 @@ const html = `<!DOCTYPE html>
// 修改登录函数 // 修改登录函数
function login() { function login() {
const returnUrl = `${BASE_URL}/auth/callback`; const returnUrl = window.location.origin + '/auth/callback';
window.location.href = `${BASE_URL}/api/auth/login?return_url=${encodeURIComponent(returnUrl)}`; window.location.href = '/api/auth/login?return_url=' + encodeURIComponent(returnUrl);
} }
// 修改登出函数 // 修改登出函数
async function logout() { async function logout() {
try { try {
await fetch(`${BASE_URL}/api/auth/logout`, { await fetch('/api/auth/logout', {
method: 'POST', method: 'POST',
credentials: 'include' credentials: 'include'
}); });