* @date 2020/7/3 16:29 * * @return \think\response\Json * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getNearby() { $param = $this->request->param(); $param['category_id'] = $this->request->param('category_id', 0); $limit = 10; $map['status'] = 1; if ($param['category_id']) { $map['category_id'] = $param['category_id']; } // $taxi = model('common/Taxi')->with(['user']) ->where($map) ->select(); return $this->ApiJson(0,'获取成功', $taxi); } /** * 用车信息 * * @url GET /taxi/:id * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/7/1 10:37 * * @param int $id 用车ID * @return \think\response\Json * @throws \Lettered\Support\Exceptions\EvidentException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getTaxiByID($id) { (new IDMustBePositiveInt())->valid(); $taxi = model('common/Taxi')->with(['user'])->hidden(['user.id_card']) ->find($id); if (!$taxi){ return $this->ApiJson(-1, '不存在用车编号'); } return $this->ApiJson(0, '获取用车信息成功', $taxi); } /** * 用车 */ public function callTaxi() { $params = $this->request->param(); // 参数校验 $valid = $this->validate($params, [ 'taxi_id|关联车辆' => 'require', 'mobile|联系方式' => 'require', 'count|乘车人数' => 'require', 'depart|起始位置' => 'require', 'arrive|送达位置' => 'require', 'km|公里数' => 'require', 'price|价格' => 'require', ]); // 错误返回 if(true !== $valid){ return $this->ApiJson(-1, $valid); }; $user = $this->auth->user(); // 统计订单 $count = model('common/TaxiOrder')->where('status','>=','2')->where(['is_free' => '1','user_id' => $user['id']])->count('id'); // 创建订单 $params['order_no'] = get_order_no(); $params['user_id'] = $user['id']; $order = model('common/TaxiOrder')::create($params,true); // if ($order){ // 创建对应支付记录 $trade_no = get_order_no(); $logID = model('common/OrderPaylog')->storeBy([ 'out_trade_no' => $trade_no, 'total_price' => $params['price'], 'order_idx' => $order['id'], 'ascription' => 'motor' // 归属订单 ]); // 免单优惠 20201201 添加条件 仅限xx 公里以下 不包含 xx 公里 if ($count < sys_config('taxi_order_free_num','store') && $params['km'] < sys_config('taxi_order_free_km','store')){ model('common/OrderPaylog')->updateBy($logID,[ 'pay_price' => $params['price'], 'is_pay' => 1 ]); model('common/TaxiOrder')->updateBy($order['id'], [ 'is_free' => 1, // 免单 'status' => 2 ]); // 后台推送 // push_socket_data('motor',[ // 'id' => $order['id'], // 'msg' => '有新的(免费)摩的订单等待处理哟,点击前往!' // ]); return $this->ApiJson(0,"本单免费,等待师傅接驾", [ 'type' => 'free', 'success' => "ok!" ]); } // 返回支付单号 return $this->ApiJson(0,'订单提交成功', [ 'type' => 'wx', 'success' => "ok!", 'trade_no' => $trade_no ]); } return $this->ApiJson(-1,'发生异常,请骚后重试...'); } /** * 用车,新的地图下单方式 */ public function newCallTaxi() { $datas = $this->request->param(); // 参数校验 $valid = $this->validate($datas, [ 'mobile|联系方式' => 'require', 'count|乘车人数' => 'require', 'depart|起始位置' => 'require', 'arrive|送达位置' => 'require', 'km|公里数' => 'require', 'price|价格' => 'require', ]); // 错误返回 if(true !== $valid){ return $this->ApiJson(-1, $valid); }; $user = $this->auth->user(); // 统计订单 $count = model('common/TaxiOrder')->where('status','>=','2')->where(['is_free' => '1','user_id' => $user['id']])->count('id'); // 创建订单 $driver = sys_config('', 'driver'); $divide = isset($driver['platform_divide'])? $driver['platform_divide'] : 0; $depart = isset($datas['depart'])? $datas['depart'] : ''; $arrive = isset($datas['arrive'])? $datas['arrive'] : ''; $params = [ 'user_id'=> $user['id'], 'category_id'=> isset($datas['category_id'])? $datas['category_id'] : 0, 'order_no'=> get_order_no(), 'price'=> isset($datas['price'])? $datas['price'] : 0.00, 'settle_price' => $divide>0 && $divide<=100? round($datas['price']*(100-$divide)/100, 2) : $datas['price'], 'count'=> isset($datas['count'])? $datas['count'] : 0, 'mobile'=> isset($datas['mobile'])? $datas['mobile'] : '', 'depart'=> $depart, 'depart_address'=> isset($datas['depart_address'])? $datas['depart_address'] : $depart, 'arrive'=> $arrive, 'arrive_address'=> isset($datas['arrive_address'])? $datas['arrive_address'] : $arrive, 'depart_lat'=> isset($datas['depart_lat'])? $datas['depart_lat'] : 0, 'depart_lng'=> isset($datas['depart_lng'])? $datas['depart_lng'] : 0, 'lat'=> isset($datas['lat'])? $datas['lat'] : 0, 'lng'=> isset($datas['lng'])? $datas['lng'] : 0, 'km'=> isset($datas['km'])? $datas['km'] : '', ]; // 验证是否有未支付订单,有则直接更新订单为新订单 $hasOrderId = model('common/TaxiOrder')->where(['user_id'=> $user['id'],'status'=> 1])->order('created_at','desc')->value('id'); if($hasOrderId){ model('common/TaxiOrder')->where(['id'=> $hasOrderId])->update($params); $order = model('common/TaxiOrder')->where(['id'=> $hasOrderId])->findOrFail(); }else{ $order = model('common/TaxiOrder')::create($params,true); } // 订单信息验证处理 if ($order && $order['id']){ // 创建对应支付记录 $trade_no = get_order_no(); $logID = model('common/OrderPaylog')->storeBy([ 'out_trade_no' => $trade_no, 'total_price' => $params['price'], 'order_idx' => $order['id'], 'ascription' => 'motor' // 归属订单 ]); // 免单优惠 20201201 添加条件 仅限xx 公里以下 不包含 xx 公里 if ($count < sys_config('taxi_order_free_num','store') && $params['km'] < sys_config('taxi_order_free_km','store')){ model('common/OrderPaylog')->updateBy($logID,[ 'pay_price' => $params['price'], 'is_pay' => 1 ]); model('common/TaxiOrder')->updateBy($order['id'], [ 'is_free' => 1, // 免单 'status' => 2 ]); // 后台推送 // push_socket_data('motor',[ // 'id' => $order['id'], // 'msg' => '有新的(免费)摩的订单等待处理哟,点击前往!' // ]); return $this->ApiJson(0,"本单免费,等待师傅接驾", [ 'type' => 'free', 'success' => "ok!" ]); } // 返回支付单号 return $this->ApiJson(0,'订单提交成功', [ 'type' => 'wx', 'success' => "ok!", 'order_id' => $order['id'], 'trade_no' => $trade_no ]); } return $this->ApiJson(-1,'发生异常,请骚后重试...'); } /** * 订单详情 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/7/6 11:09 * * @return \think\response\Json * @throws \Lettered\Support\Exceptions\FailedException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function taxiOrder() { $param = $this->request->param(); // 数据校验 $valid = $this->validate($param, [ 'order_id' => 'require', ]); // 错误 if (true !== $valid) { return $this->ApiJson(-1, $valid); } $info = model('common/TaxiOrder')->with(['paylog','user','taxi','taxiUser']) ->where(['user_id' => $this->auth->user()['id'], 'id'=> $param['order_id']]) ->find(); return $this->ApiJson(0,'获取成功', $info); } /** * 当前进行中订单详情 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/7/6 11:09 * * @return \think\response\Json * @throws \Lettered\Support\Exceptions\FailedException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function waitOrder() { $info = model('common/TaxiOrder')->with(['paylog','user','taxi','taxiUser']) ->where(['user_id' => $this->auth->user()['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 $this->ApiJson(0,'获取成功', !is_null($info)? $info : []); } /** * 订单 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/7/6 11:09 * * @return \think\response\Json * @throws \Lettered\Support\Exceptions\FailedException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function order() { $param = $this->request->param(); $limit = 10; // 数据校验 $valid = $this->validate($param, [ 'page' => 'require', ]); // 错误 if (true !== $valid) { return $this->ApiJson(-1, $valid); } $orders = model('common/TaxiOrder') ->with(['paylog']) ->where(['user_id' => $this->auth->user()['id']]) ->limit((($param['page'] - 1) * $limit) . "," . $limit) ->order(['id' => 'desc']) ->select(); return $this->ApiJson(0,'获取成功', $orders); } /** * @desc 取消订单 * @return \think\response\Json * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @author weichuanbao<654745815@qq.com> * @date 2021/12/2 0002 */ public function cancelOrder() { $param = $this->request->param(); // 数据校验 $valid = $this->validate($param, [ 'id' => 'require|number', ]); // 错误 if (true !== $valid) { return $this->ApiJson(-1, $valid); } $row = model('common/TaxiOrder')->with(['paylog']) ->field('id,taxi_uid,taxi_id,status,created_at') ->whereNotIn('status', [0,5]) ->find($param['id']); $user = $this->auth->user(); if ($row) { if (time() - $row['created_at']['val'] < (60 * 10)) { // return $this->ApiJson(-1, '10分钟内无法取消订单'); } $taxiUid = isset($row['taxi_uid'])? intval($row['taxi_uid']) : 0; if ($row['status'] == 2 || $row['status'] == 3) { $config = Cache::get('system_config'); $total_price = $row->paylog['total_price']; // 如果平台已指派司机,则扣除部分金额 $cost_price = 0; $total = $total_price; $createTime = isset($row['created_at'])? $row['created_at'] : 0; $cancelTime = isset($config['store']['store_cancel_time'])? $config['store']['store_cancel_time'] : 0; $taxiUser = model('common/TaxiUser')->where(['id'=> $taxiUid])->find(); $taxiUserId = isset($taxiUser['user_id'])? $taxiUser['user_id'] : 0; if ($row['status'] == 3 ) { // 免费取消时间内不扣除 if($cancelTime<=0 || $createTime < (time() - $cancelTime*60)){ $total_price = round($total_price * (1 - $config['store']['store_cancel_order']/100), 2); $cost_price = $total-$total_price; } } if ($row->paylog['pay_type'] == 'balance') { model('common/Users')->changeBalance( $user['id'], $total_price, "取消成功,取消金额【" . $total_price . "】", true ); // 退款扣除金额直接给司机 if($taxiUserId && $cost_price>0){ model('common/Users')->changePartnership( $taxiUserId, $cost_price, "取消订单,扣除金额到账【" . $cost_price . "】", 30, true ); } } else if ($row->paylog['pay_type'] == 'property') { model('common/Users')->changeProperty( $user['id'], $total_price, "取消成功,取消资产【{$total_price}】", true ); // 退款扣除金额直接给司机 if($taxiUserId && $cost_price>0){ model('common/Users')->changePartnership( $taxiUserId, $cost_price, "取消订单,扣除金额到账【" . $cost_price . "】", 30, true ); } } else if ($row->paylog['pay_type'] == 'wechat') { // 加载配置 $wechat = sys_config('', 'wechat'); $config = [ // 前面的appid什么的也得保留哦 'app_id' => $wechat['mini_appid'], 'mch_id' => $wechat['pay_mch_id'], 'key' => $wechat['pay_secret_key'], // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书) 'cert_path' => $wechat['cert_path'], // XXX: 绝对路径!!!! 'key_path' => $wechat['key_path'], // XXX: 绝对路径!!!! // 'notify_url' => 'https://api.gxrrj.cn/api/v1/wechat/notify', // 'notify_url' => 'http://rrj.gxnwsoft.com/api/v1/wechat/refundNotify', // 'sandbox' => true ]; // 创建应用实例 $app = Factory::payment($config); // Example: $result = $app->refund->byOutTradeNumber($row->paylog['out_trade_no'], get_order_no(), $row->paylog['total_price'] * 100, $total_price * 100, [ // 可在此处传入其他参数,详细参数见微信支付文档 'refund_desc' => '用户申请退款', ]); app()->log(json_encode($result)); if ($result['return_code'] == 'SUCCESS') { if ($result['result_code'] != 'SUCCESS') { $this->ApiJson(-1, '失败'); } } // 退款扣除金额直接给司机 if($taxiUserId && $cost_price>0){ model('common/Users')->changePartnership( $taxiUserId, $cost_price, "取消订单,扣除金额到账【" . $cost_price . "】", 30, true ); } } } // 后台推送 push_socket_data('motor',[ 'id' => $row['id'], 'msg' => '有用户取消订单,点击前往!' ]); Db::startTrans(); $row->status = 5; $row->settle_price = $cost_price; $row->remark = $param['result']; if(!$row->save()){ Db::rollback(); return IResponse::failure('取消失败'); } if ($row['taxi_id'] && !\app\common\model\Taxi::where(['taxi_user_id' => $taxiUid, 'id' => $row['taxi_id']])->update(['status' => 1])) { Db::rollback(); return IResponse::failure('取消失败'); } Db::commit(); return $this->ApiJson(0, '取消成功', $row); } return $this->ApiJson(-1, '取消失败'); } }