Home php教程 php手册 微信公众平台自定义菜单后台代码实现

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

Jun 07, 2016 am 11:35 AM

对于新版的微信自定义菜单的创建,前端利用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元

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Learn about introductory code examples for Python programming Learn about introductory code examples for Python programming Jan 04, 2024 am 10:50 AM

Learn about Python programming with introductory code examples Python is an easy-to-learn, yet powerful programming language. For beginners, it is very important to understand the introductory code examples of Python programming. This article will provide you with some concrete code examples to help you get started quickly. Print HelloWorldprint("HelloWorld") This is the simplest code example in Python. The print() function is used to output the specified content

Go language programming examples: code examples in web development Go language programming examples: code examples in web development Mar 04, 2024 pm 04:54 PM

"Go Language Programming Examples: Code Examples in Web Development" With the rapid development of the Internet, Web development has become an indispensable part of various industries. As a programming language with powerful functions and superior performance, Go language is increasingly favored by developers in web development. This article will introduce how to use Go language for Web development through specific code examples, so that readers can better understand and use Go language to build their own Web applications. 1. Simple HTTP Server First, let’s start with a

PHP variables in action: 10 real-life examples of use PHP variables in action: 10 real-life examples of use Feb 19, 2024 pm 03:00 PM

PHP variables store values ​​during program runtime and are crucial for building dynamic and interactive WEB applications. This article takes an in-depth look at PHP variables and shows them in action with 10 real-life examples. 1. Store user input $username=$_POST["username"];$passWord=$_POST["password"]; This example extracts the username and password from the form submission and stores them in variables for further processing. 2. Set the configuration value $database_host="localhost";$database_username="username";$database_pa

How to use PHP to write inventory management function code in the inventory management system How to use PHP to write inventory management function code in the inventory management system Aug 06, 2023 pm 04:49 PM

How to use PHP to write the inventory management function code in the inventory management system. Inventory management is an indispensable part of many enterprises. For companies with multiple warehouses, the inventory management function is particularly important. By properly managing and tracking inventory, companies can allocate inventory between different warehouses, optimize operating costs, and improve collaboration efficiency. This article will introduce how to use PHP to write code for inventory warehouse management functions, and provide you with relevant code examples. 1. Establish the database before starting to write the code for the inventory warehouse management function.

Java implements simple bubble sort code Java implements simple bubble sort code Jan 30, 2024 am 09:34 AM

The simplest code example of Java bubble sort Bubble sort is a common sorting algorithm. Its basic idea is to gradually adjust the sequence to be sorted into an ordered sequence through the comparison and exchange of adjacent elements. Here is a simple Java code example that demonstrates how to implement bubble sort: publicclassBubbleSort{publicstaticvoidbubbleSort(int[]arr){int

Huawei Cloud Edge Computing Interconnection Guide: Java code examples to quickly implement interfaces Huawei Cloud Edge Computing Interconnection Guide: Java code examples to quickly implement interfaces Jul 05, 2023 pm 09:57 PM

Huawei Cloud Edge Computing Interconnection Guide: Java Code Samples to Quickly Implement Interfaces With the rapid development of IoT technology and the rise of edge computing, more and more enterprises are beginning to pay attention to the application of edge computing. Huawei Cloud provides edge computing services, providing enterprises with highly reliable computing resources and a convenient development environment, making edge computing applications easier to implement. This article will introduce how to quickly implement the Huawei Cloud edge computing interface through Java code. First, we need to prepare the development environment. Make sure you have the Java Development Kit installed (

From beginner to proficient: Code implementation of commonly used data structures in Go language From beginner to proficient: Code implementation of commonly used data structures in Go language Mar 04, 2024 pm 03:09 PM

Title: From Beginner to Mastery: Code Implementation of Commonly Used Data Structures in Go Language Data structures play a vital role in programming and are the basis of programming. In the Go language, there are many commonly used data structures, and mastering the implementation of these data structures is crucial to becoming a good programmer. This article will introduce the commonly used data structures in the Go language and give corresponding code examples to help readers from getting started to becoming proficient in these data structures. 1. Array Array is a basic data structure, a group of the same type

Guidance and Examples: Learn to implement the selection sort algorithm in Java Guidance and Examples: Learn to implement the selection sort algorithm in Java Feb 18, 2024 am 10:52 AM

Java Selection Sorting Method Code Writing Guide and Examples Selection sorting is a simple and intuitive sorting algorithm. The idea is to select the smallest (or largest) element from the unsorted elements each time and exchange it until all elements are sorted. This article will provide a code writing guide for selection sorting, and attach specific Java sample code. Algorithm Principle The basic principle of selection sort is to divide the array to be sorted into two parts, sorted and unsorted. Each time, the smallest (or largest) element is selected from the unsorted part and placed at the end of the sorted part. Repeat the above

See all articles