You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
3.2 KiB
99 lines
3.2 KiB
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\JsonReturn;
|
|
use App\Models\ImsCjdcOrderMain;
|
|
use App\Models\v3\LanzuEmployees;
|
|
use App\Models\v3\User;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Redis;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
/**
|
|
* 骑手抢单
|
|
*/
|
|
class GrabOrderController extends Controller
|
|
{
|
|
use JsonReturn;
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$id = (int)$request->route('id');
|
|
if (!$id || !$order = ImsCjdcOrderMain::find($id)) {
|
|
return self::error('唉呀,来晚啦,订单已经被接走啦~~~');
|
|
}
|
|
|
|
/*$order = ImsCjdcOrderMain::with(['market:id,name'])
|
|
->where([['state', '=', 2], ['pay_time', '>', 0]])
|
|
->orderBy('pay_time')->orderByDesc('id')
|
|
->limit(15)->get();*/
|
|
|
|
return view('web.grab_order', ['order_list' => $order ?? []]);
|
|
}
|
|
|
|
/**
|
|
* 抢单post
|
|
*/
|
|
public function grabOrder(Request $request)
|
|
{
|
|
try {
|
|
$validator = Validator::make($request->all(), [
|
|
'order_num' => ['required', 'regex:/^\d+$/'],
|
|
'name' => 'required|between:2,20',
|
|
'tel' => ['required', 'regex:/^1\d{10}$/'],
|
|
], [
|
|
'*.required' => ':attribute不能为空',
|
|
'*.regex' => ':attribute格式不正确',
|
|
'name.between' => ':attribute输入不正确',
|
|
], [
|
|
'order_num' => '订单号',
|
|
'name' => '姓名',
|
|
'tel' => '手机号',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
throw new \Exception($validator->errors()->first());
|
|
}
|
|
|
|
$formData = $validator->validated();
|
|
|
|
$user = User::where('tel', $formData['tel'])->first();
|
|
if (!$user) {
|
|
throw new \Exception('用户信息不存在');
|
|
}
|
|
|
|
$employee = LanzuEmployees::where(['user_id' => $user->id, 'name' => $formData['name']])->orderByDesc('id')->first();
|
|
if (!$employee || !in_array(29, $employee->position)) { //29是骑手,见:config("role.position")
|
|
throw new \Exception("您还不是骑手,请联系管理员增加骑手权限");
|
|
}
|
|
|
|
$order = ImsCjdcOrderMain::where('order_num', $formData['order_num'])->first();
|
|
if (!$order) {
|
|
throw new \Exception('订单跑丢了~~');
|
|
} else if ($order->state != 3) {
|
|
throw new \Exception('您来晚啦,订单已经被人接走啦~~');
|
|
}
|
|
|
|
/**
|
|
* 抢单逻辑详见:
|
|
* @see \App\Admin\Forms\SelectHorseman
|
|
*/
|
|
$SelectHorseman = new \App\Admin\Forms\SelectHorseman;
|
|
$result = $SelectHorseman->handle(['horseman_id' => $employee->id, 'order_id' => $order->id]);
|
|
|
|
return self::success('抢单成功,已消息通知客户,请尽快配送');
|
|
} catch (\Exception $exception) {
|
|
return self::error($exception->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取订单信息
|
|
*/
|
|
public function orderInfo()
|
|
{
|
|
$order_num = \request('order_num');
|
|
}
|
|
}
|