gps接口

This commit is contained in:
unknown 2023-08-16 14:10:41 +08:00
parent 55bf5749a8
commit d798315cf6
6 changed files with 204 additions and 73 deletions

View File

@ -14,8 +14,7 @@
namespace app\api\controller; namespace app\api\controller;
use think\Exception; use app\api\logic\GpsLogic;
use think\facade\Cache;
/** /**
* 文章管理 * 文章管理
@ -24,82 +23,45 @@ use think\facade\Cache;
*/ */
class GpsController extends BaseApiController class GpsController extends BaseApiController
{ {
public array $notNeedLogin = ['getCarInfo','getCarStatus','getHistory','exportHistory']; public array $notNeedLogin = ['getCarInfo','getCarStatus','getCarHistory','test'];
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 function getCarInfo() { public function getCarInfo() {
//获取token $result = (new GpsLogic()) -> info();
$token = $this->getToken(); return $result['code'] == 1 ? $this->success($result['msg'],$result['data']) : $this->fail($result['msg']);
//请求地址
$url = 'https://www.gpsnow.net/car/getByImei.do';
//请求参数
$data = [
'token' => $token,
'imei' => '863010000029350'
];
$result = curl_post($url,[],$data);
dump($result);
} }
public function getCarStatus() { public function getCarStatus() {
//获取token $result = (new GpsLogic()) -> status();
$token = $this->getToken(); return $result['code'] == 1 ? $this->success($result['msg'],$result['data']) : $this->fail($result['msg']);
//请求地址
$url = 'https://www.gpsnow.net/car/getCarAndStatus.do';
//请求参数
$data = [
'token' => $token,
'carId' => '629942',
'mapType' => 1
];
$result = curl_post($url,[],$data);
dump($result);
} }
public function getHistory() { public function getCarHistory() {
dump(PHP_VERSION);die; //获取参数
//获取token $params = $this->request->get(['car_id','start_time','end_time']);
$token = $this->getToken(); //验证参数
//请求地址 if(empty($params['car_id']) || empty($params['start_time']) || empty($params['end_time'])){
$url = 'https://www.gpsnow.net/position/queryHistory.do'; return $this->fail('参数错误');
//请求参数 }
$data = [ //获取车辆行驶历史信息
'token' => $token, $result = (new GpsLogic()) -> history($params);
'carId' => '629942', if($result['code'] == 1){
'startTime' => '2023-08-15 00:00:00', $data = [];
'endTime' => '2023-08-15 23:59:59', foreach ($result['data'] as $k => $v) {
'filter' => true, $data[$k]['lat'] = $v['latc'];
'interval' => 1, $data[$k]['lon'] = $v['lonc'];
'pointType' => 2, }
return $this->success($result['msg'],$data);
}else{
return $this->fail($result['msg']);
}
}
public function test() {
$message = [
'title' => '测试消息',
'msg_content' => '这是一条新的推送消息'
]; ];
$result = curl_post($url,[],$data); $res = push_message('100d85590992b509d86',json_encode($message));
dump($result); return !($res['code'] == 0);
} }
} }

128
app/api/logic/GpsLogic.php Normal file
View File

@ -0,0 +1,128 @@
<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台PHP版
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用可去除界面版权logo
// | gitee下载https://gitee.com/likeshop_gitee/likeadmin
// | github下载https://github.com/likeshop-github/likeadmin
// | 访问官网https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
namespace app\api\logic;
use app\common\logic\BaseLogic;
use app\common\model\vehicle\Vehicle;
use think\facade\Cache;
/**
* 登录逻辑
* Class GpsLogic
* @package app\api\logic
*/
class GpsLogic extends BaseLogic
{
private string $authName = 'lihai';
private string $authPwd = 'a123456';
private int $expTime = 432000;
/*
* 获取请求token
*/
private function token() {
//获取缓存数据
$gps_token = Cache::get('gps_token');
//验证缓存数据是否存在,如果缓存数据存在则直接返回缓存数据
if(!empty($gps_token)) return $gps_token;
//缓存数据不存在从新请求touken
$url = 'https://www.gpsnow.net/user/login.do';
$result = curl_post($url,[],[
'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 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']];
}
}
}

View File

@ -27,5 +27,6 @@ Route::rule('userMessage','Logistics/sendMessageToApp','get');
/*-------------------------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------------------------*/
Route::rule('getCarInfo','Gps/getCarInfo','get'); Route::rule('getCarInfo','Gps/getCarInfo','get');
Route::rule('getCarStatus','Gps/getCarStatus','get'); Route::rule('getCarStatus','Gps/getCarStatus','get');
Route::rule('getHistory','Gps/getHistory','get'); Route::rule('getCarHistory','Gps/getCarHistory','get');
Route::rule('exportHistory','Gps/exportHistory','get');
Route::rule('test','Gps/test','get');

View File

@ -2,6 +2,7 @@
// 应用公共文件 // 应用公共文件
use app\common\service\FileService; use app\common\service\FileService;
use think\helper\Str; use think\helper\Str;
use JPush\Client;
/** /**
* @notes 生成密码加密密钥 * @notes 生成密码加密密钥
@ -319,3 +320,22 @@ function curl_post($url,$headers,$data) {
curl_close($ch); curl_close($ch);
return json_decode($output,true); 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'=>[]];
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace app\common\model\vehicle;
use app\common\model\BaseModel;
class Vehicle extends BaseModel
{
protected $name = 'vehicle';
public function getCreateTimeAttr($value): string
{
return date('Y-m-d H:i:s',$value);
}
}

View File

@ -29,4 +29,9 @@ return [
'error_message' => '页面错误!请稍后再试~', 'error_message' => '页面错误!请稍后再试~',
// 显示错误信息 // 显示错误信息
'show_error_msg' => false, 'show_error_msg' => false,
// 机关推送
'jpush' => [
'app_key' => '5ced5ec5fa7bb86302944f0f',
'master_secret' => 'd85135e7d8470c90b476e535',
],
]; ];