Browse Source

企业号类 增加 JSAPI相关方法,修改缓存前缀

binsee 10 years ago
parent
commit
0af174da79
2 changed files with 113 additions and 7 deletions
  1. 1 0
      qyerrCode.php
  2. 112 7
      qywechat.class.php

+ 1 - 0
qyerrCode.php

@@ -164,6 +164,7 @@ class ErrCode
 	        '60112'=>'成员姓名不合法',
 	        '60113'=>'身份认证信息(微信号/手机/邮箱)不能同时为空',
 	        '60114'=>'性别不合法',
+	        '60023'=>'应用已授权予第三方,不允许通过分级管理员修改菜单',
 
 	);
 

+ 112 - 7
qywechat.class.php

@@ -71,6 +71,7 @@ class Wechat
     const MENU_GET_URL = '/menu/get?';
     const MENU_DELETE_URL = '/menu/delete?';
     const TOKEN_GET_URL = '/gettoken?';
+    const TICKET_GET_URL = '/get_jsapi_ticket?';
 	const CALLBACKSERVER_GET_URL = '/getcallbackip?';
 	const OAUTH_PREFIX = 'https://open.weixin.qq.com/connect/oauth2';
 	const OAUTH_AUTHORIZE_URL = '/authorize?';
@@ -825,7 +826,7 @@ class Wechat
 </xml>";
 	    return sprintf($format, $encrypt, $signature, $timestamp, $nonce);
 	}
-	
+
 	/**
 	 * 设置缓存,按需重载
 	 * @param string $cachename
@@ -837,7 +838,7 @@ class Wechat
 		//TODO: set cache implementation
 		return false;
 	}
-	
+
 	/**
 	 * 获取缓存,按需重载
 	 * @param string $cachename
@@ -847,7 +848,7 @@ class Wechat
 		//TODO: get cache implementation
 		return false;
 	}
-	
+
 	/**
 	 * 清除缓存,按需重载
 	 * @param string $cachename
@@ -873,13 +874,13 @@ class Wechat
 		    $this->access_token=$token;
 		    return $this->access_token;
 		}
-		
-		$authname = 'wechat_access_token'.$appid;
+
+		$authname = 'qywechat_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)
 		{
@@ -904,12 +905,116 @@ class Wechat
 	public function resetAuth($appid=''){
 		if (!$appid) $appid = $this->appid;
 		$this->access_token = '';
-		$authname = 'wechat_access_token'.$appid;
+		$authname = 'qywechat_access_token'.$appid;
+		$this->removeCache($authname);
+		return true;
+	}
+
+	/**
+	 * 删除JSAPI授权TICKET
+	 * @param string $appid 用于多个appid时使用
+	 */
+	public function resetJsTicket($appid=''){
+		if (!$appid) $appid = $this->appid;
+		$this->jsapi_ticket = '';
+		$authname = 'qywechat_jsapi_ticket'.$appid;
 		$this->removeCache($authname);
 		return true;
 	}
 
 	/**
+	 * 获取JSAPI授权TICKET
+	 * @param string $appid 用于多个appid时使用,可空
+	 * @param string $jsapi_ticket 手动指定jsapi_ticket,非必要情况不建议用
+	 */
+	public function getJsTicket($appid='',$jsapi_ticket=''){
+		if (!$this->access_token && !$this->checkAuth()) return false;
+		if ($jsapi_ticket) { //手动指定token,优先使用
+		    $this->jsapi_ticket = $jsapi_ticket;
+		    return $this->access_token;
+		}
+		$authname = 'qywechat_jsapi_ticket'.$appid;
+		if ($rs = $this->getCache($authname))  {
+			$this->jsapi_ticket = $rs;
+			return $rs;
+		}
+		$result = $this->http_get(self::API_URL_PREFIX.self::TICKET_GET_URL.'access_token='.$this->access_token);
+		if ($result)
+		{
+			$json = json_decode($result,true);
+			if (!$json || !empty($json['errcode'])) {
+				$this->errCode = $json['errcode'];
+				$this->errMsg = $json['errmsg'];
+				return false;
+			}
+			$this->jsapi_ticket = $json['ticket'];
+			$expire = $json['expires_in'] ? intval($json['expires_in'])-100 : 3600;
+			$this->setCache($authname, $this->jsapi_ticket, $expire);
+			return $this->jsapi_ticket;
+		}
+		return false;
+	}
+
+
+	/**
+	 * 获取JsApi使用签名
+	 * @param string $url 网页的URL,不包含#及其后面部分
+	 * @param string $timeStamp 当前时间戳(需与JS输出的一致)
+	 * @param string $nonceStr 随机串(需与JS输出的一致)
+	 * @param string $appid 用于多个appid时使用,可空
+	 * @return string 返回签名字串
+	 */
+	public function getJsSign($url, $timeStamp, $nonceStr, $appid=''){
+	    if (!$this->jsapi_ticket && !$this->getJsTicket($appid)) return false;
+	    $ret = strpos($url,'#');
+	    if ($ret)
+	        $url = substr($url,0,$ret);
+	    $url = trim($url);
+	    if (empty($url))
+	        return false;
+	    $arrdata = array("timestamp" => $timeStamp, "noncestr" => $nonceStr, "url" => $url, "jsapi_ticket" => $this->jsapi_ticket);
+	    return $this->getSignature($arrdata);
+	}
+
+	/**
+	 * 获取签名
+	 * @param array $arrdata 签名数组
+	 * @param string $method 签名方法
+	 * @return boolean|string 签名值
+	 */
+	public function getSignature($arrdata,$method="sha1") {
+		if (!function_exists($method)) return false;
+		ksort($arrdata);
+		$paramstring = "";
+		foreach($arrdata as $key => $value)
+		{
+			if(strlen($paramstring) == 0)
+				$paramstring .= $key . "=" . $value;
+			else
+				$paramstring .= "&" . $key . "=" . $value;
+		}
+		$Sign = $method($paramstring);
+		return $Sign;
+	}
+
+	/**
+	 * 生成随机字串
+	 * @param number $length 长度,默认为16,最长为32字节
+	 * @return string
+	 */
+	public function generateNonceStr($length=16){
+		// 密码字符集,可任意添加你需要的字符
+		$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+		$str = "";
+		for($i = 0; $i < $length; $i++)
+		{
+			$str .= $chars[mt_rand(0, strlen($chars) - 1)];
+		}
+		return $str;
+	}
+
+
+	/**
 	 * 创建菜单
 	 * @param array $data 菜单数组数据
 	 * example: