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/process_booking.php
T
wsh5485 89a22c7b11 fix: 确保日志目录存在以避免写入日志失败
在多个文件中添加了检查并创建日志目录的逻辑,防止因目录不存在而导致日志写入失败
2025-12-12 02:50:40 +08:00

229 lines
9.3 KiB
PHP

<?php
// 加载数据库配置
require_once 'db_connect.php';
// #region agent log
$log_data = json_encode(['location' => 'process_booking.php:3', 'message' => 'Database connection check', 'data' => ['has_pdo' => isset($pdo), 'using_db_connect' => true], 'timestamp' => time() * 1000, 'sessionId' => 'debug-session', 'runId' => 'run1', 'hypothesisId' => 'D']);
// 确保日志目录存在
$log_dir = '.cursor';
if (!file_exists($log_dir)) {
mkdir($log_dir, 0777, true);
}
file_put_contents('.cursor/debug.log', $log_data . "\n", FILE_APPEND);
// #endregion
// 检查表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
try {
// 获取表单数据
$customer_name = $_POST['customer_name'] ?? '';
$phone = $_POST['phone'] ?? '';
$car_model = $_POST['car_model'] ?? '';
$car_number = $_POST['car_number'] ?? '';
$booking_date = $_POST['booking_date'] ?? '';
$time_slot = $_POST['time_slot'] ?? '';
$package_id = $_POST['package_id'] ?? '';
$notes = $_POST['notes'] ?? '';
$source = $_POST['source'] ?? '其他'; // 来源标识,用于确定返回页面
$duration = isset($_POST['duration']) ? (int)$_POST['duration'] : 60; // 默认60分钟
$total_price = isset($_POST['total_price']) ? (float)$_POST['total_price'] : 0;
// 验证必填字段
if (empty($customer_name)) {
throw new Exception('请输入客户姓名');
}
if (empty($phone)) {
throw new Exception('请输入手机号码');
} elseif (!preg_match('/^1[3-9]\d{9}$/', $phone)) {
throw new Exception('请输入正确的手机号码');
}
if (empty($car_number)) {
throw new Exception('请输入车牌号');
}
if (empty($booking_date)) {
throw new Exception('请选择预约日期');
}
if (empty($time_slot)) {
throw new Exception('请选择预约时间');
}
if (empty($package_id)) {
throw new Exception('请选择洗车套餐');
}
// 验证日期是否为过去
$current_date = date('Y-m-d');
if ($booking_date < $current_date) {
throw new Exception('不能选择过去的日期');
}
// 解析时间段,转换为start_time和end_time
// time_slot格式可能是 "09:00-10:00" 或 "09:00"
$start_time_str = '';
$end_time_str = '';
// 验证日期格式
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $booking_date)) {
throw new Exception('预约日期格式不正确');
}
if (strpos($time_slot, '-') !== false) {
// 格式:09:00-10:00
list($start_time_str, $end_time_str) = explode('-', $time_slot);
$start_time_str = trim($start_time_str);
$end_time_str = trim($end_time_str);
// 验证时间格式
if (!preg_match('/^\d{2}:\d{2}$/', $start_time_str) || !preg_match('/^\d{2}:\d{2}$/', $end_time_str)) {
throw new Exception('时间段格式不正确');
}
$start_time = $booking_date . ' ' . $start_time_str . ':00';
$end_time = $booking_date . ' ' . $end_time_str . ':00';
// 验证时间有效性
$start_timestamp = strtotime($start_time);
$end_timestamp = strtotime($end_time);
if ($start_timestamp === false || $end_timestamp === false) {
throw new Exception('时间段无效');
}
if ($end_timestamp <= $start_timestamp) {
throw new Exception('结束时间必须晚于开始时间');
}
} else {
// 格式:09:00,使用默认时长
$time_slot = trim($time_slot);
if (!preg_match('/^\d{2}:\d{2}$/', $time_slot)) {
throw new Exception('时间格式不正确');
}
$start_time = $booking_date . ' ' . $time_slot . ':00';
$start_timestamp = strtotime($start_time);
if ($start_timestamp === false) {
throw new Exception('开始时间无效');
}
$end_time = date('Y-m-d H:i:s', $start_timestamp + $duration * 60);
if ($end_time === false) {
throw new Exception('计算结束时间失败');
}
}
// #region agent log
$log_data = json_encode(['location' => 'process_booking.php:70', 'message' => 'Time conversion', 'data' => ['booking_date' => $booking_date, 'time_slot' => $time_slot, 'start_time' => $start_time, 'end_time' => $end_time, 'duration' => $duration], 'timestamp' => time() * 1000, 'sessionId' => 'debug-session', 'runId' => 'run1', 'hypothesisId' => 'E']);
// 确保日志目录存在
$log_dir = '.cursor';
if (!file_exists($log_dir)) {
mkdir($log_dir, 0777, true);
}
file_put_contents('.cursor/debug.log', $log_data . "\n", FILE_APPEND);
// #endregion
// 获取套餐信息以获取价格和默认时长
$stmt = $pdo->prepare("SELECT * FROM packages WHERE id = ? AND is_active = 1");
$stmt->execute([$package_id]);
$package = $stmt->fetch();
if (!$package) {
throw new Exception('选择的套餐无效');
}
// 如果未提供价格,使用套餐价格
if ($total_price <= 0) {
$total_price = $package['price'];
}
// 如果未提供时长,使用套餐默认时长
if ($duration <= 0) {
$duration = $package['base_duration'];
// 重新计算结束时间
$end_time = date('Y-m-d H:i:s', strtotime($start_time) + $duration * 60);
}
// 检查时间冲突
// 两个时间段重叠的条件:现有预约的开始时间 < 新预约的结束时间 AND 现有预约的结束时间 > 新预约的开始时间
// #region agent log
$log_data = json_encode(['location' => 'process_booking.php:100', 'message' => 'Checking time conflict', 'data' => ['start_time' => $start_time, 'end_time' => $end_time], 'timestamp' => time() * 1000, 'sessionId' => 'debug-session', 'runId' => 'run1', 'hypothesisId' => 'H']);
// 确保日志目录存在
$log_dir = '.cursor';
if (!file_exists($log_dir)) {
mkdir($log_dir, 0777, true);
}
file_put_contents('.cursor/debug.log', $log_data . "\n", FILE_APPEND);
// #endregion
$stmt = $pdo->prepare("SELECT COUNT(*) FROM bookings
WHERE status != '已取消'
AND start_time < ?
AND end_time > ?");
$stmt->execute([$end_time, $start_time]);
$conflict_count = $stmt->fetchColumn();
// #region agent log
$log_data = json_encode(['location' => 'process_booking.php:110', 'message' => 'Time conflict check result', 'data' => ['conflict_count' => $conflict_count], 'timestamp' => time() * 1000, 'sessionId' => 'debug-session', 'runId' => 'run1', 'hypothesisId' => 'H']);
// 确保日志目录存在
$log_dir = '.cursor';
if (!file_exists($log_dir)) {
mkdir($log_dir, 0777, true);
}
file_put_contents('.cursor/debug.log', $log_data . "\n", FILE_APPEND);
// #endregion
if ($conflict_count > 0) {
throw new Exception('该时间段已被预约,请选择其他时间');
}
// 对于0元订单,自动标记为已付款
$payment_status = ($total_price <= 0) ? '已付款' : '未付款';
// 插入预约记录(使用新的数据库结构)
$stmt = $pdo->prepare("INSERT INTO bookings
(customer_name, phone, car_model, car_number, package_id, custom_services,
start_time, end_time, duration, total_price, notes, member_type, source, payment_status, status)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$member_type = '普通客户'; // 默认普通客户,VIP客户应该通过index.php添加
$stmt->execute([
$customer_name,
$phone,
$car_model,
$car_number,
$package_id,
'', // custom_services
$start_time,
$end_time,
$duration,
$total_price,
$notes,
$member_type,
$source,
$payment_status,
'待确认' // status
]);
// 预约成功
$success_message = '预约添加成功!';
// 根据来源决定返回页面
$redirect_url = $source === 'vip_page' ? 'vip.php' : 'index.php';
echo "<script>
alert('$success_message');
window.location.href = '$redirect_url';
</script>";
} catch (Exception $e) {
$error_message = $e->getMessage();
echo "<script>
alert('$error_message');
window.history.back();
</script>";
}
} else {
// 不是POST请求,重定向到首页
header('Location: index.php');
exit;
}
?>