// API 调用封装 const API_BASE = '/api'; const API = { // 菜品相关 async getDishes(params = {}) { const query = new URLSearchParams(params).toString(); const res = await fetch(`${API_BASE}/dishes/${query ? '?' + query : ''}`); return res.json(); }, async getDish(id) { const res = await fetch(`${API_BASE}/dishes/${id}/`); return res.json(); }, async createDish(data) { const res = await fetch(`${API_BASE}/dishes/`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); return res.json(); }, async updateDish(id, data) { const res = await fetch(`${API_BASE}/dishes/${id}/`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); return res.json(); }, async deleteDish(id) { await fetch(`${API_BASE}/dishes/${id}/`, { method: 'DELETE' }); }, // 分类相关 async getCategories() { const res = await fetch(`${API_BASE}/categories/`); return res.json(); }, // 订单相关 async getOrders(params = {}) { const query = new URLSearchParams(params).toString(); const res = await fetch(`${API_BASE}/orders/${query ? '?' + query : ''}`); return res.json(); }, async createOrder(data) { const res = await fetch(`${API_BASE}/orders/`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); return res.json(); }, async completeOrder(id) { const res = await fetch(`${API_BASE}/orders/${id}/complete/`, { method: 'POST' }); return res.json(); }, async deleteOrder(id) { await fetch(`${API_BASE}/orders/${id}/`, { method: 'DELETE' }); }, // URL解析 async parseUrl(url) { const res = await fetch(`${API_BASE}/parse-url/`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ url }) }); return res.json(); }, // 图片上传 async uploadImage(file) { const formData = new FormData(); formData.append('image', file); const res = await fetch('/upload-image/', { method: 'POST', body: formData }); return res.json(); } };