


CI framework WeChat development-custom menu, ci framework custom menu_PHP tutorial
CI framework WeChat development - custom menu, ci framework custom menu
Implemented the custom menu function under the CI framework. Wrote a model and a class library. By the way Comes with access_token implementation
<?<span>php </span><span>class</span><span> Makemenu{ </span><span>public</span> <span>$menustr</span><span>; </span><span>public</span> <span>function</span><span> __construct(){ } </span><span>public</span> <span>function</span><span> init(){ </span><span>$this</span>-><span>dolist(); </span><span>return</span> <span>$this</span>-><span>setmenu(); } </span><span>private</span> <span>function</span><span> dolist(){ </span><span>$CI</span> =&<span> get_instance(); </span><span>$CI</span> -> load ->model("Menu_model","menu"<span>); </span><span>$plist</span> = <span>$CI</span>->menu -><span>isplist(); </span><span>foreach</span>(<span>$plist</span> <span>as</span> <span>$pid</span><span>){ </span><span>$pidarr</span>[] = <span>$pid</span>['pid'<span>]; } </span><span>$list</span> = <span>$CI</span>->menu ->maketree(<span>$CI</span>->menu-><span>getlist()); </span><span>foreach</span>(<span>$list</span> <span>as</span> <span>$btn</span><span>){ </span><span>if</span>(<span>in_array</span>(<span>$btn</span>['id'],<span>$pidarr</span><span>)){ </span><span>//</span><span>生成不带key和url的链接作为父级菜单</span> <span>$btn_arr</span>[<span>$btn</span>['id']] = <span>array</span>("type"=><span>$btn</span>['menutype'], "name"=><span>$btn</span>['content'<span>]); }</span><span>elseif</span>(<span>$btn</span>['pid'] == 0<span>){ </span><span>//</span><span>生成有操作的一级菜单</span> <span>$btn_arr</span>[<span>$btn</span>['id']] = <span>array</span>("type"=><span>$btn</span>['menutype'], "name"=><span>$btn</span>['content'], "key"=><span>$btn</span>['clickkey'], "url"=><span>$btn</span>['url'<span>]); }</span><span>else</span><span>{ </span><span>//</span><span>生成子菜单</span> <span>$btn_arr</span>[<span>$btn</span>['pid']]['sub_button'][] = <span>array</span>("type"=><span>$btn</span>['menutype'], "name"=><span>$btn</span>['content'], "key"=><span>$btn</span>['clickkey'], "url"=><span>$btn</span>['url'<span>]); } } </span><span>$btnarr</span>['button'] = <span>array_values</span>(<span>$btn_arr</span><span>); </span><span>$r</span> = <span>$this</span>->menustr = json_encode(<span>$btnarr</span>,<span>JSON_UNESCAPED_UNICODE); </span><span>return</span> <span>$r</span><span>; } </span><span>private</span> <span>function</span><span> setmenu(){ </span><span>$accesstoken</span> =<span> get_access_token(); </span><span>$url</span> = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={<span>$accesstoken</span>}"<span>; </span><span>$ch</span> =<span> curl_init(); curl_setopt(</span><span>$ch</span>, CURLOPT_URL, <span>$url</span><span>); curl_setopt(</span><span>$ch</span>, CURLOPT_CUSTOMREQUEST, "POST"<span>); curl_setopt(</span><span>$ch</span>, CURLOPT_SSL_VERIFYPEER, <span>FALSE</span><span>); curl_setopt(</span><span>$ch</span>, CURLOPT_SSL_VERIFYHOST, <span>FALSE</span><span>); curl_setopt(</span><span>$ch</span>, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'<span>); curl_setopt(</span><span>$ch</span>, CURLOPT_FOLLOWLOCATION, 1<span>); curl_setopt(</span><span>$ch</span>, CURLOPT_AUTOREFERER, 1<span>); curl_setopt(</span><span>$ch</span>, CURLOPT_POSTFIELDS, <span>$this</span>-><span>menustr); curl_setopt(</span><span>$ch</span>, CURLOPT_RETURNTRANSFER, <span>true</span><span>); </span><span>$info</span> = curl_exec(<span>$ch</span><span>); </span><span>if</span> (curl_errno(<span>$ch</span><span>)) { </span><span>return</span> curl_error(<span>$ch</span><span>); } curl_close(</span><span>$ch</span><span>); </span><span>return</span> <span>$info</span><span>; } }</span>
The above is the content in the library, which mainly generates the json string of the menu based on the data table
<?<span>php </span><span>class</span> Menu_model <span>extends</span><span> CI_Model { </span><span>public</span> <span>$table_name</span><span>; </span><span>public</span> <span>function</span><span> __construct(){ parent</span>::<span>__construct(); </span><span>$this</span>->load-><span>database(); </span><span>$this</span>->table_name = "data_menu"<span>; } </span><span>public</span> <span>function</span> query(<span>$sql</span><span>){ </span><span>return</span> <span>$this</span>->db->query(<span>$sql</span><span>); } </span><span>public</span> <span>function</span> getone(<span>$id</span><span>){ </span><span>$get_sql</span> = "select * from {<span>$this</span>->table_name} where id = {<span>$id</span>}"<span>; </span><span>return</span> <span>$this</span>->query(<span>$get_sql</span>)-><span>row(); } </span><span>public</span> <span>function</span> addone(<span>$data</span><span>){ </span><span>if</span>((<span>$data</span>['pid'] == 0)&&(<span>$this</span>->checksum()>=3<span>)){ </span><span>//</span><span>一级菜单不超过3个</span> <span>return</span> "toomany1"<span>; }</span><span>elseif</span>((<span>$data</span>['pid']!=0)&&(<span>$this</span>->checksum(<span>$data</span>['pid']))>=7<span>){ </span><span>//</span><span>二级菜单不超过7个</span> <span>return</span> "toomany2"<span>; } </span><span>if</span>(<span>is_array</span>(<span>$data</span>)&&!<span>empty</span>(<span>$data</span><span>)){ </span><span>$keys</span> = "`".<span>implode</span>("`,`",<span>array_keys</span>(<span>$data</span>))."`"<span>; </span><span>$vals</span> = "'".<span>implode</span>("','",<span>array_values</span>(<span>$data</span>))."'"<span>; </span><span>$insert_sql</span> = "insert into {<span>$this</span>->table_name} (<span>$keys</span>) values (<span>$vals</span>)"<span>; </span><span>return</span> <span>$this</span>->query(<span>$insert_sql</span><span>); }</span><span>else</span><span>{ </span><span>return</span> <span>false</span><span>; } } </span><span>public</span> <span>function</span> del(<span>$id</span><span>){ </span><span>$infos</span> = <span>$this</span>->getone(<span>$id</span><span>); </span><span>$del_sql</span> = "delete from {<span>$this</span>->table_name} where id = {<span>$id</span>} and pid = {<span>$id</span>}"<span>; </span><span>return</span> <span>$this</span>->query(<span>$del_sql</span><span>); } </span><span>private</span> <span>function</span> checksum(<span>$id</span> = ''<span>){ </span><span>if</span>(<span>$id</span> == ''<span>){ </span><span>$get_sql</span> = "select count(1) as total from {<span>$this</span>->table_name} where pid =0"<span>; }</span><span>else</span><span>{ </span><span>$id</span> = <span>intval</span>(<span>$id</span><span>); </span><span>$get_sql</span> = "select count(1) as total from {<span>$this</span>->table_name} where pid ={<span>$id</span>}"<span>; } </span><span>$r</span> = <span>$this</span>->db->query(<span>$get_sql</span>)-><span>row(); </span><span>return</span> <span>$r</span>-><span>total; } </span><span>public</span> <span>function</span><span> getplist(){ </span><span>//</span><span>获取一级菜单</span> <span>$get_sql</span> = "select * from {<span>$this</span>->table_name} where pid=0 order by menuorder asc"<span>; </span><span>return</span> <span>$this</span>->db->query(<span>$get_sql</span>)-><span>result_array(); } </span><span>public</span> <span>function</span><span> isplist(){ </span><span>$get_sql</span> = "select pid from {<span>$this</span>->table_name} where pid <> 0 group by pid"<span>; </span><span>return</span> <span>$this</span>->db->query(<span>$get_sql</span>)-><span>result_array(); } </span><span>public</span> <span>function</span><span> getlist(){ </span><span>$get_sql</span> = "select * from {<span>$this</span>->table_name} where 1 order by pid asc, menuorder asc"<span>; </span><span>return</span> <span>$this</span>->db->query(<span>$get_sql</span>)-><span>result_array(); } </span><span>public</span> <span>function</span> maketree(<span>$data</span><span>){ </span><span>$pids</span> = <span>array</span><span>(); </span><span>foreach</span>(<span>$data</span> <span>as</span> <span>$k</span>=><span>$v</span><span>){ </span><span>if</span>(<span>$v</span>['pid'] == 0<span>){ </span><span>$pids</span>[<span>$v</span>['id']][] = <span>$v</span><span>; }</span><span>else</span><span>{ </span><span>$pids</span>[<span>$v</span>['pid']][] = <span>$v</span><span>; } } </span><span>list</span>(<span>$t1</span>,<span>$t2</span>,<span>$t3</span>) = <span>array_values</span>(<span>$pids</span><span>); </span><span>$r</span> = <span>array_merge_recursive</span>(<span>is_array</span>(<span>$t1</span>)?<span>$t1</span>:<span>array</span>(),<span>is_array</span>(<span>$t2</span>)?<span>$t2</span>:<span>array</span>(),<span>is_array</span>(<span>$t3</span>)?<span>$t3</span>:<span>array</span><span>()); </span><span>return</span> <span>$r</span><span>; } </span><span>public</span> <span>function</span> update(<span>$data</span><span>){ </span><span>if</span>(<span>is_array</span>(<span>$data</span>)&&!<span>empty</span>(<span>$data</span><span>)){ </span><span>$id</span> = <span>$data</span>['id'<span>]; </span><span>unset</span>(<span>$data</span>['id'<span>]); </span><span>foreach</span>(<span>$data</span> <span>as</span> <span>$k</span>=><span>$v</span><span>){ </span><span>$update_arr</span>[] = "`".<span>$k</span>."` = '".<span>$v</span>."'"<span>; } </span><span>$update_fs</span> = <span>implode</span>(",",<span>$update_arr</span><span>); </span><span>$update_sql</span> = "update {<span>$this</span>->table_name} set {<span>$update_fs</span>} where id = {<span>$id</span>}"<span>; </span><span>return</span> <span>$this</span>->query(<span>$update_sql</span><span>); }</span><span>else</span><span>{ </span><span>return</span> <span>false</span><span>; } } }</span>
The above are various methods in the model.
The table structure of the database is as follows, with statements for creating tables.
<span>CREATE</span> <span>TABLE</span><span> `menu` ( `id` </span><span>int</span>(<span>11</span>) <span>NOT</span> <span>NULL</span><span> AUTO_INCREMENT, `content` </span><span>varchar</span>(<span>20</span>) <span>DEFAULT</span> <span>NULL</span><span>, `pid` </span><span>int</span>(<span>11</span>) <span>DEFAULT</span> <span>'</span><span>0</span><span>'</span><span>, `menutype` enum(</span><span>'</span><span>click</span><span>'</span>,<span>'</span><span>view</span><span>'</span>,<span>'</span><span>scancode_push</span><span>'</span>,<span>'</span><span>scancode_waitmsg</span><span>'</span>,<span>'</span><span>pic_sysphoto</span><span>'</span>,<span>'</span><span>pic_photo_or_album</span><span>'</span>,<span>'</span><span>pic_weixin</span><span>'</span>,<span>'</span><span>location_select</span><span>'</span>) <span>DEFAULT</span> <span>'</span><span>view</span><span>'</span> COMMENT <span>'</span><span>消息类型</span><span>'</span><span>, `url` </span><span>varchar</span>(<span>200</span>) <span>DEFAULT</span> <span>NULL</span> COMMENT <span>'</span><span>链接地址</span><span>'</span><span>, `clickkey` </span><span>varchar</span>(<span>20</span>) <span>DEFAULT</span> <span>NULL</span> COMMENT <span>'</span><span>事件KEY</span><span>'</span><span>, `menuorder` </span><span>int</span>(<span>11</span>) <span>DEFAULT</span> <span>NULL</span> COMMENT <span>'</span><span>排序</span><span>'</span><span>, `submenu` </span><span>tinyint</span>(<span>2</span>) <span>DEFAULT</span> <span>'</span><span>0</span><span>'</span><span>, </span><span>PRIMARY</span> <span>KEY</span><span> (`id`) ) ENGINE</span><span>=</span>MyISAM AUTO_INCREMENT<span>=</span><span>0</span> <span>DEFAULT</span> CHARSET<span>=</span>utf8
Field | Type | Comment | |
主键 | id | int(11) NOT NULL | |
content | varchar(20) NULL | <内容> | |
pid | int(11) NULL | <父类ID> | |
menutype | enum('click','view','scancode_push','scancode_waitmsg','pic_sysphoto','pic_photo_or_album','pic_weixin','location_select') NULL | 消息类型 | |
url | varchar(200) NULL | 链接地址 | |
clickkey | varchar(20) NULL | 事件KEY | |
menuorder | int(11) NULL | 排序 | |
submenu | tinyint(2) NULL | <是否是子菜单> |
The following is the method of obtaining token written under system/core/common.php. In fact, it needs to be salted, otherwise disgusting people will do disgusting things.
<span>function</span><span> get_access_token(){ </span><span>//</span><span>从微信服务器获取access_token 并保留一个小时</span> <span>$old_filename</span> = APPPATH."cache/".<span>md5</span>(<span>date</span>("YmdH",<span>time</span>()-3600)).".php"<span>; @</span><span>unlink</span>(<span>$old_filename</span><span>); </span><span>$filename</span> = APPPATH."cache/".<span>md5</span>(<span>date</span>("YmdH",<span>time</span>())).".php"<span>; </span><span>if</span>(<span>is_file</span>(<span>$filename</span><span>)){ </span><span>$r</span> = <span>include</span>(<span>$filename</span><span>); }</span><span>else</span><span>{ </span><span>$url</span> = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".<span>APPSECRET; </span><span>$access_token</span> = <span>file_get_contents</span>(<span>$url</span><span>); </span><span>$res</span> = "<?php return ".<span>var_export</span>(json_decode(<span>$access_token</span>,1),1).";"<span>; </span><span>file_put_contents</span>(<span>$filename</span>,<span>$res</span><span>); </span><span>$r</span> = <span>include</span>(<span>$filename</span><span>); } </span><span>return</span> (<span>$r</span>['access_token'<span>]); }</span>
I won’t write about the previous menu management, it’s just to manage the data in that table and make sure that there is no problem with the data in the data table.
Only need
in the controller$this->load->library("Makemenu");
Then call $this->makemenu->dolist();
will be pushed to the WeChat server. You also need to pay attention to defining two constants APPID and APPSECRET in the entry file.
I released it for everyone, I hope it will be useful, and I will make a backup copy for myself.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

An avatar on Netflix is a visual representation of your streaming identity. Users can go beyond the default avatar to express their personality. Continue reading this article to learn how to set a custom profile picture in the Netflix app. How to quickly set a custom avatar in Netflix In Netflix, there is no built-in feature to set a profile picture. However, you can do this by installing the Netflix extension on your browser. First, install a custom profile picture for the Netflix extension on your browser. You can buy it in the Chrome store. After installing the extension, open Netflix on your browser and log into your account. Navigate to your profile in the upper right corner and click

How to customize background image in Win11? In the newly released win11 system, there are many custom functions, but many friends do not know how to use these functions. Some friends think that the background image is relatively monotonous and want to customize the background image, but don’t know how to customize the background image. If you don’t know how to define the background image, the editor has compiled the steps to customize the background image in Win11 below. If you are interested If so, take a look below! Steps for customizing background images in Win11: 1. Click the win button on the desktop and click Settings in the pop-up menu, as shown in the figure. 2. Enter the settings menu and click Personalization, as shown in the figure. 3. Enter Personalization and click on Background, as shown in the picture. 4. Enter background settings and click to browse pictures

A Venn diagram is a diagram used to represent relationships between sets. To create a Venn diagram we will use matplotlib. Matplotlib is a commonly used data visualization library in Python for creating interactive charts and graphs. It is also used to create interactive images and charts. Matplotlib provides many functions to customize charts and graphs. In this tutorial, we will illustrate three examples to customize Venn diagrams. The Chinese translation of Example is: Example This is a simple example of creating the intersection of two Venn diagrams; first, we imported the necessary libraries and imported venns. Then we create the dataset as a Python set, after that we use the "venn2()" function to create

How to customize shortcut key settings in Eclipse? As a developer, mastering shortcut keys is one of the keys to improving efficiency when coding in Eclipse. As a powerful integrated development environment, Eclipse not only provides many default shortcut keys, but also allows users to customize them according to their own preferences. This article will introduce how to customize shortcut key settings in Eclipse and give specific code examples. Open Eclipse First, open Eclipse and enter

CakePHP is a powerful PHP framework that provides developers with many useful tools and features. One of them is pagination, which helps us divide large amounts of data into several pages, making browsing and manipulation easier. By default, CakePHP provides some basic pagination methods, but sometimes you may need to create some custom pagination methods. This article will show you how to create custom pagination in CakePHP. Step 1: Create a custom pagination class First, we need to create a custom pagination class. this

The iOS 17 update for iPhone brings some big changes to Apple Music. This includes collaborating with other users on playlists, initiating music playback from different devices when using CarPlay, and more. One of these new features is the ability to use crossfades in Apple Music. This will allow you to transition seamlessly between tracks, which is a great feature when listening to multiple tracks. Crossfading helps improve the overall listening experience, ensuring you don't get startled or dropped out of the experience when the track changes. So if you want to make the most of this new feature, here's how to use it on your iPhone. How to Enable and Customize Crossfade for Apple Music You Need the Latest

How to implement custom middleware in CodeIgniter Introduction: In modern web development, middleware plays a vital role in applications. They can be used to perform some shared processing logic before or after the request reaches the controller. CodeIgniter, as a popular PHP framework, also supports the use of middleware. This article will introduce how to implement custom middleware in CodeIgniter and provide a simple code example. Middleware overview: Middleware is a kind of request

1. The picture below is the default screen layout of edius. The default EDIUS window layout is a horizontal layout. Therefore, in a single-monitor environment, many windows overlap and the preview window is in single-window mode. 2. You can enable [Dual Window Mode] through the [View] menu bar to make the preview window display the playback window and recording window at the same time. 3. You can restore the default screen layout through [View menu bar>Window Layout>General]. In addition, you can also customize the layout that suits you and save it as a commonly used screen layout: drag the window to a layout that suits you, then click [View > Window Layout > Save Current Layout > New], and in the pop-up [Save Current Layout] Layout] enter the layout name in the small window and click OK
