JAVA微信公众号网页开发——生成自定义微信菜单(携带参数)

Wesley13
• 阅读 573

官网接口地址:https://developers.weixin.qq.com/doc/offiaccount/Custom_Menus/Creating_Custom-Defined_Menu.html

//创建一个微信菜单实体类

WeixinMenu.java    

package com.weixin.menu;

import java.io.Serializable;
import java.util.Set;


public abstract class WeixinMenu implements Serializable {


    // primary key
    private Integer id;

    // fields
    private String name;
    private String type;
    private String url;
    private String key;

    // many to one
    private WeixinMenu parent;

    // collections
    private java.util.Set<WeixinMenu> child;


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public WeixinMenu getParent() {
        return parent;
    }

    public void setParent(WeixinMenu parent) {
        this.parent = parent;
    }

    public Set<WeixinMenu> getChild() {
        return child;
    }

    public void setChild(Set<WeixinMenu> child) {
        this.child = child;
    }

    public String toString() {
        return super.toString();
    }


}

//控制器方法

WeixinMenuAct.java

package com.weixin.menu;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.net.URI;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class Menu {


    /**
     * 生成微信菜单请求方法
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/weixinMenu/o_menu.do")
    public String menu(HttpServletRequest request, ModelMap model) {
        List<WeixinMenu> menus =null; //获取菜单集合
        String msg =createMenu(getMenuJsonString(menus));
        try {
            JSONObject object = new JSONObject(msg);
            if (!object.get("errcode").equals("0")){
                model.addAttribute("msg",msg);
                //操作失败处理代码
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 创建自定义菜单
     */
    public String createMenu(String menus){
        String token=getToken();//获取access_token
        String createMenuUrl="https://api.weixin.qq.com/cgi-bin/menu/create"; //微信提供的菜单接口地址
        String url = createMenuUrl+"?access_token=" + token;
        String msg = post(url, menus,"application/json");
        return msg;
    }

    /**
     * 获取access_token
     * @return
     */
    public String getToken() {
        String tokenGetUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";//微信提供获取access_token接口地址
        String appid="";
        String secret="";

        System.out.println("~~~~~appid:"+appid);
        System.out.println("~~~~~secret:"+secret);
        JSONObject tokenJson=new JSONObject();
        if(StringUtils.isNotBlank(appid)&&StringUtils.isNotBlank(secret)){
            tokenGetUrl+="&appid="+appid+"&secret="+secret;
            tokenJson=getUrlResponse(tokenGetUrl);
            System.out.println("~~~~~tokenJson:"+tokenJson.toString());
            try {
                return (String) tokenJson.get("access_token");
            } catch (JSONException e) {
                System.out.println("报错了");
                return null;
            }
        }else{
            System.out.println("appid和secret为空");
            return null;
        }
    }

    private  JSONObject getUrlResponse(String url){
        CharsetHandler handler = new CharsetHandler("UTF-8");
        try {
            HttpGet httpget = new HttpGet(new URI(url));
            HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
            //HttpClient
            CloseableHttpClient client = httpClientBuilder.build();
            client = (CloseableHttpClient) wrapClient(client);
            return new JSONObject(client.execute(httpget, handler));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 将菜单集合转换为json数据
     * @param menus
     * @return
     */
    public String getMenuJsonString(List<WeixinMenu> menus) {

        String strJson = "{" +
                "\"button\":[";

        for (int i = 0; i < menus.size(); i++) {
            strJson = strJson + "{    ";
            WeixinMenu menu = menus.get(i);
            if(menu.getChild().size()>0){
                strJson = strJson +
                        "\"name\":\""+menu.getName()+"\","+
                        "\"sub_button\":[";
                Set<WeixinMenu> sets = menu.getChild();
                Iterator<WeixinMenu> iter = sets.iterator();
                int no = 1;
                while(iter.hasNext()){
                    if(no>5){
                        break;
                    }else{
                        if(no==1){
                            strJson = strJson + "{";
                        }else{
                            strJson = strJson + ",{";
                        }
                        WeixinMenu child = iter.next();
                        if(child.getType().equals("click")){
                            strJson = strJson +
                                    "\"type\":\"click\","+
                                    "\"name\":\""+child.getName()+"\","+
                                    "\"key\":\""+child.getKey()+"\"}";
                        }else{
                            strJson = strJson +
                                    "\"type\":\"view\","+
                                    "\"name\":\""+child.getName()+"\","+
                                    "\"url\":\""+child.getUrl()+"\"}";
                        }
                        no++;
                    }
                }
                strJson = strJson+"]";
            }else if(menu.getType().equals("click")){
                strJson = strJson +
                        "\"type\":\"click\","+
                        "\"name\":\""+menu.getName()+"\","+
                        "\"key\":\""+menu.getKey()+"\"";
            }else{
                strJson = strJson +
                        "\"type\":\"view\","+
                        "\"name\":\""+menu.getName()+"\","+
                        "\"url\":\""+menu.getUrl()+"\"";
            }
            if(i==menus.size()-1){
                strJson = strJson + "}";
            }else{
                strJson = strJson + "},";
            }
        }
        strJson = strJson + "]}";
        return strJson;

    }

    /**
     * 发送请求
     * @param url 请求地址
     * @param json  数据
     * @param contentType  编码
     * @return
     */
    private  String post(String url, String json,String contentType)
    {
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        //HttpClient
        CloseableHttpClient client = httpClientBuilder.build();
        client = (CloseableHttpClient) wrapClient(client);
        HttpPost post = new HttpPost(url);
        try
        {
            StringEntity s = new StringEntity(json,"utf-8");
            if(StringUtils.isBlank(contentType)){
                s.setContentType("application/json");
            }
            s.setContentType(contentType);
            post.setEntity(s);
            HttpResponse res = client.execute(post);
            HttpEntity entity = res.getEntity();
            String str= EntityUtils.toString(entity, "utf-8");
            return str;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }


    private static HttpClient wrapClient(HttpClient base) {
        try {
            SSLContext ctx = SSLContext.getInstance("TLSv1");
            X509TrustManager tm = new X509TrustManager() {
                public void checkClientTrusted(X509Certificate[] xcs,
                                               String string) throws CertificateException {
                }

                public void checkServerTrusted(X509Certificate[] xcs,
                                               String string) throws CertificateException {
                }

                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };
            ctx.init(null, new TrustManager[] { tm }, null);
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(ctx, new String[] { "TLSv1" }, null,
                    SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
            CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
            return httpclient;

        } catch (Exception ex) {
            return null;
        }
    }


    private class CharsetHandler implements ResponseHandler<String> {
        private String charset;

        public CharsetHandler(String charset) {
            this.charset = charset;
        }

        public String handleResponse(HttpResponse response)
                throws ClientProtocolException, IOException {
            StatusLine statusLine = response.getStatusLine();
            if (statusLine.getStatusCode() >= 300) {
                throw new HttpResponseException(statusLine.getStatusCode(),
                        statusLine.getReasonPhrase());
            }
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                if (!StringUtils.isBlank(charset)) {
                    return EntityUtils.toString(entity, charset);
                } else {
                    return EntityUtils.toString(entity);
                }
            } else {
                return null;
            }
        }
    }

}
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
5个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
梦
3年前
微信小程序new Date()转换时间异常问题
微信小程序苹果手机页面上显示时间异常,安卓机正常问题image(https://imghelloworld.osscnbeijing.aliyuncs.com/imgs/b691e1230e2f15efbd81fe11ef734d4f.png)错误代码vardate'2021030617:00:00'vardateT
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
3年前
Docker 部署SpringBoot项目不香吗?
  公众号改版后文章乱序推荐,希望你可以点击上方“Java进阶架构师”,点击右上角,将我们设为★“星标”!这样才不会错过每日进阶架构文章呀。  !(http://dingyue.ws.126.net/2020/0920/b00fbfc7j00qgy5xy002kd200qo00hsg00it00cj.jpg)  2
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
11个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这