Home > php教程 > php手册 > body text

微信公众平台自定义菜单后台代码实现

WBOY
Release: 2016-06-07 11:35:58
Original
1426 people have browsed it

对于新版的微信自定义菜单的创建,前端利用ajax的方式依次给后端传输自定义菜单信息,后端首先将信息存入到数据库中,然后发布时根据数据库中存储的信息进行数据组织。然后调用API接口进行自定义菜单的发布。前端数据回填直接拉取数据库中的信息即可。
//创建自定义菜单<br>         public function createMenu(){<br>             /*<br>              * 自定义菜单创建逻辑<br>              * 1、首先判断创建的为主菜单还是子菜单。<br>              * 2、假如为主菜单,判断其是否已经创建过子菜单,假如有,则只能修改菜单名字,否则,等同于子菜单的创建;假如为子菜单,则需要选择创建类型<br>              * 3、创建类型判断:发送消息(图文消息、文字、图片、卡券)、跳转网页<br>              * 4、对于发送消息的类型,需要进行相关信息的收集<br>              */<br>             //对数据库中处理数据表进行处理的逻辑。<br>             /*<br>              * 1、根据数据库中的信息,选择最新的一天的信息作为创建自定义菜单的选择,将这部分数据作为选择的原始数据<br>              * 2、查询主菜单信息,组成数组,用到去重。<br>              * 3、查询子菜单信息,组成数组<br>              * 4、组织数据<br>              */<br> <br>             header("Content-Type:text/html;charset=utf-8");<br>             //实例化menu表<br>             $menu = M('weixin_menu');<br>             $create_time = $menu->order('create_time desc')->limit(1)->getField('create_time');<br>             //查询主菜单信息,组成数组,用到去重,排序<br>             $zhu_name = $menu->distinct(true)->where("create_time='$create_time'")->field('zhu_name')->select();<br>             //dump($zhu_name);<br>             //根据查询到的对应的主菜单的名称,去数据库中找到数据表对应的pid;假如pid为0,则直接取其uid<br>             //主菜单数据名称组织<br> <br>             $menu_data = array();<br>             foreach($zhu_name as $key=>$value){<br>                 $tip = $value['zhu_name'];<br>                 //数组查询条件<br>                 $data['create_time'] = $create_time;<br>                 $data['zhu_name'] = $tip;<br>                 $pid = $menu->where($data)->getField('pid');<br>                 if($pid==0){<br>                     $pid = $menu->where($data)->getField('uid');<br>                 }<br>                 $menu_data[$pid] = array();<br>                 //此处直接进行数据的组织 根据pid找到对应的子菜单<br>                 $zi_data['pid'] = $pid;<br>                 $zi_data['create_time'] = $create_time;<br>                 $zi_menu_data = $menu->where($zi_data)->order('uid')->select();    //假如此数据为空,则证明没有子菜单;否则组织子菜单的信息<br>                 if(!empty($zi_menu_data)){          //组织子菜单数据<br>                     $menu_data[$pid]['name'] = $tip;<br>                     foreach($zi_menu_data as $key=>$value){<br>                         //菜单的相应类型判断<br>                         switch ($value['type']){<br>                             case 'view_limited':<br>                                 $type = 'media_id';<br>                                 $type_data = $value['type_media_id'];<br>                                 break;<br>                             case 'media_id':<br>                                 $type = 'media_id';<br>                                 $type_data = $value['type_media_id'];<br>                                 break;<br>                             case 'click':<br>                                 $type = 'key';<br>                                 $type_data = $value['type_key'];<br>                                 break;<br>                             case 'view':<br>                                 $type = 'url';<br>                                 $type_data = $value['type_url'];<br>                                 break;<br>                         }<br>                         $menu_data[$pid]['sub_button'][$key] = array(<br>                             'type'=>$value['type'],<br>                             'name'=>$value['zi_name'],<br>                             $type=>$type_data<br>                         );<br>                     }<br>                 }else{                              //组织主菜单数据<br>                     $zhu_data['uid'] = $pid;<br>                     $zhu_data['pid'] = 0;<br>                     $zhu_data['create_time'] = $create_time;<br>                     $zhu_menu_data = $menu->where($zhu_data)->find();<br>                     $menu_data[$pid]['name'] = $tip;<br>                     //菜单的相应类型判断<br>                     switch ($zhu_menu_data['type']){<br>                         case 'view_limited':<br>                             $z_type = 'media_id';<br>                             $z_type_data = $zhu_menu_data['type_media_id'];<br>                             break;<br>                         case 'media_id':<br>                             $z_type = 'media_id';<br>                             $z_type_data = $zhu_menu_data['type_media_id'];<br>                             break;<br>                         case 'click':<br>                             $z_type = 'key';<br>                             $z_type_data = $zhu_menu_data['type_key'];<br>                             break;<br>                         case 'view':<br>                             $z_type = 'url';<br>                             $z_type_data = $zhu_menu_data['type_url'];<br>                             break;<br>                     }<br>                     $menu_data[$pid]['type'] = $zhu_menu_data['type'];<br>                     $menu_data[$pid][$z_type] = $z_type_data;<br> <br>                 }<br> <br>             }<br>             ksort($menu_data);<br>             sort($menu_data);<br> <br>             $zong_menu_data = array(<br>                 'button'=>$menu_data<br>             );<br>             //dump($zong_menu_data);<br> <br>             $type = 'create';<br>             $m_data = json_encode($zong_menu_data,JSON_UNESCAPED_UNICODE);<br>             $access_token = $this->getToken();<br>             $res = $this->MenuAPI($type,$access_token,$m_data);<br>             if($res){<br>                 echo 'success';<br>             }<br> <br>         }

附件 自定义菜单.zip ( 1.4 MB 下载:161 次 )

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!