Browse Source

Merge remote-tracking branch 'online/master' into dev

Conflicts:
	README.md
	Thinkphp/Wechatext.class.php
	wechat.class.php
	wechat.js
binsee 10 years ago
parent
commit
7142596155
8 changed files with 127 additions and 35 deletions
  1. 1 0
      README.md
  2. 1 1
      Thinkphp/Snoopy.class.php
  3. 77 6
      Thinkphp/Wechat.class.php
  4. 10 9
      Thinkphp/Wechatext.class.php
  5. 1 1
      snoopy.class.php
  6. 12 9
      wechat.class.php
  7. 15 0
      wechat.js
  8. 10 9
      wechatext.class.php

+ 1 - 0
README.md

@@ -141,6 +141,7 @@ https://mp.weixin.qq.com/cgi-bin/readtemplate?t=business/course2_tmpl&lang=zh_CN
  * WeixinJS.sendEmail(title,content) 发送邮件
  * WeixinJS.openProductView(latitude,longitude,name,address,scale,infoUrl) 查看地图
  * WeixinJS.addContact(username) 添加微信账号
+ * WeixinJS.imagePreview(urls,current) 调出微信内图片预览
  * WeixinJS.payCallback(appId,package,timeStamp,nonceStr,signType,paySign,callback) 微信JsApi支付接口
  * WeixinJS.editAddress(appId,addrSign,timeStamp,nonceStr,callback) 微信JsApi支付接口
  * 通过定义全局变量dataForWeixin配置触发分享的内容:

+ 1 - 1
Thinkphp/Snoopy.class.php

@@ -1220,7 +1220,7 @@ class Snoopy
 		if (count($formvars) == 0 && count($formfiles) == 0)
 			return;
 		if (is_string($formvars)) return $formvars;
-		if(count($formvars) == 1) return $formvars[0];
+		if((count($formvars) == 1) && isset($formvars[0])) return $formvars[0];
 		switch ($this->_submit_type) {
 			case "application/x-www-form-urlencoded":
 				reset($formvars);

+ 77 - 6
Thinkphp/Wechat.class.php

@@ -64,17 +64,19 @@ class Wechat
 	const QR_SCENE = 0;
 	const QR_LIMIT_SCENE = 1;
 	const QRCODE_IMG_URL='https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=';
+	const SHORT_URL='/shorturl?';
 	const USER_GET_URL='/user/get?';
 	const USER_INFO_URL='/user/info?';
 	const USER_UPDATEREMARK_URL='/user/info/updateremark?';	
 	const GROUP_GET_URL='/groups/get?';
+	const USER_GROUP_URL='/groups/getid?';
 	const GROUP_CREATE_URL='/groups/create?';
 	const GROUP_UPDATE_URL='/groups/update?';
 	const GROUP_MEMBER_UPDATE_URL='/groups/members/update?';
 	const CUSTOM_SEND_URL='/message/custom/send?';
 	const MEDIA_UPLOADNEWS_URL = '/media/uploadnews?';
 	const MASS_SEND_URL = '/message/mass/send?';
-	const Templat_SEND_URL = '/message/template/send?';
+	const TEMPLATE_SEND_URL = '/message/template/send?';
 	const MASS_SEND_GROUP_URL = '/message/mass/sendall?';
 	const MASS_DELETE_URL = '/message/mass/delete?';
 	const UPLOAD_MEDIA_URL = 'http://file.api.weixin.qq.com/cgi-bin';
@@ -85,6 +87,7 @@ class Wechat
 	const OAUTH_TOKEN_URL = '/access_token?';
 	const OAUTH_REFRESH_URL = '/refresh_token?';
 	const OAUTH_USERINFO_URL = 'https://api.weixin.qq.com/sns/userinfo?';
+	const OAUTH_AUTH_URL = 'https://api.weixin.qq.com/sns/auth?';
 	const PAY_DELIVERNOTIFY = 'https://api.weixin.qq.com/pay/delivernotify?';
 	const PAY_ORDERQUERY = 'https://api.weixin.qq.com/pay/orderquery?';
 	const CUSTOM_SERVICE_GET_RECORD = '/customservice/getrecord?';
@@ -355,12 +358,11 @@ class Wechat
 		if (isset($this->_receive['EventKey'])){
 			$array['key'] = $this->_receive['EventKey'];
 		}
-		
 		if (isset($array) && count($array) > 0) {
 			return $array;
 		} else {
 			return false;
-		} 
+		}
 	}
 	
 	/**
@@ -946,7 +948,7 @@ class Wechat
 	 * @param int $scene_id 自定义追踪id
 	 * @param int $type 0:临时二维码;1:永久二维码(此时expire参数无效)
 	 * @param int $expire 临时二维码有效期,最大为1800秒
-	 * @return array('ticket'=>'qrcode字串','expire_seconds'=>1800)
+	 * @return array('ticket'=>'qrcode字串','expire_seconds'=>1800,'url'=>'二维码图片解析后的地址')
 	 */
 	public function getQRCode($scene_id,$type=0,$expire=1800){
 		if (!$this->access_token && !$this->checkAuth()) return false;
@@ -982,6 +984,31 @@ class Wechat
 	}
 	
 	/**
+	 * 长链接转短链接接口
+	 * @param string $long_url 传入要转换的长url
+	 * @return boolean|string url 成功则返回转换后的短url
+	 */
+	public function getShortUrl($long_url){
+	    if (!$this->access_token && !$this->checkAuth()) return false;
+	    $data = array(
+            'action'=>'long2short',
+            'long_url'=>$long_url
+	    );
+	    $result = $this->http_post(self::API_URL_PREFIX.self::SHORT_URL.'access_token='.$this->access_token,self::json_encode($data));
+	    if ($result)
+	    {
+	        $json = json_decode($result,true);
+	        if (!$json || !empty($json['errcode'])) {
+	            $this->errCode = $json['errcode'];
+	            $this->errMsg = $json['errmsg'];
+	            return false;
+	        }
+	        return $json['short_url'];
+	    }
+	    return false;
+	}
+
+	/**
 	 * 批量获取关注用户列表
 	 * @param unknown $next_openid
 	 */
@@ -1069,6 +1096,30 @@ class Wechat
 	}
 	
 	/**
+	 * 获取用户所在分组
+	 * @param string $openid
+	 * @return boolean|int 成功则返回用户分组id
+	 */
+	public function getUserGroup($openid){
+	    if (!$this->access_token && !$this->checkAuth()) return false;
+	    $data = array(
+	            'openid'=>$openid
+	    );
+	    $result = $this->http_post(self::API_URL_PREFIX.self::USER_GROUP_URL.'access_token='.$this->access_token,self::json_encode($data));
+	    if ($result)
+	    {
+	        $json = json_decode($result,true);
+	        if (!$json || !empty($json['errcode'])) {
+	            $this->errCode = $json['errcode'];
+	            $this->errMsg = $json['errmsg'];
+	            return false;
+	        } else 
+                if (isset($json['groupid'])) return $json['groupid'];
+	    }
+	    return false;
+	}
+    
+	/**
 	 * 新增自定分组
 	 * @param string $name 分组名称
 	 * @return boolean|array
@@ -1236,6 +1287,27 @@ class Wechat
 		}
 		return false;
 	}
+
+	/**
+	 * 检验授权凭证是否有效
+	 * @param string $access_token
+	 * @param string $openid
+	 * @return boolean 是否有效
+	 */
+	public function getOauthAuth($access_token,$openid){
+	    $result = $this->http_get(self::OAUTH_AUTH_URL.'access_token='.$access_token.'&openid='.$openid);
+	    if ($result)
+	    {
+	        $json = json_decode($result,true);
+	        if (!$json || !empty($json['errcode'])) {
+	            $this->errCode = $json['errcode'];
+	            $this->errMsg = $json['errmsg'];
+	            return false;
+	        } else
+	          if ($json['errcode']==0) return true;
+	    }
+	    return false;
+	}
 	
 	/**
 	 * 获取签名
@@ -1473,8 +1545,7 @@ class Wechat
 	 */
 	public function sendTemplateMessage($data){
 		if (!$this->access_token && !$this->checkAuth()) return false;
-		$result = $this->http_post(self::API_URL_PREFIX.self::Templat_SEND_URL.'access_token='.$this->access_token,self::json_encode($data));
-		
+		$result = $this->http_post(self::API_URL_PREFIX.self::TEMPLATE_SEND_URL.'access_token='.$this->access_token,self::json_encode($data));
 		if($result){
 			$json = json_decode($result,true);
 			if (!$json || !empty($json['errcode'])) {

+ 10 - 9
Thinkphp/Wechatext.class.php

@@ -629,7 +629,8 @@ class Wechatext
 			$this->_token = $matches[1];
 			$this->log('token:'.$this->_token);
 		}
-		$this->saveCookie($this->_cookiename,$cookie);
+		$cookies='{"cookie":"'.$cookie.'","token":"'.$this->_token.'"}';
+		$this->saveCookie($this->_cookiename,$cookies);
 		return $cookie;
 	}
 
@@ -651,19 +652,19 @@ class Wechatext
 	public function getCookie($filename){
 		$data = S($filename);
 		if($data){
-			$send_snoopy = new Snoopy; 
-			$send_snoopy->rawheaders['Cookie']= $data;
+			$login=json_decode($data,true);
+			$send_snoopy = new Snoopy;
+			$send_snoopy->rawheaders['Cookie']= $login['cookie'];
 			$send_snoopy->maxredirs = 0;
-			$url = "https://mp.weixin.qq.com/cgi-bin/indexpage?t=wxm-index&lang=zh_CN";
+			$url = "https://mp.weixin.qq.com/cgi-bin/home?t=home/index&lang=zh_CN&token=".$login['token'];
 			$send_snoopy->fetch($url);
-			$header = implode(',',$send_snoopy->headers);
+			$header = $send_snoopy->headers;
 			$this->log('header:'.print_r($send_snoopy->headers,true));
-			preg_match("/token=(\d+)/i",$header,$matches);
-			if(empty($matches)){
+			if( strstr($header[3], 'EXPIRED')){
 				return $this->login();
 			}else{
-				$this->_token = $matches[1];
-				return $data;
+				$this->_token =$login['token'];
+				return $login['cookie'];
 			}
 		}else{
 			return $this->login();

+ 1 - 1
snoopy.class.php

@@ -1221,7 +1221,7 @@ class Snoopy
 		if (count($formvars) == 0 && count($formfiles) == 0)
 			return;
 		if (is_string($formvars)) return $formvars;
-		if(count($formvars) == 1) return $formvars[0];
+		if((count($formvars) == 1) && isset($formvars[0])) return $formvars[0];
 		switch ($this->_submit_type) {
 			case "application/x-www-form-urlencoded":
 				reset($formvars);

+ 12 - 9
wechat.class.php

@@ -76,7 +76,7 @@ class Wechat
 	const CUSTOM_SEND_URL='/message/custom/send?';
 	const MEDIA_UPLOADNEWS_URL = '/media/uploadnews?';
 	const MASS_SEND_URL = '/message/mass/send?';
-	const Templat_SEND_URL = '/message/template/send?';
+	const TEMPLATE_SEND_URL = '/message/template/send?';
 	const MASS_SEND_GROUP_URL = '/message/mass/sendall?';
 	const MASS_DELETE_URL = '/message/mass/delete?';
 	const UPLOAD_MEDIA_URL = 'http://file.api.weixin.qq.com/cgi-bin';
@@ -348,12 +348,16 @@ class Wechat
 	 */
 	public function getRevEvent(){
 		if (isset($this->_receive['Event'])){
-			return array(
-				'event'=>$this->_receive['Event'],
-				'key'=>$this->_receive['EventKey'],
-			);
-		} else 
+			$array['event'] = $this->_receive['Event'];
+		}
+		if (isset($this->_receive['EventKey'])){
+			$array['key'] = $this->_receive['EventKey'];
+		}
+		if (isset($array) && count($array) > 0) {
+			return $array;
+		} else {
 			return false;
+		}
 	}
 	
 	/**
@@ -1290,7 +1294,7 @@ class Wechat
 	            $this->errMsg = $json['errmsg'];
 	            return false;
 	        } else
-	          if ($json['errcode'==0) return true;
+	          if ($json['errcode']==0) return true;
 	    }
 	    return false;
 	}
@@ -1531,8 +1535,7 @@ class Wechat
 	 */
 	public function sendTemplateMessage($data){
 		if (!$this->access_token && !$this->checkAuth()) return false;
-		$result = $this->http_post(self::API_URL_PREFIX.self::Templat_SEND_URL.'access_token='.$this->access_token,self::json_encode($data));
-		
+		$result = $this->http_post(self::API_URL_PREFIX.self::TEMPLATE_SEND_URL.'access_token='.$this->access_token,self::json_encode($data));
 		if($result){
 			$json = json_decode($result,true);
 			if (!$json || !empty($json['errcode'])) {

+ 15 - 0
wechat.js

@@ -18,6 +18,7 @@
  * WeixinJS.sendEmail(title,content) 发送邮件
  * WeixinJS.openProductView(latitude,longitude,name,address,scale,infoUrl) 查看地图
  * WeixinJS.addContact(username) 添加微信账号
+ * WeixinJS.imagePreview(urls,current) 调出微信内图片预览
  * WeixinJS.payCallback(appId,package,timeStamp,nonceStr,signType,paySign,callback) 微信JsApi支付接口
  * WeixinJS.editAddress(appId,addrSign,timeStamp,nonceStr,callback) 微信JsApi支付接口
  * 自定义分享内容数据格式:
@@ -178,6 +179,19 @@ WeixinJS.addContact=function weixinAddContact(username){
     });
 };
 
+/**
+ * 调出微信内图片预览scrollview
+ * @param array urls 图片url数组
+ * @param string current 当前图片url
+ */
+WeixinJS.imagePreview = function(urls,current) {
+	if (typeof WeixinJSBridge!='undefined') 
+		WeixinJSBridge.invoke("imagePreview", {
+			current: current,
+			urls: urls
+		});
+};
+
 //微信JsApi支付接口
 WeixinJS.payCallback = function(appId,package,timeStamp,nonceStr,signType,paySign,callback){
 	if (typeof WeixinJSBridge!='undefined')
@@ -214,6 +228,7 @@ WeixinJS.editAddress = function(appId,addrSign,timeStamp,nonceStr,callback){
 };
 
 (function(){
+   if (typeof dataForWeixin=="undefined") return;
    var onBridgeReady=function(){
    WeixinJSBridge.on('menu:share:appmessage', function(argv){
 	  (dataForWeixin.prepare)(argv);

+ 10 - 9
wechatext.class.php

@@ -629,7 +629,8 @@ class Wechatext
 			$this->_token = $matches[1];
 			$this->log('token:'.$this->_token);
 		}
-		$this->saveCookie($this->_cookiename,$cookie);
+		$cookies='{"cookie":"'.$cookie.'","token":"'.$this->_token.'"}';
+		$this->saveCookie($this->_cookiename,$cookies);
 		return $cookie;
 	}
 
@@ -658,20 +659,20 @@ class Wechatext
 		} else
 			$data = '';
 		if($data){
-			$send_snoopy = new Snoopy; 
-			$send_snoopy->rawheaders['Cookie']= $data;
+			$login=json_decode($data,true);
+			$send_snoopy = new Snoopy;
+			$send_snoopy->rawheaders['Cookie']= $login['cookie'];
 			$send_snoopy->maxredirs = 0;
-			$url = "https://mp.weixin.qq.com/cgi-bin/indexpage?t=wxm-index&lang=zh_CN";
+			$url = "https://mp.weixin.qq.com/cgi-bin/home?t=home/index&lang=zh_CN&token=".$login['token'];
 			$send_snoopy->fetch($url);
-			$header = implode(',',$send_snoopy->headers);
+			$header = $send_snoopy->headers;
 			$this->log('header:'.print_r($send_snoopy->headers,true));
-			preg_match("/token=(\d+)/i",$header,$matches);
-			if(empty($matches)){
+			if( strstr($header[3], 'EXPIRED')){
 				return $this->login();
 			}else{
-				$this->_token = $matches[1];
+				$this->_token =$login['token'];
 				$this->log('token:'.$this->_token);
-				return $data;
+				return $login['cookie'];
 			}
 		}else{
 			return $this->login();