Table of Contents
CI框架微信开发-自定义菜单,ci框架自定义菜单
Home php教程 php手册 CI框架微信开发-自定义菜单,ci框架自定义菜单

CI框架微信开发-自定义菜单,ci框架自定义菜单

Jun 13, 2016 am 09:19 AM
customize

CI框架微信开发-自定义菜单,ci框架自定义菜单

在CI框架下面实现了自定义菜单功能.写了一个model,一个类库.顺便附带access_token的实现方式

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

<?<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>

Copy after login

上面是library里面的内容,主要是根据数据表生成菜单的json串

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

<?<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>

Copy after login

上面是model里面的各种方法.

数据库的表结构如下,附创建表的语句.

1

2

3

4

5

6

7

8

9

10

11

<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

Copy after login

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 <是否是子菜单>

下面是写在system/core/common.php下面的获取token的方法,其实要做一个加盐处理,要么会有恶心的人做恶心的事情.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<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>

Copy after login

 

 

前面的菜单管理就不写了,就是管理那个表的数据,保证数据表里面的数据没问题即可.

在控制器里面只需要

$this->load->library("Makemenu");

然后调用  $this->makemenu->dolist();

就会推送到微信的服务器.   还需要注意在入口文件定义两个常量   APPID和APPSECRET    .

放出来给大家,希望有用,也给我自己备份个.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How to quickly set up a custom avatar in Netflix How to quickly set up a custom avatar in Netflix Feb 19, 2024 pm 06:33 PM

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 How to customize background image in Win11 Jun 30, 2023 pm 08:45 PM

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

How to create and customize Venn diagrams in Python? How to create and customize Venn diagrams in Python? Sep 14, 2023 pm 02:37 PM

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

render function in Vue3: custom rendering function render function in Vue3: custom rendering function Jun 18, 2023 pm 06:43 PM

Vue is a popular JavaScript framework that provides many convenient functions and APIs to help developers build interactive front-end applications. With the release of Vue3, the render function has become an important update. This article will introduce the concept and purpose of the render function in Vue3 and how to use it to customize the rendering function. What is the render function? In Vue, template is the most commonly used rendering method, but in Vue3, you can use another method: r

How to customize shortcut key settings in Eclipse How to customize shortcut key settings in Eclipse Jan 28, 2024 am 10:01 AM

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

How to enable and customize crossfades in Apple Music on iPhone with iOS 17 How to enable and customize crossfades in Apple Music on iPhone with iOS 17 Jun 28, 2023 pm 12:14 PM

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 create custom pagination in CakePHP? How to create custom pagination in CakePHP? Jun 04, 2023 am 08:32 AM

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

How to implement custom middleware in CodeIgniter How to implement custom middleware in CodeIgniter Jul 29, 2023 am 10:53 AM

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

See all articles