model = new \app\common\model\TaxiOrder(); $this->taxiUserModel = new \app\api\model\taxi\User(); } /** * 接单大厅订单列表 * @return mixed|\think\response\Json * @throws \Lettered\Support\Exceptions\FailedException */ public function index() { // 1. 传入用户位置 $param = $this->request->param(); // 数据校验 $valid = $this->validate($param, [ 'lng' => ['require', 'regex|-?((0|1?[0-7]?[0-9]?)(([.][0-9]{1,4})?)|180(([.][0]{1,4})?))'], 'lat' => ['require', 'regex|-?((0|1?[0-7]?[0-9]?)(([.][0-9]{1,4})?)|180(([.][0]{1,4})?))'], 'page' => 'require', ], [ 'lng.require' => '缺少经度参数', 'lng.regex' => '经度参数有误', 'lat.require' => '缺少维度参数', 'lat.regex' => '维度参数有误' ]); // 错误 if (true !== $valid) { return IResponse::failure($valid); } $limit = isset($param['pageSize']) ? $param['pageSize'] : 20; $taxiUser = $this->auth->guard('taxi_user')->user(); if (!$taxiUser) { return IResponse::failure('用户不存在,或已被冻结'); } // 用户等级 $level = isset($taxiUser['level'])? $taxiUser['level'] : 0; $levelData = TaxiUsersLevel::where(['level'=> $level])->order('created_at','desc')->find(); $levelTime = isset($levelData['time'])? $levelData['time'] : 20; $categoryIds = Taxi::where(['taxi_user_id' => $taxiUser['id']])->column('category_id'); $categoryIds = array_unique($categoryIds); // 经纬度升序 $lists = $this->model ->fieldRaw("* , TRUNCATE(( 6371 * acos ( cos ( radians(" . $param['lat'] . ") ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(" . $param['lng'] . ") ) + sin ( radians(" . $param['lat'] . ") ) * sin( radians( lat ) ) ) ), 2) AS distance") ->having('distance < 10') ->where(['status' => 2]) ->where('created_at','<=', time() - $levelTime) ->where('lat', '>', 0) ->where('lng', '>', 0) ->where(function ($query) use ($categoryIds) { if ($categoryIds) { $query->whereIn('category_id', $categoryIds); } }) ->order(['distance' => 'ASC']) ->limit((($param['page'] - 1) * $limit) . "," . $limit) ->select(); $order = isset($lists[0]) ? $lists[0] : []; $lastOrderTime = isset($order['created_at']) ? $order['created_at']['val'] : time(); $hasNewOrder = $order && $lastOrderTime != $taxiUser->last_order_time ? true : false; $taxiUser->last_order_time = $hasNewOrder ? $lastOrderTime : $taxiUser->last_order_time; $taxiUser->save(); $count = $lists->count(); return IResponse::success(['list' => $lists, 'total' => $count, 'hasNewOrder' => $hasNewOrder], '获取成功'); } /** * 我的订单 * @return mixed * @throws \Lettered\Support\Exceptions\FailedException */ public function myOrder() { // 1. 传入用户位置 $param = $this->request->param(); $limit = isset($param['pageSize']) ? $param['pageSize'] : 20; $taxiUser = $this->auth->guard('taxi_user')->user(); if (!$taxiUser) { return IResponse::failure('用户不存在,或已被冻结'); } // 经纬度升序 $lists = $this->model->with(['paylog', 'user', 'taxi', 'taxiUser']) ->where(['taxi_uid' => $taxiUser['id']]) ->order(['created_at' => 'desc']) ->limit((($param['page'] - 1) * $limit) . "," . $limit) ->select(); return IResponse::success($lists, '获取成功'); } /** * 等待服务订单 * @return mixed|\think\response\Json * @throws \Lettered\Support\Exceptions\FailedException */ public function waitOrder() { $taxiUser = $this->auth->guard('taxi_user')->user(); if (!$taxiUser) { return IResponse::failure('用户不存在,或已被冻结'); } $info = model('common/TaxiOrder')->with(['paylog', 'user', 'taxi', 'taxiUser']) ->where(['taxi_uid' => $taxiUser['id']]) ->whereIn('status', [2, 3]) ->order('created_at', 'desc') ->find(); if ($info) { if($info['taxi_user']){ $info['taxi_user']['level_name'] = TaxiUsersLevel::where(['level'=>$info['taxi_user']['level']])->value('title'); } return IResponse::success(!is_null($info) ? $info : [], '获取成功'); } else { return IResponse::failure('获取失败'); } } /** * 接单 * @return mixed * @throws \Lettered\Support\Exceptions\FailedException */ public function receive(App $app = null) { $param = $this->request->param(); $id = isset($param['id']) ? $param['id'] : 0; $taxiUser = $this->auth->guard('taxi_user')->user(); if (!$taxiUser) { return IResponse::failure('用户不存在,或已被冻结'); } $info = model('common/TaxiOrder') ->where(['id' => $id, 'status' => 2]) ->where('deleted_at', '<=', 0) ->order('created_at', 'desc') ->find(); if (empty($info)) { return IResponse::failure('订单不存在,或已处理'); } if (empty($info['category_id'])) { return IResponse::failure('车型参数错误或不匹配'); } $taxi = Taxi::where(['taxi_user_id' => $taxiUser['id'], 'category_id' => $info['category_id']]) ->where('status', '>', 0) ->order('status', 'asc') ->order('created_at', 'desc') ->find(); if (empty($taxi)) { return IResponse::failure('车型参数错误或您的车型不匹配'); } if ($taxi['status'] != 1) { return IResponse::failure('您的车辆当前不可再接单,请先完成订单或联系客服'); } //$driver = sys_config('', 'driver'); //$divide = isset($driver['platform_divide'])? $driver['platform_divide'] : 0; // 写入数据 Db::startTrans(); $info['status'] = 3; $info['served'] = 1; $info['taxi_id'] = $taxi['id']; $info['taxi_uid'] = $taxiUser['id']; //$info['settle_price'] = $divide>0 && $divide<=100? round($info['price']*(100-$divide)/100, 2) : $info['price']; if (!$info->save()) { Db::rollback(); return IResponse::failure('接单失败'); } if (!Taxi::where(['taxi_user_id' => $taxiUser['id'], 'id' => $taxi['id']])->update(['status' => 2])) { Db::rollback(); return IResponse::failure('接单失败'); } push_socket_data('motor', [ 'id' => $info['id'], 'msg' => '有新的摩的订单等待处理,点击前往!' ]); Db::commit(); // 模板消息 try { $tplTitle = "人人接 - 摩的服务"; $thing7 = '电话请保持通畅,师傅正在赶往路上,请稍候'; $user = model('common/Users')->where(['id' => $info['user_id']])->find(); if ($user && $user['open_id']) { $tpl1 = [ 'template_id' => 'bhkIzzwYpjjXJJ9lPdfXIsUYRrfSz52aJel1n74AA7A', // 所需下发的订阅模板id 'touser' => 'o11PJ5QDWJnIa1kPKsvvStXj243U', 'data' => [ ] ]; $tpl1['touser'] = $user['open_id']; $tpl1['data'] = [ 'character_string1' => [ 'value' => $info['order_no'], ], 'thing3' => [ 'value' => $tplTitle, ], 'amount4' => [ 'value' => $info['price'], ], 'date2' => [ 'value' => date("Y/m/d H:i:s"), ], 'thing7' => [ 'value' => $thing7, ] ]; // 模板消息 $wechat = sys_config('', 'wechat'); $config = [ 'app_id' => $wechat['mini_appid'], 'secret' => $wechat['mni_secret_key'], 'response_type' => 'array', 'log' => [ 'level' => 'debug', 'file' => app()->getRuntimePath() . 'log/' . date('Ym') . '/wechat_debug.log', ], ]; $wechat = Factory::miniProgram($config); $wechat->subscribe_message->send($tpl1); } } catch (\Exception $exception) { } return IResponse::success('接单成功,请尽快前往客户所在地接驾'); } /** * 已送达 * @return mixed * @throws \Lettered\Support\Exceptions\FailedException */ public function delivered() { $param = $this->request->param(); $id = isset($param['id']) ? $param['id'] : 0; $taxiUser = $this->auth->guard('taxi_user')->user(); if (!$taxiUser) { return IResponse::failure('用户不存在,或已被冻结'); } if ($taxiUser['user_id'] <= 0) { return IResponse::failure('账户未绑定用户'); } $info = model('common/TaxiOrder') ->where(['taxi_uid' => $taxiUser['id'], 'id' => $id, 'status' => 3]) ->order('created_at', 'desc') ->find(); if (empty($info)) { return IResponse::failure('订单不存在,或已处理'); } if ($info['taxi_uid'] <= 0 || $info['taxi_id'] <= 0) { return IResponse::failure('参数错误'); } // 写入数据 Db::startTrans(); $info['status'] = 4; if (!$info->save()) { Db::rollback(); return IResponse::failure('确认失败'); } if (!Taxi::where(['taxi_user_id' => $taxiUser['id'], 'id' => $info['taxi_id']])->update(['status' => 1])) { Db::rollback(); return IResponse::failure('确认失败'); } // 到账 if ($info['settle_price']) { if (!model('common/Users')->changePartnership( $taxiUser['user_id'], $info['settle_price'], "订单完成,金额到账【" . $info['settle_price'] . "】", 30, true )) { Db::rollback(); return IResponse::failure('确认失败'); } } // 订单结算给摩的代理 model('\app\api\model\taxi\Award')->send($info); Db::commit(); return IResponse::success('确认成功'); } }