<?php
namespace slock;
/**
* 劲卫智能门锁
*/
class Jwlock{

	private $appid="wxrho8a0d0lumi52ep";
	private $appApiSecret="v2e7w8wavwm07wo95f778eu7y8hliojt";

	private $api_url="http://api.weixinlock.com";
	public function __construct($config=[]){
		if(isset($config['appid'])&&isset($config['appApiSecret'])){
			$this->appid=$config['appid'];
			$this->appApiSecret=$config['appApiSecret'];
		}
	}
	public function qrcode($pmsNo="0109",$startTime="1631852051",$endTime="1631853051",$phoneNo="15860162181"){
		$timestamp=time();
		$nonceStr=time();
		$sign=self::createSign($nonceStr,$timestamp);
		$param['appId']=$this->appid;
		$param['nonceStr']=$nonceStr;
		$param['timestamp']=$timestamp;
		$param['sign']=$sign;
		$param['version']=1;
		$param['lockSn']=$pmsNo;
		$param['startTime']=date('YmdHi',$startTime);
		$param['endTime']=date('YmdHi',$endTime);
		$param['keyFlag']="ADD";
		$param['keyType']="QR_CODE_TIME";
		$param['phoneNo']=$phoneNo;
		$param=http_build_query($param);
		$url=$this->api_url.'/api/platform/lock/checkIn?'.$param;
		$res=self::httpRequest($url);
		$res=json_decode($res,1);
		if($res['statusCode']=="success"){
			return ['code'=>0,'data'=>$res['result']['qrTimeCodeHex']];
		}else{
			return ['code'=>1,'msg'=>$res['message']];
		}
	}
	public function oneKeyLock($pmsNo="0109",$phoneNo="15860162181"){
		$timestamp=time();
		$ret=self::qrcode($pmsNo,$timestamp,$timestamp+300,$phoneNo);
		if($ret['code']==1){
			return $ret;
		}
		$nonceStr=time();
		$sign=self::createSign($nonceStr,$timestamp);
		$param['appId']=$this->appid;
		$param['nonceStr']=$nonceStr;
		$param['timestamp']=$timestamp;
		$param['sign']=$sign;
		$param['version']=1;
		$param['lockSn']=$pmsNo;
		$param['phoneNo']=$phoneNo;
		$url=$this->api_url.'/api/v1/platform/lock/sendNetCmd';
		$res=self::httpRequest($url,'POST',$param);
		$res=json_decode($res,1);
		if($res['statusCode']=="success"){
			return ['code'=>0,'data'=>$res['result']['pmsNo']];
		}else{
			return ['code'=>1,'msg'=>$res['message']];
		}
	}
	//生成签名
	public function createSign($nonceStr='',$time=''){
        $param[]=$this->appApiSecret;
		$param[]=$nonceStr;
		$param[]=$time;
		rsort($param);
		$param=implode('',$param);
		return md5($param);
	}
	public function httpRequest($url, $method = "GET", $postfields = null, $headers = array(), $debug = false)
	{
	    $method = strtoupper($method);
	    $ci = curl_init();
	    /* Curl settings */
	    curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);//版本
	    curl_setopt($ci, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");//在HTTP请求中包含一个"User-Agent: "头的字符串。
	    curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 60); /* 在发起连接前等待的时间，如果设置为0，则无限等待 */
	    curl_setopt($ci, CURLOPT_TIMEOUT, 7); /* 设置cURL允许执行的最长秒数 */
	    curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);//将curl_exec()获取的信息以文件流的形式返回，而不是直接输出。
	    switch ($method) {
	        case "POST":
	            curl_setopt($ci, CURLOPT_POST, true);//启用时会发送一个常规的POST请求，类型为：application/x-www-form-urlencoded，就像表单提交的一样。
	            if (!empty($postfields)) {
	                $tmpdatastr = is_array($postfields) ? http_build_query($postfields) : $postfields;
	                curl_setopt($ci, CURLOPT_POSTFIELDS, $tmpdatastr);
	            }
	            break;
	        default:
	            curl_setopt($ci, CURLOPT_CUSTOMREQUEST, $method); /* //设置请求方式 */
	            break;
	    }
	    $ssl = preg_match('/^https:\/\//i', $url) ? TRUE : FALSE;
	    curl_setopt($ci, CURLOPT_URL, $url);//需要获取的URL地址，也可以在curl_init()函数中设置
	    if ($ssl) {
	        curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
	        curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, FALSE); // 不从证书中检查SSL加密算法是否存在
	    }
	    //curl_setopt($ci, CURLOPT_HEADER, true); /*启用时会将头文件的信息作为数据流输出*/
	    curl_setopt($ci, CURLOPT_FOLLOWLOCATION, 1);
	    curl_setopt($ci, CURLOPT_MAXREDIRS, 2);/*指定最多的HTTP重定向的数量，这个选项是和CURLOPT_FOLLOWLOCATION一起使用的*/
	    curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
	    curl_setopt($ci, CURLINFO_HEADER_OUT, true);
	    /*curl_setopt($ci, CURLOPT_COOKIE, $Cookiestr); * *COOKIE带过去** */
	    $response = curl_exec($ci);
	    $requestinfo = curl_getinfo($ci);
	    curl_close($ci);
	    return $response;
	}
}