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.
112 lines
3.8 KiB
112 lines
3.8 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): JsonResponse
|
|
{
|
|
try {
|
|
$validator = Validator::make($request->all(), [
|
|
'order_id' => ['required', 'int'],
|
|
'name' => 'required|between:2,20',
|
|
'tel' => ['required', 'regex:/^1\d{10}$/'],
|
|
], [
|
|
'*.required' => ':attribute不能为空',
|
|
'*.regex' => ':attribute格式不正确',
|
|
'order_id.int' => ':attribute错误',
|
|
'name.between' => ':attribute输入不正确',
|
|
], [
|
|
'order_id' => '订单ID',
|
|
'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('id', $formData['order_id'])->first();
|
|
if (!$order) {
|
|
throw new \Exception('订单跑丢了~~');
|
|
} else if ($order->state != 3) {
|
|
throw new \Exception('您来晚啦,订单已经被人接走啦~~');
|
|
}
|
|
|
|
$SelectHorseman = new \App\Admin\Forms\SelectHorseman;
|
|
$result = $SelectHorseman->selectHorseman(['horseman_id' => $employee->id, 'order_id' => $order->id]);
|
|
if (!$result) {
|
|
throw new \Exception('服务器开小差,抢单失败~');
|
|
}
|
|
|
|
return self::success('抢单成功,已消息通知客户,请尽快配送');
|
|
} catch (\Exception $exception) {
|
|
return self::error($exception->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取订单信息
|
|
*/
|
|
public function orderInfo(): JsonResponse
|
|
{
|
|
$id = request('id');
|
|
|
|
$fields = ['id', 'order_num', 'refund_time', 'complete_time', 'money', 'tel', 'name', 'address', 'note', 'area',
|
|
'lat', 'lng', 'pay_type', 'order_type', 'market_id', 'total_money', 'created_at', 'pay_time'];
|
|
$order = ImsCjdcOrderMain::where('state', 3)->find($id, $fields);
|
|
if (!$order) {
|
|
return self::error('订单不存在或已配送完成~~');
|
|
}
|
|
|
|
if ($order->tel) {
|
|
$order->tel = substr_replace($order->tel, '****', 3, 4);
|
|
}
|
|
|
|
return self::success($order);
|
|
}
|
|
}
|