The custom menu functionality is implemented under the CI framework. wrote a model, a class library. By the way with the Access_token implementation
Phpclassmakemenu{ Public $menustr; Public function__construct () {} Public functioninit () {$this-dolist (); return $this-SetMenu (); } Private functiondolist () {$CI=&get_instance (); $CILoad->model ("Menu_model", "menu"); $plist=$CI->menuisplist (); foreach($plist as $pid){ $pidarr[] =$pid[' PID ']; } $list=$CI->menu->maketree ($CI->menu->getlist ()); foreach($list as $btn){ if(In_array($btn[' ID '],$pidarr)){ //generate a link with no key and URL as a parent menu $btn _arr[$btn[' ID ']] =Array("type" = =$btn[' Menutype '], ' name ' and ' = '$btn[' Content ']); }ElseIf($btn[' pid '] = = 0){ //create an action-level menu $btn _arr[$btn[' ID ']] =Array("type" = =$btn[' Menutype '], ' name ' and ' = '$btn[' content '], ' key ' =$btn[' Clickkey '], "url" = =$btn[' URL ']); }Else{ //creating submenus $btn _arr[$btn[' PID '] [' Sub_button '] [] =Array("type" = =$btn[' Menutype '], ' name ' and ' = '$btn[' content '], ' key ' =$btn[' Clickkey '], "url" = =$btn[' URL ']); } } $btnarr[' button '] =array_values($btn _arr); $r=$this->menustr = Json_encode ($btnarr,Json_unescaped_unicode); return $r; } Private functionSetMenu () {$accesstoken=Get_access_token (); $url= "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$accesstoken}"; $ch=Curl_init (); curl_setopt ($ch, Curlopt_url,$url); curl_setopt ($ch, Curlopt_customrequest, "POST"); curl_setopt ($ch, Curlopt_ssl_verifypeer,FALSE); curl_setopt ($ch, Curlopt_ssl_verifyhost,FALSE); curl_setopt ($ch, Curlopt_useragent, ' mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0) '); curl_setopt ($ch, Curlopt_followlocation, 1); curl_setopt ($ch, Curlopt_autoreferer, 1); curl_setopt ($ch, Curlopt_postfields,$this-menustr); curl_setopt ($ch, Curlopt_returntransfer,true); $info= Curl_exec ($ch); if(Curl_errno ($ch)) { returnCurl_error ($ch); } curl_close ($ch); return $info; }}
The above is the contents of the library, mainly based on the data table to generate the menu JSON string
PhpclassMenu_modelextendsCi_model { Public $table _name; Public function__construct () {Parent::__construct (); $this->load->database (); $this->table_name = "Data_menu"; } Public functionQuery$sql){ return $this->db->query ($sql); } Public functionGetOne ($id){ $get _sql= "Select * FROM {$this->table_name} where id = {$id}"; return $this->query ($get _sql),row (); } Public functionAddOne ($data){ if(($data[' pid '] = = 0) && ($this->checksum () >=3)){ //no more than 3 first-level menus return"Toomany1"; }ElseIf(($data[' PID ']!=0) && ($this->checksum ($data[' PID '])) >=7){ //no more than 7 levels of menu two return"Toomany2"; } if(Is_array($data) &&!Empty($data)){ $keys= "`".implode("`,`",Array_keys($data))." `"; $vals= "'".implode("','",array_values($data))."'"; $insert _sql= "INSERT INTO {$this->table_name} ($keys) VALUES ($vals)"; return $this->query ($insert _sql); }Else{ return false; } } Public functionDel$id){ $infos=$this->getone ($id); $del _sql= "Delete from {$this->table_name} where id = {$id} and PID = {$id}"; return $this->query ($del _sql); } Private functionChecksum$id= ''){ if($id== ''){ $get _sql= "SELECT count (1) as Total from {$this->table_name} where pid = 0 "; }Else{ $id=intval($id); $get _sql= "SELECT count (1) as Total from {$this->table_name} where PID ={$id}"; } $r=$this->db->query ($get _sql),row (); return $r-Total ; } Public functiongetplist () {//Get a first- level menu $get _sql= "Select * FROM {$this->table_name} where Pid=0 ORDER by menuorder ASC "; return $this->db->query ($get _sql),Result_array (); } Public functionisplist () {$get _sql= "Select pid from {$this->table_name} where PID <> 0 GROUP by PID "; return $this->db->query ($get _sql),Result_array (); } Public functiongetlist () {$get _sql= "Select * FROM {$this->table_name} where 1 ORDER by PID ASC, Menuorder ASC "; return $this->db->query ($get _sql),Result_array (); } Public functionMaketree ($data){ $pids=Array(); foreach($data as $k=$v){ if($v[' pid '] = = 0){ $pids[$v[' ID ']] [] =$v; }Else{ $pids[$v[' PID '] [] =$v; } } List($t 1,$t 2,$t 3) =array_values($pids); $r=array_merge_recursive(Is_array($t 1)?$t 1:Array(),Is_array($t 2)?$t 2:Array(),Is_array($t 3)?$t 3:Array()); return $r; } Public functionUpdate$data){ if(Is_array($data) &&!Empty($data)){ $id=$data[' ID ']; unset($data[' ID ']); foreach($data as $k=$v){ $update _arr[] = "`".$k." ` = '".$v."'"; } $update _fs=implode(",",$update _arr); $update _sql= "Update {$this->table_name} set {$update _fs} where id = {$id}"; return $this->query ($update _sql); }Else{ return false; } }}
The above is the model inside the various methods.
The table structure of the database is as follows, with the statement creating the table.
CREATE TABLE' menu ' (' id ' )int( One) not NULLauto_increment, ' content 'varchar( -)DEFAULT NULL, ' pid 'int( One)DEFAULT '0', ' Menutype ' enum ('Click','View','Scancode_push','scancode_waitmsg','Pic_sysphoto','Pic_photo_or_album','pic_weixin','Location_select')DEFAULT 'View'COMMENT'Message Type', ' url 'varchar( $)DEFAULT NULLCOMMENT'Link Address', ' Clickkey 'varchar( -)DEFAULT NULLCOMMENT'Event Key', ' Menuorder 'int( One)DEFAULT NULLCOMMENT'Sort', ' submenu 'tinyint(2)DEFAULT '0', PRIMARY KEY(' id ')) ENGINE=MyISAM auto_increment=0 DEFAULTCHARSET=Utf8
|
Field |
Type |
Comment |
Primary key |
Id |
Int (one) not NULL |
|
|
Content |
varchar (+) NULL |
<内容> |
|
Pid |
Int (one) NULL |
<父类id> |
|
Menutype |
Enum (' Click ', ' View ', ' Scancode_push ', ' scancode_waitmsg ', ' Pic_sysphoto ', ' pic_photo_or_album ', ' pic_weixin ', ' Location_select ') NULL |
Message type |
|
Url |
varchar ($) NULL |
Link Address |
|
Clickkey |
varchar (+) NULL |
Event key |
|
Menuorder |
Int (one) NULL |
Sort |
|
Submenu |
tinyint (2) NULL |
<是否是子菜单> |
Below is written in the system/core/common.php below the method of obtaining tokens, in fact, to do a salt treatment, or there will be disgusting people do disgusting things.
functionGet_access_token () {//get Access_token from the server and keep it for one hours $old _filename= APPPATH. " cache/".MD5(Date("YMDH", Time()-3600)). ". Php; @unlink($old _filename); $filename= APPPATH. " cache/".MD5(Date("YMDH", Time())).". Php; if(Is_file($filename)){ $r=include($filename); }Else{ $url= "Https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=". APPID. " &secret= ".Appsecret; $access _token=file_get_contents($url); $res= "
Var_export(Json_decode ($access _token, 1), 1). ";"; file_put_contents($filename,$res); $r=include($filename); } return($r[' Access_token ']); }
The front of the menu management will not write, is to manage the table of data, to ensure that the data table inside the data is not a problem.
Inside the controller, you only need
$this->load->library ("Makemenu");
Then call $this->makemenu->dolist ();
Will be pushed to the server. It is also important to note that two constants AppID and Appsecret are defined in a portal file.
Let out to everyone, hope useful, also give me back up a.
The above describes the CI Framework development-custom menu, including the aspects of the content, want to be interested in PHP tutorial friends helpful.