Explorar o código

根据 @pkkgu 提出的需要,为checkAuth()增加一个$token参数,用于在特殊情况
下手动指定access_token值,以避免每次重复获取access_token到达API请求限值。

binsee %!s(int64=10) %!d(string=hai) anos
pai
achega
6da1880ef5

+ 2 - 2
README.md

@@ -67,7 +67,7 @@ https://mp.weixin.qq.com/cgi-bin/readtemplate?t=business/course2_tmpl&lang=zh_CN
 ```
 
 ### 新增Auth高级权限类方法:   
- *  checkAuth($appid,$appsecret) 此处传入公众后台高级接口提供的appid和appsecret, 函数将返回access_token操作令牌
+ *  checkAuth($appid,$appsecret,$token) 此处传入公众后台高级接口提供的appid和appsecret, 或者手动指定$token为access_token。函数将返回access_token操作令牌
  *  createMenu($data) 创建菜单 $data菜单结构详见 http://mp.weixin.qq.com/wiki/index.php?title=%E8%87%AA%E5%AE%9A%E4%B9%89%E8%8F%9C%E5%8D%95%E5%88%9B%E5%BB%BA%E6%8E%A5%E5%8F%A3 
  *  getMenu() 获取菜单 
  *  deleteMenu() 删除菜单 
@@ -252,7 +252,7 @@ $options = array(
 * reply() 将已经设置好的消息,回复给微信服务器
 
 ### 主动接口方法:
-* checkAuth($appid='',$appsecret='') 通用auth验证方法,也用来换取ACCESS_TOKEN
+* checkAuth($appid='',$appsecret='',$token='') 通用auth验证方法,也用来换取ACCESS_TOKEN 。仅在需要手动指定access_token时才用`$token`
 * resetAuth($appid='') 清除记录的ACCESS_TOKEN
 * createMenu($data,$agentid='') 创建菜单,参数:菜单内容数组,要创建菜单应用id
 * getMenu($agentid='') 获取菜单内容,参数:要获取菜单内容的应用id

+ 6 - 1
Thinkphp/Wechat.class.php

@@ -833,13 +833,18 @@ class Wechat
 	 * 通用auth验证方法,暂时仅用于菜单更新操作
 	 * @param string $appid
 	 * @param string $appsecret
+	 * @param string $token 手动指定access_token,非必要情况不建议用
 	 */
-	public function checkAuth($appid='',$appsecret=''){
+	public function checkAuth($appid='',$appsecret='',$token=''){
 		if (!$appid || !$appsecret) {
 			$appid = $this->appid;
 			$appsecret = $this->appsecret;
 		}
 		$authname = 'wechat_access_token'.$appid;
+		if ($token) { //手动指定token,优先使用
+		    $this->access_token=$token;
+		    return $this->access_token;
+		}
 		if ($rs = S($authname))  {
 			$this->access_token = $rs;
 			return $rs;

+ 6 - 1
Thinkphp/qywechat.class.php

@@ -702,13 +702,18 @@ class Wechat
 	 * 通用auth验证方法
 	 * @param string $appid
 	 * @param string $appsecret
+	 * @param string $token 手动指定access_token,非必要情况不建议用
 	 */
-	public function checkAuth($appid='',$appsecret=''){
+	public function checkAuth($appid='',$appsecret='',$token=''){
 		if (!$appid || !$appsecret) {
 			$appid = $this->appid;
 			$appsecret = $this->appsecret;
 		}
 		$authname = 'qywechat_access_token'.$appid;
+		if ($token) { //手动指定token,优先使用
+		    $this->access_token=$token;
+		    return $this->access_token;
+		}
 		if ($rs = S($authname))  {
 			$this->access_token = $rs;
 			return $rs;

+ 6 - 1
qywechat.class.php

@@ -697,12 +697,17 @@ class Wechat
 	 * 通用auth验证方法
 	 * @param string $appid
 	 * @param string $appsecret
+	 * @param string $token 手动指定access_token,非必要情况不建议用
 	 */
-	public function checkAuth($appid='',$appsecret=''){
+	public function checkAuth($appid='',$appsecret='',$token=''){
 		if (!$appid || !$appsecret) {
 			$appid = $this->appid;
 			$appsecret = $this->appsecret;
 		}
+		if ($token) { //手动指定token,优先使用
+		    $this->access_token=$token;
+		    return $this->access_token;
+		}
 		//TODO: get the cache access_token
 		$result = $this->http_get(self::API_URL_PREFIX.self::TOKEN_GET_URL.'corpid='.$appid.'&corpsecret='.$appsecret);
 		if ($result)

+ 6 - 1
wechat.class.php

@@ -830,12 +830,17 @@ class Wechat
 	 * 通用auth验证方法,暂时仅用于菜单更新操作
 	 * @param string $appid
 	 * @param string $appsecret
+	 * @param string $token 手动指定access_token,非必要情况不建议用
 	 */
-	public function checkAuth($appid='',$appsecret=''){
+	public function checkAuth($appid='',$appsecret='',$token=''){
 		if (!$appid || !$appsecret) {
 			$appid = $this->appid;
 			$appsecret = $this->appsecret;
 		}
+		if ($token) { //手动指定token,优先使用
+		    $this->access_token=$token;
+		    return $this->access_token;
+		}
 		//TODO: get the cache access_token
 		$result = $this->http_get(self::API_URL_PREFIX.self::AUTH_URL.'appid='.$appid.'&secret='.$appsecret);
 		if ($result)

+ 1 - 1
wiki/企业号API类库.md

@@ -62,7 +62,7 @@ $options = array(
 * reply() 将已经设置好的消息,回复给微信服务器
 
 ## 主动接口方法:
-* checkAuth($appid='',$appsecret='') 通用auth验证方法,也用来换取ACCESS_TOKEN
+* checkAuth($appid='',$appsecret='',$token='') 通用auth验证方法,也用来换取ACCESS_TOKEN 。仅在需要手动指定access_token时才用`$token`
 * resetAuth($appid='') 清除记录的ACCESS_TOKEN
 * createMenu($data,$agentid='') 创建菜单,参数:菜单内容数组,要创建菜单应用id
 * getMenu($agentid='') 获取菜单内容,参数:要获取菜单内容的应用id

+ 1 - 1
wiki/官方API类库.md

@@ -38,7 +38,7 @@
 ```
 
 ## 新增Auth高级权限类方法:   
- *  checkAuth($appid,$appsecret) 此处传入公众后台高级接口提供的appid和appsecret, 函数将返回access_token操作令牌
+ *  checkAuth($appid,$appsecret,$token) 此处传入公众后台高级接口提供的appid和appsecret, 或者手动指定$token为access_token。函数将返回access_token操作令牌
  *  createMenu($data) 创建菜单 $data菜单结构详见 http://mp.weixin.qq.com/wiki/index.php?title=%E8%87%AA%E5%AE%9A%E4%B9%89%E8%8F%9C%E5%8D%95%E5%88%9B%E5%BB%BA%E6%8E%A5%E5%8F%A3 
  *  getMenu() 获取菜单 
  *  deleteMenu() 删除菜单