logistics/app/api/logic/GpsLogic.php
2023-08-16 14:10:41 +08:00

128 lines
4.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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']];
}
}
}