123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- <?php
- namespace app\watch\controller;
- use think\App;
- use think\facade\Log;
- class Api extends Base {
- protected $testUrl = 'http://120.24.56.48:8889';
- protected $produceUrl = 'http://api.aiqiangua.com:8888';
- protected $companyName = '京宜安科技';
- protected $userName = '13838379499';
- protected $pwd = 'hzd2021';
- protected $imeI = '863204050000136';
- protected $communityId = '609b36c615f7164e6ff56246';
- public function __construct(App $app) {
- parent::__construct($app);
- }
- /**
- * 查看人员信息
- *
- * @author wst
- * @date 2021/5/31 17:10
- */
- public function getPerson($personId){
- $url = $this->produceUrl . '/api/person/'.$personId;
- $data = $this->getApiContents($url,'GET',[],$this->getCookie());
- return $data;
- }
- /**
- * 查看指定设备
- *
- * @author wst
- * @date 2021/5/31 14:06
- */
- public function getDevice($deviceId){
- $url = $this->produceUrl . '/api/device/'.$deviceId;
- $data = $this->getApiContents($url,'GET',[],$this->getCookie());
- return $data;
- }
- /**
- * 批量获取设备在离线状态//列表
- *
- * @author wst
- * @date 2021/5/31 17:43
- */
- public function community($page=1,$pageSize=20){
- $url = $this->produceUrl . '/api/community/'.$this->communityId.'/devicesloc_new?rows_per_page='.$pageSize.'&page='.$page;
- $data = $this->getApiContents($url,'GET',[],$this->getCookie());
- return $data;
- }
- public function t(){
- echo "<pre/>";
- echo nDate(1620814184843);
- die;
- print_r($this->community());
- die;
- }
- /**
- * 修改设备
- *
- * @author wst
- * @date 2021/5/31 14:44
- */
- public function editDevice($deviceId,$params){
- $url = $this->produceUrl . '/api/device/'.$deviceId.'/4g/edit?'.http_build_query($params);
- $data = $this->getApiContents($url,'GET',[],$this->getCookie());
- return $data;
- }
- /**
- * 设置提醒
- *
- * @author wst
- * @date 2021/5/31 15:09
- * @alertId 提醒编号 设备详情->alert->seqid 字段值
- */
- public function deviceAlert($deviceId,$alertId,$params){
- $url = $this->produceUrl . '/api/device/'.$deviceId.'/4g/alerts/'.$alertId.'?'.http_build_query($params);
- $data = $this->getApiContents($url,'GET',[],$this->getCookie());
- return $data;
- }
- /**
- * 设置亲情号码
- *
- * @author wst
- * @date 2021/5/31 15:31
- * @sos_numbers 亲情编号 设备详情->sos_numbers->seqid 字段值
- */
- public function deviceSosNumbers($deviceId,$sos_numbers,$params){
- $url = $this->produceUrl . '/api/device/'.$deviceId.'/4g/sos_numbers/'.$sos_numbers.'?'.http_build_query($params);
- $data = $this->getApiContents($url,'GET',[],$this->getCookie());
- return $data;
- }
- /**
- * 设置安全区域
- *
- * @author wst
- * @date 2021/5/31 15:34
- */
- public function deviceFences($deviceId,$FencesId,$params){
- $url = $this->produceUrl . '/api/device/'.$deviceId.'/4g/fences/'.$FencesId.'?'.http_build_query($params);
- $data = $this->getApiContents($url,'GET',[],$this->getCookie());
- return $data;
- }
- /**
- * 在线设备操作,获取在线设备实时数据
- *
- * @author wst
- * @date 2021/5/31 15:56
- */
- public function deviceAction($deviceId,$action){
- $url = $this->produceUrl . '/api/device/'.$deviceId.'/4g/action/'.$action;
- $data = $this->getApiContents($url,'GET',[],$this->getCookie());
- return $data;
- }
- /**
- * 查看设备开关机数据
- *
- * @author wst
- * @date 2021/5/31 16:04
- */
- public function devicePowerData($deviceId,$params){
- $params['device'] = $deviceId;
- $url = $this->produceUrl . '/api/powerdata/?'.http_build_query($params);
- $data = $this->getApiContents($url,'GET',[],$this->getCookie());
- return $data;
- }
- /**
- * 查看设备定位数据
- *
- * @author wst
- * @date 2021/5/31 16:04
- */
- public function deviceLocationData($deviceId,$params){
- $params['device'] = $deviceId;
- $url = $this->produceUrl . '/api/locationdata/?'.http_build_query($params);
- $data = $this->getApiContents($url,'GET',[],$this->getCookie());
- return $data;
- }
- /**
- * 查看设备运动数据
- *
- * @author wst
- * @date 2021/5/31 16:28
- */
- public function devicePedometerData($deviceId,$params){
- $params['device'] = $deviceId;
- $url = $this->produceUrl . '/api/pedometerdata/?'.http_build_query($params);
- $data = $this->getApiContents($url,'GET',[],$this->getCookie());
- return $data;
- }
- /**
- * 查看设备心率数据
- *
- * @author wst
- * @date 2021/5/31 16:28
- */
- public function deviceHeartRatData($deviceId,$params){
- $params['device'] = $deviceId;
- $url = $this->produceUrl . '/api/heartratedata/?'.http_build_query($params);
- $data = $this->getApiContents($url,'GET',[],$this->getCookie());
- return $data;
- }
- /**
- * 查看设备睡眠数据
- *
- * @author wst
- * @date 2021/5/31 16:28
- */
- public function deviceSleepData($deviceId,$params){
- $params['device'] = $deviceId;
- $url = $this->produceUrl . '/api/sleepdata/?'.http_build_query($params);
- $data = $this->getApiContents($url,'GET',[],$this->getCookie());
- return $data;
- }
- /**
- * 查看设备答复数据
- *
- * @author wst
- * @date 2021/5/31 16:28
- */
- public function deviceReplyData($deviceId,$params){
- $params['device'] = $deviceId;
- $url = $this->produceUrl . '/api/replydata/?'.http_build_query($params);
- $data = $this->getApiContents($url,'GET',[],$this->getCookie());
- return $data;
- }
- /**
- * 查看设备血压数据
- *
- * @author wst
- * @date 2021/5/31 16:28
- */
- public function deviceBloodPressureData($deviceId,$params){
- $params['device'] = $deviceId;
- $url = $this->produceUrl . '/api/bloodpressuredata/?'.http_build_query($params);
- $data = $this->getApiContents($url,'GET',[],$this->getCookie());
- return $data;
- }
- /**
- * 查看设备血氧数据
- *
- * @author wst
- * @date 2021/5/31 16:28
- */
- public function deviceDoXGenData($deviceId,$params){
- $params['device'] = $deviceId;
- $url = $this->produceUrl . '/api/bloodoxygendata/?'.http_build_query($params);
- $data = $this->getApiContents($url,'GET',[],$this->getCookie());
- return $data;
- }
- /**
- * 查看设备紧急呼叫
- *
- * @author wst
- * @date 2021/5/31 16:28
- */
- public function deviceSosData($deviceId,$params){
- $params['device'] = $deviceId;
- $url = $this->produceUrl . '/api/sosdata/?'.http_build_query($params);
- $data = $this->getApiContents($url,'GET',[],$this->getCookie());
- return $data;
- }
- /**
- * 查看跌倒数据
- *
- * @author wst
- * @date 2021/5/31 16:28
- */
- public function deviceFallData($deviceId,$params){
- $params['device'] = $deviceId;
- $url = $this->produceUrl . '/api/falldata/?'.http_build_query($params);
- $data = $this->getApiContents($url,'GET',[],$this->getCookie());
- return $data;
- }
- /**
- * 获取设备最新位置数据
- *
- * @author wst
- * @date 2021/5/31 16:28
- */
- public function deviceNewLocationData($deviceId,$params){
- $url = $this->produceUrl . '/api/device/'.$deviceId.'/data/locationdata?'.http_build_query($params);
- $data = $this->getApiContents($url,'GET',[],$this->getCookie());
- return $data;
- }
- /**
- * 登录
- *
- * @author wst
- * @date 2021/5/31 14:06
- */
- public function getCookie() {
- $cookie = cache('aqg_cookie');
- //\think\facade\Cache::delete('aqg_cookie');
- if(empty($cookie)|| $cookie=='false'){
- $url = $this->produceUrl . '/api/auth/login';
- $cookie = $this->getApiContents($url, 'POST',
- ['username' => $this->userName, 'password' => $this->pwd], '', true);
- cache('aqg_cookie',$cookie,86400);
- }
- return $cookie;
- }
- /**
- * 获取接口内容及相应headers详情
- * @param string $url 请求的API地址
- * @param array $post POST所需提交的数据
- * @param string $token 验证的TOKEN,放header里
- * @param bool $returnHeader 是否需要查看response header内容
- * @return array
- */
- public function getApiContents($url, $method, $post = array(), $token = '', $returnHeader = false) {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url); // 需要获取的 URL 地址,也可以在curl_init() 初始化会话的时候。
- curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
- if (!empty($token)) {
- $header = ['Cookie: ' . $token]; //设置一个你的浏览器的header
- curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
- }
- /* 如果不设置这个则无法获取response header内容 */
- curl_setopt($curl, CURLOPT_HEADER, $returnHeader);
- curl_setopt($curl, CURLOPT_TIMEOUT, 10);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- // 执行 cURL 会话
- $data = curl_exec($curl);
- $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
- if (curl_errno($curl)) {
- return curl_error($curl);
- }
- // 关闭 cURL 会话
- curl_close($curl);
- $info['code'] = $httpCode;
- $info['cookie'] = '';
- if ($returnHeader) {
- list($header, $body) = explode("\r\n\r\n", $data, 2);
- $h = explode("\r", $header);
- foreach ($h as $k => $value) {
- if (strpos($value,'Set-Cookie')!==false) {
- $c = trim(str_replace('Set-Cookie:','',$value));
- $cc = explode(';',$c);
- $info['cookie'] = $cc[0];
- }
- }
- $info['data'] = json_decode($body,true);
- if (empty($info['cookie'])) {
- log::info('登录失败:' . $body);
- }
- }
- else {
- $info['data'] = json_decode($data,true);
- }
- if(!$returnHeader){
- if(!isset($info['data']['success'])) $this->error('系统错误!');
- return $info['data'];
- }
- return $info['cookie'];
- }
- }
|