1. 程式人生 > >微信公眾號 基於PHP 拼團活動開發一 模板搭建

微信公眾號 基於PHP 拼團活動開發一 模板搭建

已測試號為例;使用者發起拼團活動(為團長),回覆給他帶有活動id與其openid的二維碼圖片,轉發給他人,已關注的掃碼之後儲存其openid,未關注的在關注之後儲存openid(防止重複參與)(團員),然後自動回覆團員自己活動資訊的二維碼。

1.微信公眾號後臺配置好 伺服器迴應地址

2.伺服器迴應:資料暫時放在redis中,

<?php 
include_once('/op********he/memcache.php');
include('tuan/token.php');		//獲取token
include('tuan/get_prcode.php');	//獲得二維碼(帶有活動id和發起人openid)
include('tuan/put_tongzhi.php');//有人幫助傳送模板訊息(發起人與幫助者)
include('tuan/tuan_redis.php');	//儲存成團資訊到redis
define("TOKEN", "dianjixia");
$wechatObj = new wechatCallbackapi();
if($_GET["echostr"])//驗證簽名
{
	$wechatObj->valid();
}else//其他事件
{
	$wechatObj->responseMsg();
}

class wechatCallbackapi
{
   public function valid()
    {
        $echoStr = $_GET["echostr"];
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }
    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];    
                
        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr,SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );
        
        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }

    public function responseMsg()
    {
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
		if (!empty($postStr))
		{
			$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
			$RX_TYPE = trim($postObj->MsgType);
			
			switch($RX_TYPE)
			{
				case "text":
					$resultStr = $this->handleText($postObj);
					break;
				case "voice":
					$resultStr = $this->receiveVoice($postObj);
					break;
				case "image":
					 $resultStr = $this->receiveImg($postObj);
					break;
				case "event":
					$resultStr = $this->handleEvent($postObj);
					break;
				default:
					$resultStr = "Unknow msg type: ".$RX_TYPE;
					break;
			}
			echo $resultStr;
		}else 
		{
			echo "error";
			exit;
		}
    }
	//處理使用者文字提問
    public function handleText($postObj)
    {
        $fromUsername	= $postObj->FromUserName;
        $toUsername	= $postObj->ToUserName;
	$content	= trim($postObj->Content);
        if($content == '拼團'){										//測試拼團觸發事件(暫時為文字)
			$act_id		= '906';
			$mediaID	= get_prcode($postObj->FromUserName,$act_id);
			if($mediaID){
				$resultStr	= $this->transmitPic($postObj,$mediaID);
				return $resultStr;
			}else{
				$resultStr = $this->transmitText($postObj,'未獲取到mediaID'.$postObj->FromUserName);
				return $resultStr;
			}
	}

    public function handleEvent($object)
    {
        $contentStr = "";
	$qrcode	    = json_decode(json_encode($object),true);
	switch ($object->Event)
		{	
			case "SCAN":
				if(count($qrcode['EventKey']) == 1 ){
					$my_openid	= $qrcode['FromUserName']; 
					$code_arr	= explode(",",$qrcode['EventKey']);
					$p_openid	= $code_arr[0];
					$act_id		= $code_arr[1];
					//$resultStr      = $this->transmitText($object,$p_openid.','.$act_id.','.$my_openid);
					//return $resultStr;
					$state		= joinTuan($p_openid,$act_id,$my_openid);
					if($state == 2){
						$resultStr = $this->transmitText($object, '此團已滿');
					}elseif($state == 3){
						$resultStr = $this->transmitText($object, '您已進過此團');
					}elseif($state == 4){
						//header("Location: http://www.qubaobei.com/weekend/view/mall_mm/pages/rule.php?$my_openid");
						$resultStr = $this->transmitText($object, '暫時先給提示語;自己的團a');
					}else{
						put_tongzhi($p_openid,$act_id);  	 //傳送通知給發起人
						put_my_tongzhi($my_openid);	 //傳送通知給掃碼人
						$mediaID	= get_prcode($object->FromUserName,$act_id);
						$resultStr 	= $this->transmitPic($object,$mediaID);
						//$resultStr      = $this->transmitText($object,$state.','.$mediaID);
					}
					return $resultStr;
				}
			case "subscribe":
				if(count($qrcode['EventKey']) == 1){
					$my_openid	= $qrcode['FromUserName']; 
					$code_arr	= explode(",",$qrcode['EventKey']);
					$p_openid	= str_replace('qrscene_','',$code_arr[0]);
					$act_id		= $code_arr[1];
					//$resultStr      = $this->transmitText($object,$p_openid.','.$act_id.','.$my_openid);
					//return $resultStr;
					$state		= joinTuan($p_openid,$act_id,$my_openid);
					if($state == 2){
						$resultStr = $this->transmitText($object, '此團已滿');
					}elseif($state == 3){
						$resultStr = $this->transmitText($object, '您已進過此團');
					}elseif($state == 4){
						//header("Location: http://www.qubaobei.com/weekend/view/mall_mm/pages/rule.php?$my_openid");
						$resultStr = $this->transmitText($object, '暫時先給提示語;自己的團');
					}else{
						put_tongzhi($p_openid);  	 //傳送通知給發起人
						put_my_tongzhi($my_openid);	 //傳送通知給掃碼人
						$mediaID	= get_prcode($object->FromUserName,$act_id);
						$resultStr 	= $this->transmitPic($object,$mediaID);
						//$resultStr      = $this->transmitText($object,$p_openid);
					}
					return $resultStr;
				}	
			case "unsubscribe"://取消關注
				break;
			case "CLICK":
				break;		
			default :
				$contentStr = "Unknow Event: ".$object->Event;
				break;
        }
    }
    
    //回覆文字訊息
    private function transmitText($object, $content)
    {
        $textTpl = "<xml>
					<ToUserName><![CDATA[%s]]></ToUserName>
					<FromUserName><![CDATA[%s]]></FromUserName>
					<CreateTime>%s</CreateTime>
					<MsgType><![CDATA[text]]></MsgType>
					<Content><![CDATA[%s]]></Content>
					<FuncFlag>0</FuncFlag>
					</xml>";
        $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
        //$result = sprintf($textTpl,'oBxp20nUq4tcxQONMKIupYRxiv04', $object->ToUserName, time(), $content);
        return $result;
    }	
	//回覆圖文訊息
	 private function transmitNews($object, $newsArray)
    {
        if(!is_array($newsArray)){
            return;
        }
        $itemTpl = "<item>
						<Title><![CDATA[%s]]></Title>
						<Description><![CDATA[%s]]></Description>
						<PicUrl><![CDATA[%s]]></PicUrl>
						<Url><![CDATA[%s]]></Url>
					</item>
				";
        $item_str = "";
        foreach ($newsArray as $item){
            $item_str .= sprintf($itemTpl, $item['title'], $item['description'], $item['picurl'], $item['url']);
        }
        $newsTpl = "<xml>
					<ToUserName><![CDATA[%s]]></ToUserName>
					<FromUserName><![CDATA[%s]]></FromUserName>
					<CreateTime>%s</CreateTime>
					<MsgType><![CDATA[news]]></MsgType>
					<Content><![CDATA[]]></Content>
					<ArticleCount>%s</ArticleCount>
					<Articles>
					$item_str</Articles>
					</xml>";

        $result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray));
        return $result;
    }
	//回覆圖片訊息
    	private function transmitPic($object,$media)
    	{	
		/*
        	$textTpl = "<xml>
				<ToUserName>< ![CDATA[%s]]></ToUserName>
				<FromUserName>< ![CDATA[%s]]></FromUserName>
				<CreateTime>%s</CreateTime>
				<MsgType>< ![CDATA[image]]></MsgType>
				<Image>
					<MediaId>< ![CDATA[%s]]></MediaId>
				</Image>
			    </xml>";
       	 	*/
		$textTpl = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[image]]></MsgType><Image><MediaId><![CDATA[%s]]></MediaId></Image></xml>";
		$result = sprintf($textTpl,$object->FromUserName, $object->ToUserName, time(),$media);
        	return $result;
    	}
	//處理圖片
	private function receiveImg($object)
    {
	
	}
}

function curl_baby($url,$data=null)
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
	//curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$tmpInfo = curl_exec($ch);
	if (curl_errno($ch)) {
	  return curl_error($ch);
	}
	curl_close($ch);
	return $tmpInfo;
}
?>