// در فایل فرانت‌اند (index.html) const API_CONFIG = { BASE_URL: '/api', // مسیر نسبی - روی همان دامنه ENDPOINTS: { LOGIN_ADMIN: '/auth/admin', LOGIN_SHOP: '/auth/shop', SHOPS: '/shops', PERSONNEL: '/personnel', PURCHASES: '/purchases', TICKETS: '/tickets', REPORTS: '/reports', UPLOAD: '/upload' }, getFullUrl: function(endpoint) { return `${this.BASE_URL}${endpoint}`; }, getHeaders: () => ({ 'Content-Type': 'application/json', 'Authorization': `Bearer ${localStorage.getItem('token') || ''}` }) }; // API Service ساده class ApiService { static async request(endpoint, method = 'GET', data = null) { const url = API_CONFIG.getFullUrl(endpoint); const options = { method, headers: API_CONFIG.getHeaders() }; if (data && method !== 'GET') { options.body = JSON.stringify(data); } console.log('📡 API Request:', { url, options }); try { const response = await fetch(url, options); const text = await response.text(); console.log('📡 API Response:', text); let result; try { result = JSON.parse(text); } catch (e) { throw new Error(`پاسخ غیرمعتبر از سرور: ${text.substring(0, 100)}...`); } if (!result.success) { throw new Error(result.message || 'خطا در ارتباط با سرور'); } return result; } catch (error) { console.error('❌ API Error:', error); throw error; } } // متدهای ساده static async adminLogin(username, password) { return this.request(API_CONFIG.ENDPOINTS.LOGIN_ADMIN, 'POST', { username, password }); } static async shopLogin(code, password) { return this.request(API_CONFIG.ENDPOINTS.LOGIN_SHOP, 'POST', { code, password }); } } // Utility Functions class Utils { static showAlert(message, type = 'info') { alert(`[${type.toUpperCase()}] ${message}`); } static showLoading() { // نمایش loading } static hideLoading() { // پنهان کردن loading } } // تست سریع API async function testApi() { try { console.log('🔍 Testing API...'); const response = await fetch('/api/test'); const data = await response.json(); console.log('✅ API Test Result:', data); return data.success; } catch (error) { console.error('❌ API Test Failed:', error); return false; } } // هنگام لود صفحه تست کن document.addEventListener('DOMContentLoaded', async () => { const apiWorking = await testApi(); if (!apiWorking) { Utils.showAlert('API در دسترس نیست! لطفا سرور را بررسی کنید.', 'error'); } });