diff --git a/app/api/controller/GpsController.php b/app/api/controller/GpsController.php deleted file mode 100644 index b53786a4..00000000 --- a/app/api/controller/GpsController.php +++ /dev/null @@ -1,63 +0,0 @@ - request -> get('imei'); - //验证参数 - if(empty($params)) return $this->fail('参数错误'); - //获取数据 - $result = (new GpsLogic()) -> info($params); - return $result['code'] == 1 ? $this->success($result['msg'],$result['data']) : $this->fail($result['msg']); - } - - public function getCarStatus() { - $result = (new GpsLogic()) -> status(); - return $result['code'] == 1 ? $this->success($result['msg'],$result['data']) : $this->fail($result['msg']); - } - - public function getCarHistory() { - //获取参数 - $params = $this->request->get(['car_id','start_time','end_time']); - //验证参数 - if(empty($params['car_id']) || empty($params['start_time']) || empty($params['end_time'])){ - return $this->fail('参数错误'); - } - //获取车辆行驶历史信息 - $result = (new GpsLogic()) -> history($params); - if($result['code'] == 1){ - $data = []; - foreach ($result['data'] as $k => $v) { - $data[$k]['lat'] = $v['latc']; - $data[$k]['lon'] = $v['lonc']; - } - return $this->success($result['msg'],$data); - }else{ - return $this->fail($result['msg']); - } - } -} diff --git a/app/api/controller/VehicleController.php b/app/api/controller/VehicleController.php index 4de3e117..2b353168 100644 --- a/app/api/controller/VehicleController.php +++ b/app/api/controller/VehicleController.php @@ -11,7 +11,7 @@ use think\response\Json; */ class VehicleController extends BaseApiController { - public array $notNeedLogin = ['getCompany','setContract','getRentRecord','updateRentRecord','getCompanyCars','getCarLocal']; + public array $notNeedLogin = ['getCompany','setContract','getRentRecord','updateRentRecord','getCompanyCars','getCarLocal','getCarHistory']; /* *获取平台公司接口 @@ -28,6 +28,7 @@ class VehicleController extends BaseApiController public function setContract():Json { //获取参数 $params = $this->request->post(['party_a','party_b','num','rent_type','car_id']); + //验证参数 if(empty($params['party_a']) || empty($params['party_b']) || empty($params['num']) || empty($params['rent_type'])){ return $this->fail('缺少必要的参数'); @@ -110,4 +111,25 @@ class VehicleController extends BaseApiController return $this->fail($result['msg']); } } + + public function getCarHistory():Json { + //获取参数 + $params = $this->request->get(['car_id','start_time','end_time']); + //验证参数 + if(empty($params['car_id']) || empty($params['start_time']) || empty($params['end_time'])){ + return $this->fail('缺少必要参数'); + } + //验证时间格式 + if(!checkDateIsValid($params['start_time'],['Y-m-d H:i:s']) || !checkDateIsValid($params['end_time'],['Y-m-d H:i:s'])) { + return $this->fail('日期格式错误'); + } + //获取数据 + $result = VehicleLogic::getCarHistory($params); + //返回数据 + if($result['code'] == 1){ + return $this->success($result['msg'],$result['data']); + }else{ + return $this->fail($result['msg']); + } + } } diff --git a/app/api/logic/GpsLogic.php b/app/api/logic/GpsLogic.php deleted file mode 100644 index 7e673d5f..00000000 --- a/app/api/logic/GpsLogic.php +++ /dev/null @@ -1,129 +0,0 @@ - $this->authName, - 'password' => $this->authPwd - ]); - //验证请求结果 - if(!empty($result['ret']) && isset($result['data'])){ - //设置缓存数据 - Cache::set('gps_user_id', $result['data']['userId'], $this->expTime); - Cache::set('gps_token', $result['data']['token'], $this->expTime); - return $result['data']['token']; - }else{ - return ''; - } - } - - /* - * 获取车辆信息 - */ - public function info($imei):array { - //获取token - $token = $this->token(); - //请求地址 - $url = 'https://www.gpsnow.net/car/getByImei.do'; - //请求参数 - $data = [ - 'token' => $token, - 'imei' => $imei - ]; - //发起请求 - $result = curl_post($url,[],$data); - //返回数据 - if(!empty($result['ret']) && isset($result['data'])){ - return ['code'=>1,'msg'=>'请求成功','data'=>$result['data']]; - }else{ - return ['code'=>0,'msg'=>$result['msg']]; - } - } - - public function status($car_id):array { - //获取token - $token = $this->token(); - //请求地址 - $url = 'https://www.gpsnow.net/car/getCarAndStatus.do'; - //请求参数 - $data = [ - 'token' => $token, - 'carId' => $car_id, - 'mapType' => 1 - ]; - //发起请求 - $result = curl_post($url,[],$data); - //返回数据 - if(!empty($result['ret']) && isset($result['data'])){ - return ['code'=>1,'msg'=>'请求成功','data'=>$result['data']]; - }else{ - return ['code'=>0,'msg'=>$result['msg']]; - } - } - - public function history($params):array { - //获取车辆信息 - $carInfo = Vehicle::where('id',$params['car_id'])->find(); - if(empty($carInfo)) return ['code'=>0,'msg'=>'车辆不存在']; - //获取token - $token = $this->token(); - //请求地址 - $url = 'https://www.gpsnow.net/position/queryHistory.do'; - //请求参数 - $data = [ - 'token' => $token, - 'carId' => $carInfo['gps_carid'], - 'startTime' => $params['start_time'], - 'endTime' => $params['end_time'], - 'filter' => true, - 'interval' => 1, - 'pointType' => 2, - ]; - //发起请求 - $result = curl_post($url,[],$data); - //返回数据 - if(!empty($result['ret']) && isset($result['data'])){ - return ['code'=>1,'msg'=>'请求成功','data'=>$result['data']]; - }else{ - return ['code'=>0,'msg'=>$result['msg']]; - } - } -} \ No newline at end of file diff --git a/app/api/logic/VehicleLogic.php b/app/api/logic/VehicleLogic.php index d8855835..3b11f096 100644 --- a/app/api/logic/VehicleLogic.php +++ b/app/api/logic/VehicleLogic.php @@ -16,6 +16,7 @@ namespace app\api\logic; use app\common\logic\BaseLogic; +use app\common\logic\GpsLogic; use app\common\model\vehicle\Company; use app\common\model\vehicle\Vehicle; use app\common\model\vehicle\VehicleRent; @@ -45,6 +46,7 @@ class VehicleLogic extends BaseLogic Cache::set('task_token', $getToken['data']['token'], 86400); return $getToken['data']['token']; } + public static function companyInfo():array { $data = Company::field('id,company_name')->where('company_name',30)->select(); if($data){ @@ -75,7 +77,7 @@ class VehicleLogic extends BaseLogic ],[ 'id' => $params['party_b'], 'party_a' => $params['party_a'], - 'contract_type' => 29 + 'contract_type' => 29, ]); if($setContract['code'] == 0){ return ['code'=>0,'msg'=>$setContract['msg']]; @@ -226,4 +228,27 @@ class VehicleLogic extends BaseLogic return ['code'=>0,'msg'=>$local['msg']]; } } + + public static function getCarHistory($params):array { + //获取车辆信息 + $car = Vehicle::where('id',$params['car_id'])->find(); + if(empty($car)){ + return ['code'=>0,'msg'=>'车辆不存在']; + }else{ + $params['gps_car_id'] = $car['gps_carid']; + } + //获取车辆行驶历史信息 + $result = (new GpsLogic()) -> history($params); + //返回数据 + if($result['code'] == 1){ + $data = []; + foreach ($result['data'] as $k => $v) { + $data[$k]['lat'] = $v['latc']; + $data[$k]['lon'] = $v['lonc']; + } + return ['code'=>1,'msg'=>$result['msg'],'data'=>$data]; + }else{ + return ['code'=>0,'msg'=>$result['msg']]; + } + } } diff --git a/app/api/route/app.php b/app/api/route/app.php index 19314916..023f79e3 100755 --- a/app/api/route/app.php +++ b/app/api/route/app.php @@ -24,16 +24,11 @@ Route::rule('courierData','Logistics/courierData','get'); Route::rule('hasCourier','Logistics/hasCourier','get'); -/*-------------------------------------------------------------------------------------------*/ -Route::rule('getCarInfo','Gps/getCarInfo','get'); -Route::rule('getCarStatus','Gps/getCarStatus','get'); -Route::rule('getCarHistory','Gps/getCarHistory','get'); - - /*-------------------------------------------------------------------------------------------*/ Route::rule('getCompany','Vehicle/getCompany','get'); Route::rule('setContract','Vehicle/setContract','post'); Route::rule('getRentRecord','Vehicle/getRentRecord','get'); Route::rule('updateRentRecord','Vehicle/updateRentRecord','post'); Route::rule('getCompanyCars','Vehicle/getCompanyCars','get'); -Route::rule('getCarLocal','Vehicle/getCarLocal','get'); \ No newline at end of file +Route::rule('getCarLocal','Vehicle/getCarLocal','get'); +Route::rule('getCarHistory','Vehicle/getCarHistory','get'); \ No newline at end of file diff --git a/app/common/logic/GpsLogic.php b/app/common/logic/GpsLogic.php new file mode 100644 index 00000000..1400f727 --- /dev/null +++ b/app/common/logic/GpsLogic.php @@ -0,0 +1,71 @@ +domain.'/user/login.do',[],[ + 'name' => $this->authName, + 'password' => $this->authPwd + ]); + //验证请求结果 + if(!empty($result['ret']) && isset($result['data'])){ + //设置缓存数据 + Cache::set('gps_token', $result['data']['token'], $this->expTime); + return $result['data']['token']; + }else{ + return ''; + } + } + + public function status($car_id):array { + //获取token + $token = $this->token(); + //发起请求 + $result = curl_post($this->domain.'/car/getCarAndStatus.do',[],[ + 'token' => $token, + 'carId' => $car_id, + 'mapType' => 1 + ]); + //返回数据 + if(!empty($result['ret']) && isset($result['data'])){ + return ['code'=>1,'msg'=>'请求成功','data'=>$result['data']]; + }else{ + return ['code'=>0,'msg'=>$result['msg']]; + } + } + + public function history($params):array { + //获取token + $token = $this->token(); + //发起请求 + $result = curl_post($this->domain.'/position/queryHistory.do',[],[ + 'token' => $token, + 'carId' => $params['gps_car_id'], + 'startTime' => $params['start_time'], + 'endTime' => $params['end_time'], + 'filter' => true, + 'interval' => 1, + 'pointType' => 2, + ]); + //返回数据 + if(!empty($result['ret']) && isset($result['data'])){ + return ['code'=>1,'msg'=>'请求成功','data'=>$result['data']]; + }else{ + return ['code'=>0,'msg'=>$result['msg']]; + } + } +} \ No newline at end of file