'未登录']); exit; } $user_id = $_SESSION['user_id']; $action = $_GET['action'] ?? ''; // 获取当前用户的账单列表 if ($action === 'list') { $stmt = $pdo->prepare("SELECT id, bill_date as date, customer, item, money, paid FROM bills WHERE user_id = ? ORDER BY id DESC"); $stmt->execute([$user_id]); $bills = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach ($bills as &$bill) { $bill['paid'] = (bool)$bill['paid']; } echo json_encode($bills); exit; } // 添加账单 if ($action === 'add') { $data = json_decode(file_get_contents('php://input'), true); $date = $data['date']; $customer = $data['customer']; $item = $data['item']; $money = $data['money']; $paid = isset($data['paid']) ? ($data['paid'] ? 1 : 0) : 0; $stmt = $pdo->prepare("INSERT INTO bills (bill_date, customer, item, money, paid, user_id) VALUES (?, ?, ?, ?, ?, ?)"); $stmt->execute([$date, $customer, $item, $money, $paid, $user_id]); echo json_encode(['success' => true, 'id' => $pdo->lastInsertId()]); exit; } // 更新账单(必须属于当前用户) if ($action === 'update') { $data = json_decode(file_get_contents('php://input'), true); $id = $data['id']; $date = $data['date']; $customer = $data['customer']; $item = $data['item']; $money = $data['money']; $paid = $data['paid'] ? 1 : 0; $stmt = $pdo->prepare("UPDATE bills SET bill_date=?, customer=?, item=?, money=?, paid=? WHERE id=? AND user_id=?"); $stmt->execute([$date, $customer, $item, $money, $paid, $id, $user_id]); echo json_encode(['success' => true]); exit; } // 删除账单 if ($action === 'delete') { $data = json_decode(file_get_contents('php://input'), true); $id = $data['id']; $stmt = $pdo->prepare("DELETE FROM bills WHERE id=? AND user_id=?"); $stmt->execute([$id, $user_id]); echo json_encode(['success' => true]); exit; } // 切换支付状态 if ($action === 'togglePaid') { $data = json_decode(file_get_contents('php://input'), true); $id = $data['id']; $stmt = $pdo->prepare("UPDATE bills SET paid = NOT paid WHERE id=? AND user_id=?"); $stmt->execute([$id, $user_id]); echo json_encode(['success' => true]); exit; } echo json_encode(['error' => '无效请求']); ?>