diff --git a/app/api/controller/GpsController.php b/app/api/controller/GpsController.php index af5c569d..f2a1cb75 100644 --- a/app/api/controller/GpsController.php +++ b/app/api/controller/GpsController.php @@ -14,8 +14,7 @@ namespace app\api\controller; -use think\Exception; -use think\facade\Cache; +use app\api\logic\GpsLogic; /** * 文章管理 @@ -24,82 +23,45 @@ use think\facade\Cache; */ class GpsController extends BaseApiController { - public array $notNeedLogin = ['getCarInfo','getCarStatus','getHistory','exportHistory']; - - private string $authName = 'lihai'; - private string $authPwd = 'a123456'; - private int $expTime = 432000; - - private function getToken() { - $gps_token = Cache::get('gps_token'); - if(!empty($gps_token)){ - return $gps_token; - } - $url = 'https://www.gpsnow.net/user/login.do'; - $data = [ - 'name' => $this->authName, - 'password' => $this->authPwd - ]; - try { - $result = curl_post($url,[],$data); - if(!empty($result) && !empty($result['data']['token'])){ - Cache::set('gps_token', $result['data']['token'], $this->expTime); - return $result['data']['token']; - - }else{ - return ''; - } - }catch (\Exception $e) { - return ''; - } - } + public array $notNeedLogin = ['getCarInfo','getCarStatus','getCarHistory','test']; public function getCarInfo() { - //获取token - $token = $this->getToken(); - //请求地址 - $url = 'https://www.gpsnow.net/car/getByImei.do'; - //请求参数 - $data = [ - 'token' => $token, - 'imei' => '863010000029350' - ]; - $result = curl_post($url,[],$data); - dump($result); + $result = (new GpsLogic()) -> info(); + return $result['code'] == 1 ? $this->success($result['msg'],$result['data']) : $this->fail($result['msg']); } public function getCarStatus() { - //获取token - $token = $this->getToken(); - //请求地址 - $url = 'https://www.gpsnow.net/car/getCarAndStatus.do'; - //请求参数 - $data = [ - 'token' => $token, - 'carId' => '629942', - 'mapType' => 1 - ]; - $result = curl_post($url,[],$data); - dump($result); + $result = (new GpsLogic()) -> status(); + return $result['code'] == 1 ? $this->success($result['msg'],$result['data']) : $this->fail($result['msg']); } - public function getHistory() { - dump(PHP_VERSION);die; - //获取token - $token = $this->getToken(); - //请求地址 - $url = 'https://www.gpsnow.net/position/queryHistory.do'; - //请求参数 - $data = [ - 'token' => $token, - 'carId' => '629942', - 'startTime' => '2023-08-15 00:00:00', - 'endTime' => '2023-08-15 23:59:59', - 'filter' => true, - 'interval' => 1, - 'pointType' => 2, + 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']); + } + } + + public function test() { + $message = [ + 'title' => '测试消息', + 'msg_content' => '这是一条新的推送消息' ]; - $result = curl_post($url,[],$data); - dump($result); + $res = push_message('100d85590992b509d86',json_encode($message)); + return !($res['code'] == 0); } } diff --git a/app/api/logic/GpsLogic.php b/app/api/logic/GpsLogic.php new file mode 100644 index 00000000..8304cfa0 --- /dev/null +++ b/app/api/logic/GpsLogic.php @@ -0,0 +1,128 @@ + $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 info():array { + //获取token + $token = $this->token(); + //请求地址 + $url = 'https://www.gpsnow.net/car/getByImei.do'; + //请求参数 + $data = [ + 'token' => $token, + 'imei' => '863010000029350' + ]; + //发起请求 + $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():array { + //获取token + $token = $this->token(); + //请求地址 + $url = 'https://www.gpsnow.net/car/getCarAndStatus.do'; + //请求参数 + $data = [ + 'token' => $token, + 'carId' => '629942', + '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/route/app.php b/app/api/route/app.php index efc9af51..a6377520 100755 --- a/app/api/route/app.php +++ b/app/api/route/app.php @@ -27,5 +27,6 @@ Route::rule('userMessage','Logistics/sendMessageToApp','get'); /*-------------------------------------------------------------------------------------------*/ Route::rule('getCarInfo','Gps/getCarInfo','get'); Route::rule('getCarStatus','Gps/getCarStatus','get'); -Route::rule('getHistory','Gps/getHistory','get'); -Route::rule('exportHistory','Gps/exportHistory','get'); \ No newline at end of file +Route::rule('getCarHistory','Gps/getCarHistory','get'); + +Route::rule('test','Gps/test','get'); \ No newline at end of file diff --git a/app/common.php b/app/common.php index e146edd2..d24456c5 100755 --- a/app/common.php +++ b/app/common.php @@ -2,6 +2,7 @@ // 应用公共文件 use app\common\service\FileService; use think\helper\Str; +use JPush\Client; /** * @notes 生成密码加密密钥 @@ -319,3 +320,22 @@ function curl_post($url,$headers,$data) { curl_close($ch); return json_decode($output,true); } + +function push_message($reg_id,$message){ + //获取配置信息 + $jpush_config = config('app.jpush'); + $app_key= $jpush_config['app_key']; //这是app密钥,填你自己的 + $master_secret= $jpush_config['master_secret']; //这也是密钥,填你自己的 + //实例化 + $client = new Client($app_key,$master_secret); + $pusher = $client->push(); + $pusher->setPlatform('all'); + $pusher->addRegistrationId($reg_id); + $pusher->setNotificationAlert($message); + try { + $res = $pusher->send(); + return ['code'=>1,'msg'=>'','data'=>$res]; + } catch (\JPush\Exceptions\JPushException $e) { + return ['code'=>0,'msg'=>$e->getMessage(),'data'=>[]]; + } +} diff --git a/app/common/model/vehicle/Vehicle.php b/app/common/model/vehicle/Vehicle.php new file mode 100644 index 00000000..e89ae14d --- /dev/null +++ b/app/common/model/vehicle/Vehicle.php @@ -0,0 +1,15 @@ + '页面错误!请稍后再试~', // 显示错误信息 'show_error_msg' => false, + // 机关推送 + 'jpush' => [ + 'app_key' => '5ced5ec5fa7bb86302944f0f', + 'master_secret' => 'd85135e7d8470c90b476e535', + ], ];