ASP源码.NET源码PHP源码JSP源码JAVA源码DELPHI源码PB源码VC源码VB源码Android源码
当前位置:首页 >> 网络编程 >> Android开发 >> 微信公众号-开发者-自定义菜单-CLICK事件处理

微信公众号-开发者-自定义菜单-CLICK事件处理

来源:网络整理     时间:2015-05-14     关键词:

本篇文章主要介绍了"微信公众号-开发者-自定义菜单-CLICK事件处理",主要涉及到方面的内容,对于Android开发感兴趣的同学可以参考一下:   想点击菜单,推送消息。功能很简单,坑了我一天时间。在此笔记。菜单代码:{"button":[{ "type":"click","name":"今日歌曲...

  想点击菜单,推送消息。功能很简单,坑了我一天时间。在此笔记。

菜单代码:

{
     "button":[
     {    
          "type":"click",
          "name":"今日歌曲",
          "key":"V1001_TODAY_MUSIC"      },
      {
           "name":"菜单",
           "sub_button":[
           {    
               "type":"view",
               "name":"搜索",
               "url":"http://www.soso.com/"            },
            {
               "type":"view",
               "name":"视频",
               "url":"http://v.qq.com/"            },
            {
               "type":"click",
               "name":"赞一下我们",
               "key":"V1001_GOOD"            }]
       }]
 }

菜单生成好了以后,如何处理“赞一下我们”相应事件呢?

引用方倍工作室的代码修改如下:

<?php
/*    方倍工作室
    CopyRight 2014 All Rights Reserved
*/define("TOKEN", "weixin");

$wechatObj = new wechatCallbackapiTest();
if (!isset($_GET['echostr'])) {
    $wechatObj->responseMsg();
}else{
    $wechatObj->valid();
}

class wechatCallbackapiTest
{
    //验证签名publicfunction valid()
    {
        $echoStr = $_GET["echostr"];
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);
        if($tmpStr == $signature){
            echo$echoStr;
            exit;
        }
        $siteURL='http://210.14.148.180/kdjy/';
    }

    publicfunction responseMsg()
    {
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        if (!empty($postStr)){
            $this->logger("R ".$postStr);
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $RX_TYPE = trim($postObj->MsgType);

            $result = "";
            switch ($RX_TYPE)
            {
                case "event":
                    $result = $this->receiveEvent($postObj);
                    break;
                case "text":
                    $result = $this->receiveText($postObj);
                    break;
            }
            $this->logger("T ".$result);
            echo$result;
        }else {
            echo "";
            exit;
        }
    }

    privatefunction receiveEvent($object)
    {
        switch ($object->Event)
        {
            case "subscribe":
                $content = "欢迎关注方倍工作室 ";

            //------------------- 赞一下 start ------------------------case "CLICK":   //这里是大写‘CLICK’$content = $object->EventKey; // 获取keyif($content=='V1001_GOOD'){
                    
                    $content = '谢谢支持!!!';

                }
            //------------------- 赞一下 end ------------------------break;
        }
        $result = $this->transmitText($object, $content);
        return$result;
    }

    privatefunction receiveText($object)
    {
        $keyword = trim($object->Content);
        
            $url = "http://apix.sinaapp.com/weather/?appkey=".$object->ToUserName."&city=".urlencode($keyword); 
            $output = file_get_contents($url);
            $content = json_decode($output, true);

            $result = $this->transmitNews($object, $content);
        
        return$result;
    }

    privatefunction transmitText($object, $content)
    {
        if (!isset($content) || empty($content)){
            return "";
        }
        $textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
        $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
        return$result;
    }

    privatefunction 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>
";
        
                
                
                

以上就介绍了微信公众号-开发者-自定义菜单-CLICK事件处理,包括了方面的内容,希望对Android开发有兴趣的朋友有所帮助。

本文网址链接:http://www.codes51.com/article/detail_134615.html

相关图片

相关文章