Browse Source

取消ThinkPHP单独版本,加入操作缓存3个重载方法setCache, getCache, removeCache.

dodgepudding 10 years ago
parent
commit
be97cbfd8f
5 changed files with 178 additions and 5324 deletions
  1. 69 0
      Thinkphp/TPWechat.class.php
  2. 0 3224
      Thinkphp/Wechat.class.php
  3. 0 2078
      Thinkphp/qywechat.class.php
  4. 49 11
      qywechat.class.php
  5. 60 11
      wechat.class.php

+ 69 - 0
Thinkphp/TPWechat.class.php

@@ -0,0 +1,69 @@
+<?php
+/**
+ *	微信公众平台PHP-SDK, ThinkPHP实例
+ *  @author dodgepudding@gmail.com
+ *  @link https://github.com/dodgepudding/wechat-php-sdk
+ *  @version 1.2
+ *  usage:
+ *   $options = array(
+ *			'token'=>'tokenaccesskey', //填写你设定的key
+ *			'encodingaeskey'=>'encodingaeskey', //填写加密用的EncodingAESKey
+ *			'appid'=>'wxdk1234567890', //填写高级调用功能的app id
+ *			'appsecret'=>'xxxxxxxxxxxxxxxxxxx' //填写高级调用功能的密钥
+ *		);
+ *	 $weObj = new TPWechat($options);
+ *   $weObj->valid();
+ *   ...
+ *  
+ */
+class TPWechat extends Wechat
+{
+	/**
+	 * log overwrite
+	 * @see Wechat::log()
+	 */
+	protected function log($log){
+		if ($this->debug) {
+			if (function_exists($this->logcallback)) {
+				if (is_array($log)) $log = print_r($log,true);
+				return call_user_func($this->logcallback,$log);
+			}elseif (class_exists('Log')) {
+				Log::write('wechat:'.$log, Log::DEBUG);
+			}
+		}
+		return false;
+	}
+	
+	/**
+	 * 重载设置缓存
+	 * @param string $cachename
+	 * @param mixed $value
+	 * @param int $expired
+	 * @return boolean
+	 */
+	protected function setCache($cachename,$value,$expired){
+		return S($cachename,$value,$expired);
+	}
+	
+	/**
+	 * 重载获取缓存
+	 * @param string $cachename
+	 * @return mixed
+	 */
+	protected function getCache($cachename){
+		return S($cachename);
+	}
+	
+	/**
+	 * 重载清除缓存
+	 * @param string $cachename
+	 * @return boolean
+	 */
+	protected function removeCache($cachename){
+		return S($cachename,null);
+	}
+
+}
+
+
+

File diff suppressed because it is too large
+ 0 - 3224
Thinkphp/Wechat.class.php


File diff suppressed because it is too large
+ 0 - 2078
Thinkphp/qywechat.class.php


+ 49 - 11
qywechat.class.php

@@ -12,7 +12,7 @@
  *			'appsecret'=>'xxxxxxxxxxxxxxxxxxx', //填写高级调用功能的密钥
  *			'agentid'=>'1', //应用的id
  *			'debug'=>false, //调试开关
- *			'_logcallback'=>'logg', //调试输出方法,需要有一个string类型的参数
+ *			'logcallback'=>'logg', //调试输出方法,需要有一个string类型的参数
  *		);
  *
  */
@@ -90,7 +90,7 @@ class Wechat
 	public $debug =  false;
 	public $errCode = 40001;
 	public $errMsg = "no access";
-	private $_logcallback;
+	public $logcallback;
 
 	public function __construct($options)
 	{
@@ -100,13 +100,13 @@ class Wechat
 		$this->appsecret = isset($options['appsecret'])?$options['appsecret']:'';
 		$this->agentid = isset($options['agentid'])?$options['agentid']:'';
 		$this->debug = isset($options['debug'])?$options['debug']:false;
-		$this->_logcallback = isset($options['logcallback'])?$options['logcallback']:false;
+		$this->logcallback = isset($options['logcallback'])?$options['logcallback']:false;
 	}
 
-	private function log($log){
-	    if ($this->debug && function_exists($this->_logcallback)) {
+	protected function log($log){
+	    if ($this->debug && function_exists($this->logcallback)) {
 	        if (is_array($log)) $log = print_r($log,true);
-	        return call_user_func($this->_logcallback,$log);
+	        return call_user_func($this->logcallback,$log);
 	    }
 	}
 
@@ -825,7 +825,38 @@ class Wechat
 </xml>";
 	    return sprintf($format, $encrypt, $signature, $timestamp, $nonce);
 	}
-
+	
+	/**
+	 * 设置缓存,按需重载
+	 * @param string $cachename
+	 * @param mixed $value
+	 * @param int $expired
+	 * @return boolean
+	 */
+	protected function setCache($cachename,$value,$expired){
+		//TODO: set cache implementation
+		return false;
+	}
+	
+	/**
+	 * 获取缓存,按需重载
+	 * @param string $cachename
+	 * @return mixed
+	 */
+	protected function getCache($cachename){
+		//TODO: get cache implementation
+		return false;
+	}
+	
+	/**
+	 * 清除缓存,按需重载
+	 * @param string $cachename
+	 * @return boolean
+	 */
+	protected function removeCache($cachename){
+		//TODO: remove cache implementation
+		return false;
+	}
 
 	/**
 	 * 通用auth验证方法
@@ -842,7 +873,13 @@ class Wechat
 		    $this->access_token=$token;
 		    return $this->access_token;
 		}
-		//TODO: get the cache access_token
+		
+		$authname = 'wechat_access_token'.$appid;
+		if ($rs = $this->getCache($authname))  {
+			$this->access_token = $rs;
+			return $rs;
+		}
+		
 		$result = $this->http_get(self::API_URL_PREFIX.self::TOKEN_GET_URL.'corpid='.$appid.'&corpsecret='.$appsecret);
 		if ($result)
 		{
@@ -854,7 +891,7 @@ class Wechat
 			}
 			$this->access_token = $json['access_token'];
 			$expire = $json['expires_in'] ? intval($json['expires_in'])-100 : 3600;
-			//TODO: cache access_token
+			$this->setCache($authname,$this->access_token,$expire);
 			return $this->access_token;
 		}
 		return false;
@@ -867,7 +904,8 @@ class Wechat
 	public function resetAuth($appid=''){
 		if (!$appid) $appid = $this->appid;
 		$this->access_token = '';
-		//TODO: remove cache
+		$authname = 'wechat_access_token'.$appid;
+		$this->removeCache($authname);
 		return true;
 	}
 
@@ -1506,7 +1544,7 @@ class Wechat
 	public function sendInvite($userid,$invite_tips=''){
 	    $data = array( 'userid' => $userid );
 	    if (!$invite_tips) {
-	    	$data['invite_tips'] = $invite_tips
+	    	$data['invite_tips'] = $invite_tips;
 	    }
 	    if (!$this->access_token && !$this->checkAuth()) return false;
 	    $result = $this->http_post(self::API_URL_PREFIX.self::USER_INVITE_URL.'access_token='.$this->access_token,self::json_encode($data));

+ 60 - 11
wechat.class.php

@@ -172,7 +172,7 @@ class Wechat
 	public $debug =  false;
 	public $errCode = 40001;
 	public $errMsg = "no access";
-	private $_logcallback;
+	public $logcallback;
 
 	public function __construct($options)
 	{
@@ -181,7 +181,7 @@ class Wechat
 		$this->appid = isset($options['appid'])?$options['appid']:'';
 		$this->appsecret = isset($options['appsecret'])?$options['appsecret']:'';
 		$this->debug = isset($options['debug'])?$options['debug']:false;
-		$this->_logcallback = isset($options['logcallback'])?$options['logcallback']:false;
+		$this->logcallback = isset($options['logcallback'])?$options['logcallback']:false;
 	}
 
 	/**
@@ -287,10 +287,15 @@ class Wechat
     		return $this;
     }
 
-    private function log($log){
-    		if ($this->debug && function_exists($this->_logcallback)) {
+    /**
+     * 日志记录,可被重载。
+     * @param mixed $log 输入日志
+     * @return mixed
+     */
+    protected function log($log){
+    		if ($this->debug && function_exists($this->logcallback)) {
     			if (is_array($log)) $log = print_r($log,true);
-    			return call_user_func($this->_logcallback,$log);
+    			return call_user_func($this->logcallback,$log);
     		}
     }
 
@@ -1068,6 +1073,38 @@ class Wechat
 			return false;
 		}
 	}
+	
+	/**
+	 * 设置缓存,按需重载
+	 * @param string $cachename
+	 * @param mixed $value
+	 * @param int $expired
+	 * @return boolean
+	 */
+	protected function setCache($cachename,$value,$expired){
+		//TODO: set cache implementation
+		return false;
+	}
+
+	/**
+	 * 获取缓存,按需重载
+	 * @param string $cachename
+	 * @return mixed
+	 */
+	protected function getCache($cachename){
+		//TODO: get cache implementation
+		return false;
+	}
+	
+	/**
+	 * 清除缓存,按需重载
+	 * @param string $cachename
+	 * @return boolean
+	 */
+	protected function removeCache($cachename){
+		//TODO: remove cache implementation
+		return false;
+	}
 
 	/**
 	 * 获取access_token
@@ -1084,7 +1121,13 @@ class Wechat
 		    $this->access_token=$token;
 		    return $this->access_token;
 		}
-		//TODO: get the cache access_token
+		
+		$authname = 'wechat_access_token'.$appid;
+		if ($rs = $this->getCache($authname))  {
+			$this->access_token = $rs;
+			return $rs;
+		}
+		
 		$result = $this->http_get(self::API_URL_PREFIX.self::AUTH_URL.'appid='.$appid.'&secret='.$appsecret);
 		if ($result)
 		{
@@ -1096,7 +1139,7 @@ class Wechat
 			}
 			$this->access_token = $json['access_token'];
 			$expire = $json['expires_in'] ? intval($json['expires_in'])-100 : 3600;
-			//TODO: cache access_token
+			$this->setCache($authname,$this->access_token,$expire);
 			return $this->access_token;
 		}
 		return false;
@@ -1109,7 +1152,8 @@ class Wechat
 	public function resetAuth($appid=''){
 		if (!$appid) $appid = $this->appid;
 		$this->access_token = '';
-		//TODO: remove cache
+		$authname = 'wechat_access_token'.$appid;
+		$this->removeCache($authname);
 		return true;
 	}
 
@@ -1120,7 +1164,8 @@ class Wechat
 	public function resetJsTicket($appid=''){
 		if (!$appid) $appid = $this->appid;
 		$this->jsapi_ticket = '';
-		//TODO: remove cache
+		$authname = 'wechat_jsapi_ticket'.$appid;
+		$this->removeCache($authname);
 		return true;
 	}
 
@@ -1135,7 +1180,11 @@ class Wechat
 		    $this->jsapi_ticket = $jsapi_ticket;
 		    return $this->access_token;
 		}
-		//TODO: get the cache jsapi_ticket
+		$authname = 'wechat_jsapi_ticket'.$appid;
+		if ($rs = $this->getCache($authname))  {
+			$this->jsapi_ticket = $rs;
+			return $rs;
+		}
 		$result = $this->http_get(self::API_URL_PREFIX.self::GET_TICKET_URL.'access_token='.$this->access_token.'&type=jsapi');
 		if ($result)
 		{
@@ -1147,7 +1196,7 @@ class Wechat
 			}
 			$this->jsapi_ticket = $json['ticket'];
 			$expire = $json['expires_in'] ? intval($json['expires_in'])-100 : 3600;
-			//TODO: cache jsapi_ticket
+			$this->setCache($authname,$this->jsapi_ticket,$expire);
 			return $this->jsapi_ticket;
 		}
 		return false;