| 
					
				 | 
			
			
				@@ -20,6 +20,9 @@ class WeixinJsSDK extends Controller { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	private $objMemache; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	public function __construct() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		$this->objMemache = new Memcached (); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		$wechat_cfg = Config::getInstance ()->get ( 'wechat_cfg' ); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		$this->appId = $wechat_cfg ['appId']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		$this->appSecret = $wechat_cfg ['appSecret']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	/** 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -36,9 +39,7 @@ class WeixinJsSDK extends Controller { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	 * getSignPackage 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	 */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	private function getSignPackage($url = null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		$wechat_cfg = Config::getInstance ()->get ( 'wechat_cfg' ); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		$appid = $wechat_cfg ['appId']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		$jsapiTicket = $this->getJsApiTicket (); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		if ($url == null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -51,7 +52,7 @@ class WeixinJsSDK extends Controller { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		$signature = sha1 ( $string ); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		$signPackage = array ( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-				"appId" => $appid, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				"appId" => $this->appId, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 				"nonceStr" => $nonceStr, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 				"timestamp" => $timestamp, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 				"url" => $url, 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -77,8 +78,22 @@ class WeixinJsSDK extends Controller { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	 * getJsApiTicket 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	 */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	private function getJsApiTicket() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		$ticket = $this->objMemache->get ( RefreshAccessToken::JSAPI_TICKET_KEY ); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		return $ticket; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		$data = json_decode($this->objMemache->get('ticket')); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	    if ($data->expire_time < time()) {   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	      $accessToken = $this->getAccessToken();   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	      $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=' . $accessToken;   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	      $res = json_decode($this->httpGet($url));   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	      $ticket = $res->ticket;   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	      if ($ticket) {   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	        $data->expire_time = time() + 7000;   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	        $data->jsapi_ticket = $ticket;   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	        $this->objMemache->set('ticket', json_encode($data)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	      }   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	    } else {   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	      $ticket = $data->jsapi_ticket;   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	    }   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	    return $ticket;  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	/* 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -89,4 +104,35 @@ class WeixinJsSDK extends Controller { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		$action = $this->action; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		$this->$action (); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	private function getAccessToken() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		$data = json_decode($this->objMemache->get('token')); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if ($data->expire_time < time()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appId.'&secret=' . $this->appSecret; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			$res = json_decode($this->httpGet($url)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			$access_token = $res->access_token; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			if ($access_token) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				$data->expire_time = time() + 7000; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				$data->access_token = $access_token; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				$this->objMemache->set('token', json_encode($data)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} else {   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			$access_token = $data->access_token;   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		return $access_token;   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	private function httpGet($url) {   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	    $curl = curl_init();   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	    curl_setopt($curl, CURLOPT_TIMEOUT, 500);   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	    curl_setopt($curl, CURLOPT_URL, $url);   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	    $res = curl_exec($curl);   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	    curl_close($curl);   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	    return $res; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 |