<?php
/**
 * 建筑接口
 *
 * User: Ellisran
 * Date: 2018/9/27
 * Time: 16:23
 */

class Building {

    private $BuildUrl = 'https://yun.smartcost.com.cn/cld'; // 建筑-cld url接口地址

    public function getUsersAndCompilationByID ($id) {
        $url = $this->BuildUrl. '/getUsersAndCompilation?ssoID='. $id;
        $result = json_decode($this->curl_request($url), true);
        if ($result['error'] == 0) {
            return $result['data'];
        } else {
            return '';
        }
    }

    public function getUsersAndCompilationByMobile ($mobile) {
        $url = $this->BuildUrl. '/getUsersAndCompilation?mobile='. $mobile;
        $result = json_decode($this->curl_request($url), true);
        if ($result['error'] == 0) {
            return $result['data'];
        } else {
            return '';
        }
    }

    public function setUserCompilation($ssoId, $compilationId) {
        $url =  $this->BuildUrl. '/setUserUpgrade';
        $data = array('ssoId' => $ssoId, 'cid' => $compilationId);
        $result = json_decode($this->curl_request($url, $data), true);
        if ($result['error'] == 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * curl 获取接口数据，data为空时为GET方法，有值则为POST方法
     *
     * @param $url
     * @param string $data
     * @return mixed|string
     */
    private function curl_request($url, $data = '') {

        $curl = curl_init();

        curl_setopt($curl, CURLOPT_URL, $url);

        curl_setopt($curl, CURLOPT_HEADER, 0);

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        if($data) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
        }

        curl_setopt($curl, CURLOPT_TIMEOUT, 10);

        $data = curl_exec($curl);

        curl_close($curl);

        return $data;
    }
}