首页 » 微信公众平台开发:从零基础到ThinkPHP5高性能框架实践 » 微信公众平台开发:从零基础到ThinkPHP5高性能框架实践全文在线阅读

《微信公众平台开发:从零基础到ThinkPHP5高性能框架实践》5.2.7 菜单事件推送

关灯直达底部

用户点击菜单之后,微信会将事件推送给接口程序,相应的参数及说明如表5-5所示。

表5-5 自定义菜单事件字段的参数说明

用户点击自定义菜单后,接口程序收到的XML数据包如下。


<xml>    <ToUserName><![CDATA[gh_36dd0f0b3132]]></ToUserName>    <FromUserName><![CDATA[oQW8FuN_DFnKZwBaUKQ1RJD2Tr9M]]></FromUserName>    <CreateTime>1468050882</CreateTime>    <MsgType><![CDATA[event]]></MsgType>    <Event><![CDATA[CLICK]]></Event>    <EventKey><![CDATA[TEXT]]></EventKey></xml>  

点击按钮类型为view的菜单后,上报的XML数据包如下。


<xml>    <ToUserName><![CDATA[gh_36dd0f0b3132]]></ToUserName>    <FromUserName><![CDATA[oQW8FuN_DFnKZwBaUKQ1RJD2Tr9M]]></FromUserName>    <CreateTime>1468050934</CreateTime>    <MsgType><![CDATA[event]]></MsgType>    <Event><![CDATA[VIEW]]></Event>    <EventKey><![CDATA[http:// xw.qq.com/]]></EventKey>    <MenuId>410418124</MenuId></xml>  

点击scancode_push类型的菜单时,接口程序收到的XML数据包如下。微信会直接运行解码后的内容,比如直接进入关注界面。


<xml>    <ToUserName><![CDATA[gh_36dd0f0b3132]]></ToUserName>    <FromUserName><![CDATA[oQW8FuN_DFnKZwBaUKQ1RJD2Tr9M]]></FromUserName>    <CreateTime>1468051082</CreateTime>    <MsgType><![CDATA[event]]></MsgType>    <Event><![CDATA[scancode_push]]></Event>    <EventKey><![CDATA[rselfmenu_2_2]]></EventKey>    <ScanCodeInfo>        <ScanType><![CDATA[qrcode]]></ScanType>        <ScanResult><![CDATA[http:// weixin.qq.com/r/l0Ozq9-EbYISrZvI9xaF]]></ScanResult>    </ScanCodeInfo></xml>  

点击scancode_waitmsg类型的菜单时,接口程序收到的XML数据包如下。


<xml>    <ToUserName><![CDATA[gh_36dd0f0b3132]]></ToUserName>    <FromUserName><![CDATA[oQW8FuN_DFnKZwBaUKQ1RJD2Tr9M]]></FromUserName>    <CreateTime>1468051112</CreateTime>    <MsgType><![CDATA[event]]></MsgType>    <Event><![CDATA[scancode_waitmsg]]></Event>    <EventKey><![CDATA[rselfmenu_2_1]]></EventKey>    <ScanCodeInfo>        <ScanType><![CDATA[qrcode]]></ScanType>        <ScanResult><![CDATA[http:// weixin.qq.com/r/l0Ozq9-EbYISrZvI9xaF]]></ScanResult>    </ScanCodeInfo></xml>  

点击pic_sysphoto类型的菜单后,微信调用手机中的系统相机,照相后再发过来时,就收到了一个图片消息。点击该菜单时的XML数据包如下。


<xml>    <ToUserName><![CDATA[gh_36dd0f0b3132]]></ToUserName>    <FromUserName><![CDATA[oQW8FuNH_V1zk4x3zTN1IMoAZDW0]]></FromUserName>    <CreateTime>1468051307</CreateTime>    <MsgType><![CDATA[event]]></MsgType>    <Event><![CDATA[pic_sysphoto]]></Event>    <EventKey><![CDATA[rselfmenu_2_3]]></EventKey>    <SendPicsInfo>        <Count>1</Count>        <PicList>            <item>                <PicMd5Sum><![CDATA[4cc7f5ab2c499b01655e99a868ef3519]]></PicMd5Sum>            </item>        </PicList>    </SendPicsInfo></xml>  

点击pic_photo_or_album类型的菜单后,先推送菜单事件给开发者,然后推送图片消息。点击该菜单时的XML数据包如下。


<xml>    <ToUserName><![CDATA[gh_36dd0f0b3132]]></ToUserName>    <FromUserName><![CDATA[oQW8FuNH_V1zk4x3zTN1IMoAZDW0]]></FromUserName>    <CreateTime>1468051528</CreateTime>    <MsgType><![CDATA[event]]></MsgType>    <Event><![CDATA[pic_photo_or_album]]></Event>    <EventKey><![CDATA[rselfmenu_2_4]]></EventKey>    <SendPicsInfo>        <Count>2</Count>        <PicList>            <item>                <PicMd5Sum><![CDATA[b5c23feac0987db7ddda1b0a3addba9d]]></PicMd5Sum>            </item>            <item>                <PicMd5Sum><![CDATA[59c3eb7e2124a75986295b8842573951]]></PicMd5Sum>            </item>        </PicList>    </SendPicsInfo></xml>  

点击pic_weixin类型的菜单后,微信客户端将调用系统相机,用户可以选择已有相片或者进行拍照,微信会将照片发送给开发者。下面是一次选择3张照片时的XML数据包。


<xml>    <ToUserName><![CDATA[gh_36dd0f0b3132]]></ToUserName>    <FromUserName><![CDATA[oQW8FuNH_V1zk4x3zTN1IMoAZDW0]]></FromUserName>    <CreateTime>1468051592</CreateTime>    <MsgType><![CDATA[event]]></MsgType>    <Event><![CDATA[pic_weixin]]></Event>    <EventKey><![CDATA[rselfmenu_2_5]]></EventKey>    <SendPicsInfo>        <Count>3</Count>        <PicList>            <item>                <PicMd5Sum><![CDATA[59c3eb7e2124a75986295b8842573951]]></PicMd5Sum>            </item>            <item>                <PicMd5Sum><![CDATA[b5c23feac0987db7ddda1b0a3addba9d]]></PicMd5Sum>            </item>            <item>                <PicMd5Sum><![CDATA[4cc7f5ab2c499b01655e99a868ef3519]]></PicMd5Sum>            </item>        </PicList>    </SendPicsInfo></xml>  

点击location_select类型的菜单后,将会调用发送位置功能,在用户发送位置后,会再推送一个地理位置消息给用户。其XML数据包如下。


<xml>    <ToUserName><![CDATA[gh_36dd0f0b3132]]></ToUserName>    <FromUserName><![CDATA[oQW8FuNH_V1zk4x3zTN1IMoAZDW0]]></FromUserName>    <CreateTime>1468051658</CreateTime>    <MsgType><![CDATA[event]]></MsgType>    <Event><![CDATA[location_select]]></Event>    <EventKey><![CDATA[SIGNIN]]></EventKey>    <SendLocationInfo>        <Location_X><![CDATA[22.53996467590332]]></Location_X>        <Location_Y><![CDATA[113.93487548828125]]></Location_Y>        <Scale><![CDATA[17]]></Scale>        <Label><![CDATA[广东省深圳市南山区深南大道10000号]]></Label>        <Poiname><![CDATA[腾讯大厦]]></Poiname>    </SendLocationInfo></xml>  

消息接口中,响应自定义菜单点击事件的核心代码如下。


// 接收事件消息private function receiveEvent($object){    $content = "";    switch ($object->Event)    {        case "subscribe":            $content = "欢迎关注方倍工作室 ";            $content .= (!empty($object->EventKey))?("/n来自二维码场景 ".str_replace            ("qrscene_","",$object->EventKey)):"";            break;        case "unsubscribe":            $content = "取消关注";            break;        case "CLICK":            switch ($object->EventKey)            {                case "COMPANY":                    $content = array;                    $content = array("Title"=>"方倍工作室", "Description"=>"", "Pic                    Url"=>"http:// discuz.comli.com/weixin/weather/icon/cartoon.jpg",                     "Url" =>"http:// m.cnblogs.com/?u=txw1958");                    break;                default:                    $content = "点击菜单:".$object->EventKey;                    break;            }            break;        case "VIEW":            $content = "跳转链接 ".$object->EventKey;            break;        case "SCAN":            $content = "扫描场景 ".$object->EventKey;            break;        case "LOCATION":            $content = "上传位置:纬度 ".$object->Latitude.";经度 ".$object->Longitude;            break;        case "scancode_waitmsg":            $content = "扫码带提示:类型 ".$object->ScanCodeInfo->ScanType." 结果:".            $object->ScanCodeInfo->ScanResult;            break;        case "scancode_push":            $content = "扫码推事件";            break;        case "pic_sysphoto":            $content = "系统拍照";            break;        case "pic_weixin":            $content = "相册发图:数量 ".$object->SendPicsInfo->Count;            break;        case "pic_photo_or_album":            $content = "拍照或者相册:数量 ".$object->SendPicsInfo->Count;            break;        case "location_select":            $content = "发送位置:标签 ".$object->SendLocationInfo->Label;            break;        default:            $content = "receive a new event: ".$object->Event." /n技术支持 方倍工作室";            break;    }    if(is_array($content)){        if (isset($content[0]['PicUrl'])){            $result = $this->transmitNews($object, $content);        }else if (isset($content['MusicUrl'])){            $result = $this->transmitMusic($object, $content);        }    }else{        $result = $this->transmitText($object, $content);    }    return $result;}