This repository has been archived on 2026-06-20. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
carwash_order/get_vip_last_booking.php
T
wsh5485 2e68b94aba feat(vip): 添加VIP客户最近预约记录显示功能
新增get_vip_last_booking.php接口查询VIP客户最近预约记录
在index.php中添加预约信息显示区域和前端逻辑
创建test_vip_booking_history.php测试页面验证功能
2025-11-19 18:37:56 +08:00

86 lines
2.4 KiB
PHP

<?php
// 获取VIP客户最近的预约记录
header('Content-Type: application/json');
// 连接数据库
require 'db_connect.php';
// 检查是否提供了VIP ID
if (!isset($_GET['vip_id']) || empty($_GET['vip_id'])) {
echo json_encode(['error' => '未提供VIP客户ID']);
exit;
}
$vipId = intval($_GET['vip_id']);
// 尝试从数据库获取数据
$lastBooking = null;
try {
// 查询该VIP客户的最近一次预约
$sql = "SELECT
b.id,
b.appointment_date,
b.appointment_time,
b.duration,
p.package_name
FROM
bookings b
LEFT JOIN
packages p ON b.package_id = p.id
WHERE
b.customer_id = :vipId
AND b.status != '已取消'
ORDER BY
b.appointment_date DESC,
b.appointment_time DESC
LIMIT 1";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':vipId', $vipId, PDO::PARAM_INT);
$stmt->execute();
$lastBooking = $stmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
// 数据库查询失败,返回错误但继续执行
echo json_encode(['error' => '数据库查询失败: ' . $e->getMessage()]);
exit;
}
// 如果没有找到记录,返回空结果
if (!$lastBooking) {
echo json_encode(['has_booking' => false]);
exit;
}
// 格式化返回数据
$response = [
'has_booking' => true,
'appointment_date' => $lastBooking['appointment_date'],
'appointment_time' => $lastBooking['appointment_time'],
'package_name' => $lastBooking['package_name'] ? $lastBooking['package_name'] : '自定义服务',
'duration' => $lastBooking['duration']
];
echo json_encode($response);
// 关闭数据库连接
$conn = null;
// 如果在模拟环境中运行,提供测试数据
if (isset($_GET['test']) && $_GET['test'] == 1) {
// 生成测试数据
$testResponse = [
'has_booking' => true,
'appointment_date' => date('Y-m-d', strtotime('-7 days')),
'appointment_time' => '14:30',
'package_name' => '豪华精洗套餐',
'duration' => 90
];
// 如果URL参数指定no_booking,则返回没有预约的结果
if (isset($_GET['no_booking'])) {
$testResponse = ['has_booking' => false];
}
echo json_encode($testResponse);
}