我正在使用laravel为uber和ola等驾驶室预订应用程序做api。在客户将使用gcm将请求发送到可用的cab驱动程序,并且cab接受请求。在客户点击请求按钮的这些过程中它将被加载,直到司机接受请求。当司机接受请求时,客户获取驱动程序详细信息,驱动程序获取客户详细信息。如何在laravel中进行操作.. ??
我可以使用以下代码发送客户请求:
public function customer_booking(Request $req)
{
if ($req->isMethod('post'))
{
$customer_id=$req->customer_id;
$driver_type=$req->type_id;
//getting driver
$res=DriverLatLongModel::where('driver_type',$driver_type)
->where('booking_status','3')->orwhere('booking_status','2')
->where('active_status','0')->orderBy('created_at', 'desc')->first();
$driver_lat='';
$driver_long='';
$driver_id_booking='';
if(empty($res))
{
$gcm_data[]=array('status'=>'0');
return Response::json(array('message'=>$gcm_data), 200);
}
else
{
$driver_id_booking=$res->driver_id;
}
$registration_id = array();
//getting gcm id
$driver_position = DriverLatLongModel::where('driver_id',$driver_id_booking)->get();
foreach($driver_position as $resid)
{
array_push($registration_id , $resid['registration_id']);
}
//send gcm
$url = 'https://android.googleapis.com/gcm/send';
$message_gcm = array("Status"=>"1","Notice" =>"WELCOME","customer_id"=>$customer_id);
$fields = array(
'registration_ids' => $registration_id ,
'data' => $message_gcm ,
);
$headers = array(
'Authorization: key=AIzaSyDCDmsrv3ELqD_6qseFgERciRnmm9uBtNg',
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
$driver_data=array();
//getting result of driver side
$finding_donor_loc=CustomerBookingModel::getCustomer($customer_id);
if(empty($finding_donor_loc))
{
$driver_data[]=array('status'=>'0');
}
else
{
//getting driver details
$driver_details=DriverDetailsModel::where('driver_id',$finding_donor_loc)->first();
$driver_name=$driver_details->driver_name;
$driver_data[]=array('status'=>'1','driver_name'=>$driver_name);
}
return Response::json(array('message'=>$driver_data), 200);
}
}
我正在使用Helper类编写代码,等待驱动程序接受请求:
public static function getCustomer($customer_id)
{
$duration =15; // Duration of the loop in seconds
$sleep = 5;// Sleep beetween each execution (with stuff execution)
for ($i = 0; $i < floor($duration / $sleep); ++$i)
{
$start = microtime(true);
$users = DB::table('booking_information')
->where('customer_id',$customer_id)->where('booking_status','1')
->where('book_confirm','1')->orderBy('created_at', 'asc')->first();
time_sleep_until($start + $sleep);
}
if(empty($users))
{
$confim_driver_id='0';
return $confim_driver_id;
}
else
{
$confim_driver_id=$users ->driver_id;
return $confim_driver_id;
}
}
帮助程序类在15秒内接受请求时执行获取驱动程序ID的工作。它对我有用,但驱动程序将在15秒内接受请求。如何在没有持续时间的情况下检查数据库是否是否存在驾驶员ID,并且在驾驶员接受请求之前,它将在客户端加载。!!
答案 0 :(得分:0)
如果您想在预订确认后立即将预订确认信发回给用户,那么您可以将IF条件放入您的for循环中。一旦满足条件,就返回driver_id。
$duration = 15; // Duration of the loop in seconds
$sleep = 5; // Sleep between each execution (with stuff execution)
for ($i = 0; $i < floor($duration / $sleep); ++$i)
{
$start = microtime(true);
// query the database to check if the booking is confirmed
$booking = DB::table('booking_information')
->where('customer_id', $customer_id)
->where('booking_status', '1')
->where('book_confirm', '1')
->orderBy('created_at', 'asc')
->first();
// if the booking is confirmed then return the driver_id
// return will also stop the loop
if($booking) {
return $booking->driver_id;
}
// Make the script execution sleep
time_sleep_until($start + $sleep);
}
// if booking is not confirmed then return 0
if(empty($booking)) return 0;
在laravel中执行此类任务的更好方法可以通过Events&amp;听众。工作流程就是这样的。
1. Your API recieves a request to book a cab
2. A NewBookingRequest event is fired & NewBookingRequestListener sends a push notificaton to the nearest driver about the new booking
3. Driver confirms/denies the booking & your API fires BookingConfirmed/BookingDenied event
4. Notification is sent to the user about the booking