import { useState, useCallback } from 'react'; import axios from 'axios'; const api = axios.create({ baseURL: '/api', timeout: 60000, }); // Global toast handler let toastHandler = null; export function setToastHandler(handler) { toastHandler = handler; } function toast(message, type = 'success') { if (toastHandler) toastHandler(message, type); } export function useApi() { const [loading, setLoading] = useState(false); const call = useCallback(async (method, url, data = null, config = {}) => { setLoading(true); try { const response = await api[method](url, data, config); return response.data; } catch (err) { const message = err.response?.data?.error || err.message || '请求失败'; toast(message, 'error'); throw err; } finally { setLoading(false); } }, []); return { call, loading }; } // ---- API functions ---- // Upload export async function uploadFile(formData, onProgress) { const res = await api.post('/upload', formData, { headers: { 'Content-Type': 'multipart/form-data' }, onUploadProgress: (e) => { if (onProgress && e.total) { onProgress(Math.round((e.loaded / e.total) * 100)); } } }); return res.data; } export async function getSources() { const res = await api.get('/upload/sources'); return res.data; } export async function deleteSource(id) { const res = await api.delete(`/upload/sources/${id}`); return res.data; } // Transactions export async function getTransactions(params = {}) { const res = await api.get('/transactions', { params }); return res.data; } export async function getTransaction(id) { const res = await api.get(`/transactions/${id}`); return res.data; } export async function updateTransaction(id, data) { const res = await api.put(`/transactions/${id}`, data); return res.data; } export async function runReconciliation() { const res = await api.post('/transactions/reconcile'); return res.data; } export async function classifyTransactions(ids) { const res = await api.post('/transactions/classify', { ids }); return res.data; } export async function classifyAll() { const res = await api.post('/transactions/classify-all'); return res.data; } // Stats export async function getOverview(params = {}) { const res = await api.get('/stats/overview', { params }); return res.data; } export async function getMonthly(months = 12) { const res = await api.get('/stats/monthly', { params: { months } }); return res.data; } export async function getDaily(days = 30) { const res = await api.get('/stats/daily', { params: { days } }); return res.data; } // Config export async function getConfig() { const res = await api.get('/config'); return res.data; } export async function updateConfig(data) { const res = await api.put('/config', data); return res.data; } export async function testLLM() { const res = await api.post('/config/test-llm'); return res.data; } export async function getCategories() { const res = await api.get('/config/categories'); return res.data; } export async function updateCategory(id, data) { const res = await api.put(`/config/categories/${id}`, data); return res.data; } export function formatAmount(amount) { if (amount === null || amount === undefined) return '--'; const n = Number(amount); const prefix = n >= 0 ? '+' : ''; return `${prefix}${n.toFixed(2)}`; } export function formatDate(dateStr) { if (!dateStr) return '--'; return dateStr.substring(0, 10); } export const SOURCE_TYPE_MAP = { alipay: '支付宝', wechat: '微信支付', bank_debit: '银行借记卡', bank_credit: '银行贷记卡', credit_card: '信用卡' };