码迷,mamicode.com
首页 > 微信 > 详细

微信公众平台开发教程(二)_微信自定义菜单的创建

时间:2017-11-06 19:07:22      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:integer   查询   private   两种   appid   pos   点击   发送   获取   

3.微信自定义菜单的创建

自定义菜单接口可实现多种类型按钮,这里主要介绍如下两种:

1、click:点击推事件用户点击click类型按钮后,微信服务器会通过消息接口推送消息类型为event的结构给开发者(参考消息接口指南),并且带上按钮中开发者填写的key值,开发者可以通过自定义的key值与用户进行交互;
2、view:跳转URL用户点击view类型按钮后,微信客户端将会打开开发者在按钮中填写的网页URL,可与网页授权获取用户基本信息接口结合,获得用户基本信息。

接口调用请求说明

http请求方式:POST(请使用https协议) https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN

click和view的请求示例

 {
     "button":[
     {	
          "type":"click",
          "name":"今日歌曲",
          "key":"V1001_TODAY_MUSIC"
      },
      {
           "name":"菜单",
           "sub_button":[
           {	
               "type":"view",
               "name":"搜索",
               "url":"http://www.soso.com/"
            },
            {
                 "type":"miniprogram",
                 "name":"wxa",
                 "url":"http://mp.weixin.qq.com",
                 "appid":"wx286b93c14bbf93aa",
                 "pagepath":"pages/lunar/index"
             },
            {
               "type":"click",
               "name":"赞一下我们",
               "key":"V1001_GOOD"
            }]
       }]
 }
具体代码实现,这里主要贴出拼装上述代码节点的代码,发送请求方式可以看看我之前的博客,通过HttpsURLConnection发送post请求。
/**
     * 组装自定义菜单数据
     * @date 2016-4-9 下午04:25:55
     * @param account_id
     * @return
     */
    private Map queryChannel(String account_id){
        //查询公众号配置
        Map account = siteConfigManageService.queryWxAccount(account_id);
        List<Map<String, Object>> channels//获取本地配置
        List<Map> menus = new ArrayList<Map>();
        for (Map channel : channels) {
            String channel_id = (String) channel.get("id");
            String parent_id = (String) channel.get("parent_id");
            String channel_type = (String) channel.get("channel_type");
            String channelCode = (String) channel.get("channel_code");
            if (StringUtils.isNotBlank(parent_id) && parent_id.equals("0")){
                List<Map<String, Object>> childrens = getChannelChildren(channel_id, channels);
                Map menu = new HashMap();
                if(childrens.size()>0){
                    List childMenus = getMenu(childrens, account);
                    menu.put("name", channel.get("channel_name"));
                    menu.put("sub_button", childMenus);
                }else{
                    int channelType = Integer.valueOf(channel_type);
                    switch (channelType){
                        case 1:
                            menu.put("name", channel.get("channel_name"));
                            menu.put("type", WxConsts.BUTTON_VIEW);
                            menu.put("url", parseAuthUrl(channel,account));
                            break;
                        case 2:
                            menu.put("name", channel.get("channel_name"));
                            menu.put("type", WxConsts.BUTTON_CLICK);
                            menu.put("key", channelCode);
                    }
                }
                menus.add(menu);
            }
        }
        
        Map button = new HashMap();
        button.put("button", menus);
        return button;
    }
    
    //拼装子节点
    private List getChannelChildren(String channel_id, List<Map<String, Object>> channels){
        List children = new ArrayList();
        for(Map channel:channels){
            String parent_id = (String) channel.get("parent_id");
            if(channel_id.equals(parent_id)){
                children.add(channel);
            }
        }
        return children;  
    }
    
    
    private List getMenu(List<Map<String, Object>> channels, Map account){
        List menus = new ArrayList();
        for(Map channel:channels){
            Map menu = new HashMap();
            String channel_type = (String) channel.get("channel_type");
            String channelCode = (String) channel.get("channel_code");
            int channelType = Integer.valueOf(channel_type);
            switch (channelType){
                case 1:
                    menu.put("name", channel.get("channel_name"));
                    menu.put("type", WxConsts.BUTTON_VIEW);
                    menu.put("url", parseAuthUrl(channel, account));
                    break;
                case 2:
                    menu.put("name", channel.get("channel_name"));
                    menu.put("type", WxConsts.BUTTON_CLICK);
                    menu.put("key", channelCode);
            }
            menus.add(menu);
        }
        return menus;  
    }

 




微信公众平台开发教程(二)_微信自定义菜单的创建

标签:integer   查询   private   两种   appid   pos   点击   发送   获取   

原文地址:http://www.cnblogs.com/Onedayzk/p/7794346.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!