105 lines
3.4 KiB
PHP
105 lines
3.4 KiB
PHP
<?php
|
||
|
||
namespace app\common\logic;
|
||
|
||
use think\facade\Cache;
|
||
|
||
class GpsLogic extends BaseLogic
|
||
{
|
||
private string $authName = 'lihai';
|
||
private string $authPwd = 'a123456';
|
||
private int $expTime = 432000;
|
||
private string $domain = 'https://www.gpsnow.net';
|
||
|
||
private function token() {
|
||
//获取缓存数据
|
||
$gps_token = Cache::get('gps_token');
|
||
//验证缓存数据是否存在,如果缓存数据存在则直接返回缓存数据
|
||
if(!empty($gps_token)) return $gps_token;
|
||
//缓存数据不存在,从新请求token
|
||
$result = curl_post($this->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 info($imei) {
|
||
//获取token
|
||
$token = $this->token();
|
||
//发起请求
|
||
$result = curl_post($this->domain.'/car/getByImei.do',[],[
|
||
'token' => $token,
|
||
'imei' => $imei,
|
||
]);
|
||
//返回数据
|
||
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();
|
||
//发起请求
|
||
$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']];
|
||
}
|
||
}
|
||
|
||
public function mileage($carId,$startTime,$endTime):array {
|
||
//获取token
|
||
$token = $this->token();
|
||
//发起请求
|
||
$result = curl_post($this->domain.'/position/mileageStaByDay.do',[],[
|
||
'token' => $token,
|
||
'carId' => $carId,
|
||
'startTime' => $startTime.' 00:00:00',
|
||
'endTime' => $endTime.' 23:59:59'
|
||
]);
|
||
//返回数据
|
||
if(!empty($result['ret']) && isset($result['data'])){
|
||
return ['code'=>1,'msg'=>'请求成功','data'=>$result['data']];
|
||
}else{
|
||
return ['code'=>0,'msg'=>$result['msg']];
|
||
}
|
||
}
|
||
} |