JAVA微信公众号网页开发——生成自定义微信菜单(携带参数)
官网接口地址:https://developers.weixin.qq.com/doc/offiaccount/Custom_Menus/Creating_Custom-Defined_Menu.html
//创建一个微信菜单实体类
WeixinMenu.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | 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 ; } } } } |
如果出现 {"errcode":40001,"errmsg":"invalid credential, hint:。。。
可能是需要把当前的ip地址加入到公众号的后台 IP白名单中
-----------------------有任何问题可以在评论区评论,也可以私信我,我看到的话会进行回复,欢迎大家指教------------------------
(蓝奏云官网有些地址失效了,需要把请求地址lanzous改成lanzoux才可以)
分类:
JAVA
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了