Files
tripz-llc/backend/food/courier/location.php
T
2026-08-09 16:56:13 +03:00

49 lines
2.0 KiB
PHP

<?php
// food/courier/location.php — بثّ موقع السائق أثناء مهمة توصيل نشطة
//
// خصوصية: الموقع يُقبل فقط ما دام الطلب في courier_assigned/picked_up، ويُخزَّن
// في Redis بعمر 90 ثانية لا في قاعدة البيانات. بمجرد التسليم يرفض الخادم أي
// تحديث، وينتهي آخر موقع تلقائياً — فلا يتحول التتبّع إلى مراقبة للسائق بعد
// انتهاء عمله، ولا يبقى أثر دائم لتحركاته في وحدة الطعام.
require_once __DIR__ . '/../connect_courier.php';
$orderId = filterRequest('order_id', 'int');
$lat = filterRequest('lat');
$lng = filterRequest('lng');
if (!$orderId || $lat === null || $lng === null) {
jsonError('order_id, lat and lng are required');
}
$lat = (float)$lat;
$lng = (float)$lng;
if ($lat < -90 || $lat > 90 || $lng < -180 || $lng > 180 || ($lat === 0.0 && $lng === 0.0)) {
jsonError('Invalid coordinates');
}
$order = foodAssertOrderOwnership($orderId, 'courier', $food_courier_id);
if (!in_array($order['status'], ['courier_assigned', 'picked_up'], true)) {
jsonError('Location sharing is only allowed while the delivery is active', 403);
}
$payload = [
'order_id' => $orderId,
'lat' => round($lat, 6),
'lng' => round($lng, 6),
'heading' => filterRequest('heading') !== null ? (float)filterRequest('heading') : null,
'ts' => time(),
];
if ($redis) {
// مصدر مسار الاحتياط: تطبيق الراكب يسحبه من order/courier_location.php
$redis->setex("food:order:{$orderId}:courier_pos", 90, json_encode($payload));
}
// المسار اللحظي — غرفة الزبون على سوكيت الطعام
foodPushToSocket('courier_location', array_merge($payload, [
'passenger_id' => (string)$order['passenger_id'],
]));
jsonSuccess(['order_id' => $orderId, 'ts' => $payload['ts']]);