CI framework WeChat development-custom menu

Source: Internet
Author: User
: This article mainly introduces the CI framework development-custom menu. if you are interested in the PHP Tutorial, refer to it. The custom menu function is implemented under the CI framework. a model and a class library are written. the access_token implementation method is included.

 Dolist (); return $ this-> setmenu ();} private function dolist () {$ CI = & get_instance (); $ CI-> load-> model ("Menu_model", "menu"); $ plist = $ CI-> menu-> isplist (); 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 without a key or url as the parent menu $ btn_arr [$ btn ['id'] = array ("type" => $ btn ['menutype' ], "name" => $ btn ['content']);} elseif ($ btn ['pid'] = 0) {// Generate an operation-level menu $ btn_arr [$ btn ['id'] = array ("type" => $ btn ['menutype '], "name" => $ btn ['content'], "key" => $ btn ['clickkey'], "url" => $ btn ['URL']);} else {// generate sub menu $ btn_arr [$ btn ['pid '] ['sub _ call'] [] = array ("type" => $ btn ['menutype '], "name" => $ 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 function setmenu () {$ 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, success, 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 )) {return curl_error ($ ch);} curl_close ($ ch); return $ info ;}}

The above is the content in the library, mainly generating the menu json string based on the data table

 Load-> database (); $ this-> table_name = "data_menu";} public function query ($ SQL) {return $ this-> db-> query ($ SQL);} public function getone ($ id) {$ get_ SQL = "select * from {$ this-> table_name} where id = {$ id}"; return $ this-> query ($ get_ SQL) -> row ();} public function addone ($ data) {if ($ data ['pid '] = 0) & ($ this-> checksum ()> = 3) {// no more than three return "toomany1";} elseif ($ data ['pid']! = 0) & ($ this-> checksum ($ data ['pid ']) >=7) {// no more than 7 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 function del ($ id) {$ infos = $ this-> getone ($ id ); $ del_ SQL = "delete from {$ this-> table_name} where id = {$ id} and pid = {$ id}"; return $ this-> query ($ Del_ SQL);} private function checksum ($ 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 function getplist () {// obtain the 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 function isplist () {$ get_ SQL = "select pid from {$ this-> table_name} where pid <> 0 group by pid"; return $ this-> db-> query ($ get_ SQL) -> result_array ();} public function getlist () {$ get_ SQL = "select * from {$ this-> table_name} where 1 order by pid asc, menuorder asc "; return $ this-> db-> query ($ get_ SQL)-> result _ Array ();} public function maketree ($ data) {$ pids = array (); foreach ($ data as $ k => $ v) {if ($ v ['pid '] = 0) {$ pids [$ v ['id'] [] = $ v ;} else {$ pids [$ v ['pid'] [] = $ v ;}} list ($ t1, $ t2, $ t3) = array_values ($ pids ); $ r = array_merge_recursive (is_array ($ t1 )? $ T1: array (), is_array ($ t2 )? $ T2: array (), is_array ($ t3 )? $ T3: array (); return $ r;} public function update ($ 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 are the methods in the model.

The table structure of the database is as follows, with the statement for creating the table.

Create table 'menu '('id' int (11) not null AUTO_INCREMENT, 'content' varchar (20) default null, 'pid' int (11) 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 (200) default null comment 'link address ', 'clickkey' varchar (20) default null comment' event key', 'menuorder' int (11) default null comment' sort', 'submenu 'tinyint (2) DEFAULT '0', primary key ('id') ENGINE = MyISAM AUTO_INCREMENT = 0 default charset = utf8

Field Type Comment
Primary key 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 Message Type
Url Varchar (200) NULL Link address
Clickkey Varchar (20) NULL Event KEY
Menuorder Int (11) NULL Sort
Submenu Tinyint (2) NULL <是否是子菜单>

The following is the method for obtaining tokens written in system/core/common. php. In fact, it is necessary to add salt, or some disgusting people will do disgusting things.

Function get_access_token () {// get access_token from the server and keep it for an hour $ 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 ="
 

The previous menu management will not be written, that is, to manage the data of that table, to ensure that the data in the data table is no problem.

In the controller, you only need

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

Then call $ this-> makemenu-> dolist ();

It will be pushed to the server. also note that two constants APPID and APPSECRET are defined in the entry file.

I hope it will be useful and back up it for myself.

The above introduces the CI framework development-custom menu, including the content, hope to be helpful to friends who are interested in PHP tutorials.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.