128 lines
4.1 KiB
PHP
128 lines
4.1 KiB
PHP
<?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($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']];
|
||
}
|
||
}
|
||
} |