创建自定义菜单

    xiaoxiao2021-12-14  26

    /** * 调用微信后台接口创建自定义菜单 * @param menu 符合微信后台可识别的json格式的按钮 * @throws Exception */ public void createMenu(String menu) throws Exception { // 获取ACCESS_TOKEN String action = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + WXAPIUtils.getAccessToken(); try { URL url = new URL(action); HttpURLConnection http = (HttpURLConnection) url.openConnection(); http.setRequestMethod("POST"); http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); http.setDoOutput(true); http.setDoInput(true); System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒 System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒 http.connect(); OutputStream os = http.getOutputStream(); os.write(menu.getBytes("UTF-8"));// 传入参数 os.flush(); os.close(); InputStream is = http.getInputStream(); int size = is.available(); byte[] jsonBytes = new byte[size]; is.read(jsonBytes); String message = new String(jsonBytes, "UTF-8"); System.out.println("创建菜单------>:" + message); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }

    getAccessToken()方即获取微信接口调用凭据,因为该凭据只有2小时失效,所以需要在程序中做一些处理,能够按时更新该凭据,我是使用了定时器进行一定时间的置空,如果判断为空则调用接口获取凭据

    public class WXAPIUtils { public static String accessToken = null; /** * 开启一个线程 每隔一段时间置空jsapi_ticket */ private static Timer timer; /** * 每隔一段时间获取accessToken */ private static Timer timer2; /** * 获取ACCESS_TOKEN * * @return * @throws Exception */ public static String getAccessToken() throws Exception { if (accessToken != null) { return accessToken; } String accessTokenResult = HttpRequest.sendGet("https://api.weixin.qq.com/cgi-bin/token", "grant_type=client_credential&appid=" + Common.WX_APPID + "&secret=" + Common.WX_SECRET); Gson gson = new Gson(); AccessTokenResult _accessTokenResult = gson.fromJson(accessTokenResult, AccessTokenResult.class); WeChatController.accessToken = _accessTokenResult.getAccess_token(); if (timer2 == null) { timer2 = new Timer(); timer2.schedule(new TimerTask() { @Override public void run() { // TODO Auto-generated method stub System.out.println("accessToken置空"); accessToken = null; } }, 1000 * 60 * 30, 1000 * 60 * 30); 每半小时置空accessToken } System.out.println(_accessTokenResult.getAccess_token()); return _accessTokenResult.getAccess_token(); } }

    需要用到gson

    创建自定义菜单的json数据格式

    String menu = "{\"button\":[{\"name\":\"功能\",\"sub_button\":[{\"type\":\"click\",\"name\":\"问诊\",\"key\":\"enso_wx_question\"},{\"type\":\"click\",\"name\":\"导诊\",\"key\":\"enso_wx_guide\"}]},{\"name\":\"医生\",\"sub_button\":[{\"type\":\"click\",\"name\":\"找医生\",\"key\":\"enso_wx_find_doc\"},{\"type\":\"click\",\"name\":\"我的私人医生\",\"key\":\"enso_wx_my_pri_doc\"},{\"type\":\"click\",\"name\":\"我的医生\",\"key\":\"enso_wx_my_doc\"},{\"type\":\"click\",\"name\":\"社区\",\"key\":\"enso_wx_com\"}]},{\"name\":\"个人\",\"sub_button\":[{\"type\":\"view\",\"name\":\"用户绑定\",\"url\"" + ":\"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx06035c16cc89d490&redirect_uri=http://draven.tunnel.qydev.com/ensoChild/weixin/bindPage&response_type=code&scope=snsapi_userinfo#wechat_redirect\"},{\"type\":\"view\",\"name\":\"test\",\"url\":\"http://draven.tunnel.qydev.com/ensoChild/weixin/test\"},{\"type\":\"click\",\"name\":\"我的病例\",\"key\":\"enso_wx_my_case\"}]}]}"; createMenu(menu);注意需要获取到用户的微信唯一id,需要进行授权(即由微信后台帮用户跳转至我们自己后台的接口,请求中有获取用户信息需要的code,查阅开发手册有详细说明)
    转载请注明原文地址: https://ju.6miu.com/read-962088.html

    最新回复(0)