Share an example tutorial on developing custom menus for WeChat public accounts

零下一度
Release: 2017-05-19 16:12:50
Original
4043 people have browsed it

Welcome to leave a message, forward,

Click me for the project source code reference address - Welcome to Start

The previous articles have already talked about how to import the project, how to start the configuration project, how to become a developer, and what is the source code analysis news? Interaction (If the first four items are not very clear, you can read here to quickly develop WeChat public accounts. This article will talk about how to implement a custom menu

There are two ways to implement a custom menu
1. Edit mode
2. Development Mode

The editing mode is very simple and I won’t go into details...

Development mode to implement custom menu

1. Use the WeChat public platform interface debugging tool to implement it
2. Use the official interface to implement it

Early preparation

Note:
1. Currently, subscription accounts can only use editing mode and cannot add hyperlinks. Development mode can only be used after WeChat authentication.
2. Editing mode and development mode cannot be turned on at the same time. 3. Generate. The menu will not be displayed immediately (the next day). If you want to see the effect immediately, you can unfollow and follow again

Add plug-in

Share an example tutorial on developing custom menus for WeChat public accounts

K8WTIEI86W9W5XERD`MRD{6.png

Use the WeChat public platform interface debugging tool to achieve

Share an example tutorial on developing custom menus for WeChat public accounts

Custom menu-interface debugging tool.png

You can see that two parameters are required and both are required

access_token body

Someone wants to ask, how to obtain these two parameters

body is actually a JSON object that needs to be generated for the menu. The official provides a chestnut for reference.

 {
     "button":[
     {    
          "type":"click",
          "name":"今日歌曲",
          "key":"V1001_TODAY_MUSIC"
      },
      {
           "name":"菜单",
           "sub_button":[
           {    
               "type":"view",
               "name":"搜索",
               "url":"http://www.soso.com/"
            },
            {
               "type":"view",
               "name":"视频",
               "url":"http://v.qq.com/"
            },
            {
               "type":"click",
               "name":"赞一下我们",
               "key":"V1001_GOOD"
            }]
       }]
 }
Copy after login

access_token is obtained as shown below

Share an example tutorial on developing custom menus for WeChat public accounts

Customized menu - Get access_token.png

Use. Officially provided interface implementation

Students who are learning about WeChat custom menus for the first time are advised to read the official documents three times first

In

Jfinal-weixin, there are encapsulated menu creation, query, deletion, and personalized menus Create, query, delete, test personalized menu matching results

Share an example tutorial on developing custom menus for WeChat public accounts

Custom menu-menu encapsulation interface.png

So the question is, how to use it after encapsulation?

The following is the package provided? The interface

    //查询自定义菜单
    public static ApiResult getMenu() {
        String jsonResult = HttpUtils.get(getMenu + AccessTokenApi.getAccessTokenStr());
        return new ApiResult(jsonResult);
    }
    //创建自定义菜单
    public static ApiResult createMenu(String jsonStr) {
        String jsonResult = HttpUtils.post(createMenu + AccessTokenApi.getAccessTokenStr(), jsonStr);
        return new ApiResult(jsonResult);
    }
    //删除自定义菜单
    public static ApiResult deleteMenu() {
        String jsonResult = HttpUtils.get(deleteMenuUrl + AccessTokenApi.getAccessTokenStr());
        return new ApiResult(jsonResult);
    }
    //创建个性化自定义菜单
    public static ApiResult addConditional(String jsonStr) {
        String jsonResult = HttpUtils.post(addConditionalUrl + AccessTokenApi.getAccessTokenStr(), jsonStr);
        return new ApiResult(jsonResult);
    }
    //删除个性化自定义菜单
    public static ApiResult delConditional(String menuid) {
        HashMap params = new HashMap();
        params.put("menuid", menuid);
        String url = delConditionalUrl + AccessTokenApi.getAccessTokenStr();
        String jsonResult = HttpUtils.post(url, JsonUtils.toJson(params));
        return new ApiResult(jsonResult);
    }
    //测试个性化菜单匹配结果
    public static ApiResult tryMatch(String userId) {
        HashMap params = new HashMap();
        params.put("user_id", userId);
        String url = tryMatchUrl + AccessTokenApi.getAccessTokenStr();
        String jsonResult = HttpUtils.post(url, JsonUtils.toJson(params));
        return new ApiResult(jsonResult);
    }

    public static ApiResult getCurrentSelfMenuInfo() {
        String jsonResult = HttpUtils.get(getCurrentSelfMenuInfoUrl + AccessTokenApi.getAccessTokenStr());
        return new ApiResult(jsonResult);
    }
Copy after login

provides a detailed usage demo in the open source project weixin_guide

com.javen.weixin.menu.MenuManager class

public static void main(String[] args) { 

           // 将菜单对象转换成json字符串
           String jsonMenu = JsonKit.toJson(getTestMenu()).toString();
           System.out.println(jsonMenu);
           ApiConfig ac = new ApiConfig();

            // 配置微信 API 相关常量 请使用你自己公众号的
            ac.setAppId("wx614c453e0d1dcd12");
            ac.setAppSecret("19a02e4927d346484fc70327970457f9");
//          ac.setAppId(PropKit.get("appId"));
//          ac.setAppSecret(PropKit.get("appSecret"));
            ApiConfigKit.setThreadLocalApiConfig(ac);

           //创建菜单
           ApiResult apiResult=MenuApi.createMenu(jsonMenu);
           System.out.println(apiResult.getJson());
     }
Copy after login

You can see that the main method calls

MenuApi.createMenu(jsonMenu)that
Where does jsonMenu come from?

In fact, this is the encapsulation of the menu by converting the entity object of the custom menu into JSON

String jsonMenu = JsonKit.toJson(getTestMenu()).toString();

Executing the main method will output the JSON of the generated menu and the status of the response

/** 
         * 组装菜单数据 
         *  
         * @return 
         */  
        private static Menu getTestMenu() {  
            ClickButton btn11 = new ClickButton();  
            btn11.setName("微信相册发图");  
            btn11.setType("pic_weixin");  
            btn11.setKey("rselfmenu_1_1");

            ClickButton btn12 = new ClickButton();  
            btn12.setName("拍照或者相册发图");  
            btn12.setType("pic_photo_or_album");  
            btn12.setKey("rselfmenu_1_2");;  

            ClickButton btn13 = new ClickButton();  
            btn13.setName("系统拍照发图");  
            btn13.setType("pic_sysphoto");  
            btn13.setKey("rselfmenu_1_3");

            ClickButton btn21 = new ClickButton();  
            btn21.setName("扫码带提示");  
            btn21.setType("scancode_waitmsg");  
            btn21.setKey("rselfmenu_2_1");;  

            ClickButton btn22 = new ClickButton();  
            btn22.setName("扫码推事件");  
            btn22.setType("scancode_push");  
            btn22.setKey("rselfmenu_2_2");;  

            ViewButton btn23 = new ViewButton();  
            btn23.setName("我的设备");  
            btn23.setType("view");  
            btn23.setUrl("https://hw.weixin.qq.com/devicectrl/panel/device-list.html?appid=wx614c453e0d1dcd12"); 

            ViewButton btn31 = new ViewButton();  
            btn31.setName("微社区");  
            btn31.setType("view");  
            btn31.setUrl("http://whsf.tunnel.mobi/whsf/msg/wsq");  


            ClickButton btn32 = new ClickButton();  
            btn32.setName("发送位置");  
            btn32.setType("location_select");  
            btn32.setKey("rselfmenu_3_2"); 

            //http://tencent://message/?uin=572839485&Site=在线咨询&Menu=yes
            //http://wpa.qq.com/msgrd?v=3&uin=572839485&site=qq&menu=yes

            ViewButton btn33 = new ViewButton();  
            btn33.setName("在线咨询");  
            btn33.setType("view");  
            btn33.setUrl("http://wpa.qq.com/msgrd?v=3&uin=572839485&site=qq&menu=yes");  

            ViewButton btn34 = new ViewButton();  
            btn34.setName("我的博客");  
            btn34.setType("view");  
            btn34.setUrl("http://www.cnblogs.com/zyw-205520"); 

            ClickButton btn35 = new ClickButton();  
            btn35.setName("点击事件");  
            btn35.setType("click");  
            btn35.setKey("rselfmenu_3_5"); 

            ComButton mainBtn1 = new ComButton();  
            mainBtn1.setName("发图");  
            mainBtn1.setSub_button(new Button[] { btn11, btn12, btn13});  

            ComButton mainBtn2 = new ComButton();  
            mainBtn2.setName("扫码");  
            mainBtn2.setSub_button(new Button[] { btn21, btn22 ,btn23});  

            ComButton mainBtn3 = new ComButton();  
            mainBtn3.setName("个人中心");  
            mainBtn3.setSub_button(new Button[] { btn31, btn32, btn33, btn34 ,btn35 });  

            /** 
             * 这是公众号xiaoqrobot目前的菜单结构,每个一级菜单都有二级菜单项<br> 
             *  
             * 在某个一级菜单下没有二级菜单的情况,menu该如何定义呢?<br> 
             * 比如,第三个一级菜单项不是“更多体验”,而直接是“幽默笑话”,那么menu应该这样定义:<br> 
             * menu.setButton(new Button[] { mainBtn1, mainBtn2, btn33 }); 
             */  
            Menu menu = new Menu();  
            menu.setButton(new Button[] { mainBtn1, mainBtn2, mainBtn3 });  
            return menu;  
        }
Copy after login

The above is the whole process of generating a custom menu.

【Related recommendations】

1.

WeChat public account platform source code download

2.

Weizhichuang T+ WeChat robot source code

3.

WeChat Network King v3.4.5 Advanced Business Edition WeChat Rubik’s Cube source code

The above is the detailed content of Share an example tutorial on developing custom menus for WeChat public accounts. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!