Background code implementation of custom menu on WeChat public platform

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to learn how to create a new custom menu. The frontend uses ajax to transmit custom menu information to the backend in sequence, the backend first stores the information in the database, and then organizes the data according to the information stored in the database during release. Then, call the API to publish a custom menu. Front-end data backfilling directly pulls information from the database.
// Create a custom menu
Public function createMenu (){
/*
* Custom menu creation Logic
* 1. First, determine whether the created menu is a main menu or a submenu.
* 2. If it is a main menu, determine whether a sub-menu has been created. If yes, you can only modify the menu name. Otherwise, it is equivalent to creating a sub-menu. If it is a sub-menu, select the creation type.
* 3. Determine the creation type: Send messages (text messages, text messages, images, and coupons) and redirect to the webpage
* 4. Collect relevant information for the message sending type
*/
// Logic for processing data tables in the database.
/*
* 1. Based on the information in the database, select the latest one-day information as the selection for creating the custom menu, and use this part of data as the selected raw data.
* 2. query the information of the main menu to form an array. deduplication is used.
* 3. query sub-menu information to form an array
* 4. Organize data
*/

Header ("Content-Type: text/html; charset = UTF-8 ");
// Instantiate the menu table
$ Menu = M ('weixin _ menu ');
$ Create_time = $ menu-> order ('create _ time desc ')-> limit (1)-> getField ('create _ Time ');
// Query the information of the main menu, which is an array consisting of deduplication and sorting.
$ Zhu_name = $ menu-> distinct (true)-> where ("create_time = '$ create_time'")-> field ('zhu _ name')-> select ();
// Dump ($ zhu_name );
// Find the pid of the data table in the database based on the name of the corresponding Main Menu. If the pid is 0, obtain the uid directly.
// Organize the data names of the Main Menu

$ Menu_data = array ();
Foreach ($ zhu_name as $ key => $ value ){
$ Tip = $ value ['zhu _ name'];
// Array query Conditions
$ Data ['create _ time'] = $ create_time;
$ Data ['zhu _ name'] = $ tip;
$ Pid = $ menu-> where ($ data)-> getField ('pid ');
If ($ pid = 0 ){
$ Pid = $ menu-> where ($ data)-> getField ('uid ');
}
$ Menu_data [$ pid] = array ();
// Directly organize the data and find the corresponding sub-menu based on the pid.
$ Zi_data ['pid '] = $ pid;
$ Zi_data ['create _ time'] = $ create_time;
$ Zi_menu_data = $ menu-> where ($ zi_data)-> order ('uid')-> select (); // if the data is empty, no sub-menu is displayed; otherwise, the information of the organizational unit sub-menu is
If (! Empty ($ zi_menu_data) {// organizes sub-menu data
$ Menu_data [$ pid] ['name'] = $ tip;
Foreach ($ zi_menu_data as $ key => $ value ){
// Determine the corresponding menu type
Switch ($ value ['type']) {
Case 'view _ limited ':
$ Type = 'Media _ id ';
$ Type_data = $ value ['Type _ media_id '];
Break;
Case 'Media _ id ':
$ Type = 'Media _ id ';
$ Type_data = $ value ['Type _ media_id '];
Break;
Case 'click ':
$ Type = 'key ';
$ Type_data = $ value ['Type _ key'];
Break;
Case 'view ':
$ Type = 'url ';
$ Type_data = $ value ['Type _ url'];
Break;
}
$ Menu_data [$ pid] ['sub _ button'] [$ key] = array (
'Type' => $ value ['type'],
'Name' => $ value ['zi _ name'],
$ Type => $ type_data
);
}
} Else {// organize the main menu data
$ Zhu_data ['uid'] = $ pid;
$ Zhu_data ['pid '] = 0;
$ Zhu_data ['create _ time'] = $ create_time;
$ Zhu_menu_data = $ menu-> where ($ zhu_data)-> find ();
$ Menu_data [$ pid] ['name'] = $ tip;
// Determine the corresponding menu type
Switch ($ zhu_menu_data ['type']) {
Case 'view _ limited ':
$ Z_type = 'Media _ id ';
$ Z_type_data = $ zhu_menu_data ['Type _ media_id '];
Break;
Case 'Media _ id ':
$ Z_type = 'Media _ id ';
$ Z_type_data = $ zhu_menu_data ['Type _ media_id '];
Break;
Case 'click ':
$ Z_type = 'key ';
$ Z_type_data = $ zhu_menu_data ['Type _ key'];
Break;
Case 'view ':
$ Z_type = 'url ';
$ Z_type_data = $ zhu_menu_data ['Type _ url'];
Break;
}
$ Menu_data [$ pid] ['type'] = $ zhu_menu_data ['type'];
$ Menu_data [$ pid] [$ z_type] = $ z_type_data;

}

}
Ksort ($ menu_data );
Sort ($ menu_data );

$ Zong_menu_data = array (
'Button '=> $ menu_data
);
// Dump ($ zong_menu_data );

$ Type = 'create ';
$ M_data = json_encode ($ zong_menu_data, JSON_UNESCAPED_UNICODE );
$ Access_token = $ this-> getToken ();
$ Res = $ this-> MenuAPI ($ type, $ access_token, $ m_data );
If ($ res ){
Echo 'success ';
}

}

Custom menu document. Zip (1.4 MB download: 161 times)

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.