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