導讀微信(WeChat)是騰訊公司于2011年1月21日推出的一個為智能終端提供即時通訊服務的免費應用程序,由張小龍所帶領的騰訊廣州研發(fā)中心產(chǎn)品團隊打造 [2] 。微信支持跨通信運營商、跨操作系統(tǒng)平臺... 微信(WeChat)是騰訊公司于2011年1月21日推出的一個為智能終端提供即時通訊服務的免費應用程序,由張小龍所帶領的騰訊廣州研發(fā)中心產(chǎn)品團隊打造 [2] 。微信支持跨通信運營商、跨操作系統(tǒng)平臺通過網(wǎng)絡快速發(fā)送免費(需消耗少量網(wǎng)絡流量)語音短信、視頻、圖片和文字,同時,也可以使用通過共享流媒體內(nèi)容的資料和基于位置的社交插件“搖一搖”、“漂流瓶”、“朋友圈”、”公眾平臺“、”語音記事本“等服務插件。 剛開始開通微信公眾號的時候是抱著好奇的心態(tài),其實我那時也不是很了解,經(jīng)過查閱分析,前端是通過自定義菜單手動配置的,菜單不是通過后臺生成的,后面想要獲取事件信息的時候出現(xiàn)了點問題,所以我重新研究了下相關的文檔,分享給大家其實生成菜單非常簡單,直接上代碼: 官方文檔地址:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141013 創(chuàng)建幾個實體類,用來生成創(chuàng)建菜單必須的json: /** * 微信公眾號菜單 view 模式 * * @author cdj * @date 2018年7月26日 下午2:02:57 */ public class ViewEntity { public String type; public String name; public String url; public ViewEntity() { super(); // TODO Auto-generated constructor stub } public ViewEntity(String type, String name, String url) { super(); this.type = type; this.name = name; this.url = url; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } } /** * 微信公眾號多級菜單按鈕實體類 * * @author cdj * @date 2018年7月26日 下午2:08:40 */ public class MenuEntity { public String name; /**下級菜單按鈕 集合 */ public List<Object> sub_button; public MenuEntity() { super(); // TODO Auto-generated constructor stub } public MenuEntity(String name, List<Object> sub_button) { super(); this.name = name; this.sub_button = sub_button; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Object> getSub_button() { return sub_button; } public void setSub_button(List<Object> sub_button) { this.sub_button = sub_button; } } 寫一個工具類直接獲json,zl import java.util.ArrayList; import java.util.List; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.modou.park.entity.wechat.MenuEntity; import com.modou.park.entity.wechat.ViewEntity; /** * 微信公眾號獲取button創(chuàng)建 * @author cdj * @date 2018年7月26日 下午2:22:02 */ public class WxCreateButtonUtils { /** * 獲得微信公眾號菜單 * @return */ public static String getWxCreateButtonJson() { JSONObject jsonObject = new JSONObject(); List<Object> lobjs = new ArrayList<>(); List<Object> firstvl = new ArrayList<>(); ViewEntity infoEntity = new ViewEntity("view", "個人信息", "http://www.xxx.com/xxxxxxxxx.html");//寫自己的要跳轉(zhuǎn)的url firstvl.add(infoEntity); MenuEntity thirdEntity = new MenuEntity("我的",firstvl); lobjs.add(thirdEntity); jsonObject.put("button", lobjs); System.out.println(jsonObject); return JSON.toJSONString(jsonObject); } } Controller: @ApiOperation("微信公眾號創(chuàng)建菜單") @ApiImplicitParams({ }) @PostMapping("/WxCreateButton") public JsonResult wxCreateButton() { try { wxInfoService.createButton(); return JsonResult.success("創(chuàng)建成功"); } catch (Exception e) { // TODO: handle exception LOG.error(e.getMessage()); return JsonResult.failMsg(e.getMessage()); } } service: @Override public void createButton() { //String accessToken = wxPublicAccessTokenUtils.getAccessToken(); //String createButton_Url = UserInfoUtil.getCreateButton_Url(accessToken); String weixin_jssdk_acceToken_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s"; String jssdkAcceTokenUrl = String.format(weixin_jssdk_acceToken_url, WxPublicProperties.APPID, WxPublicProperties.APPSCREAT); String accesstoken = HttpsUtil.httpsRequestToString(jssdkAcceTokenUrl, "GET", null); WxToken accToken = JSONObject.parseObject(accesstoken, WxToken.class); String accessToken = accToken.getAccessToken(); String createurl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=%s"; String createButton_Url = String.format(createurl, accessToken); String buttonJson = WxCreateButtonUtils.getWxCreateButtonJson(); String httpsRequestToString = HttpsUtil.httpsRequestToString(createButton_Url, "POST", buttonJson); System.out.println(httpsRequestToString); } 成功反饋:{"errcode":0,"errmsg":"ok"}apache php mysql json 里面的type 有多種情況, 例如 click 點擊(可以與click事件綁定),miniprogram (小程序:需要參數(shù)appid ,pagepath, url 等) 不同的內(nèi)容參數(shù)不同,效果也不同,可以看相應的文檔了解一下。 測試時會出現(xiàn)很多的小bug , 可以通過百度錯誤碼,網(wǎng)上有很多的回復 ; 總結(jié):新建菜單不難,但一定要細心,內(nèi)容的格式一定要正確,json的key一定不能錯,不能有的參數(shù)一定不要有,別問我怎么知道的。 相關文章: php實現(xiàn)微信公眾平臺賬號自定義菜單類,公眾賬號 相關視頻: 以上就是怎么創(chuàng)建微信公眾號自定義菜單欄?這里給出了權(quán)威解答的詳細內(nèi)容,更多請關注php中文網(wǎng)其它相關文章! 微信提供公眾平臺、朋友圈、消息推送等功能,用戶可以通過“搖一搖”、“搜索號碼”、“附近的人”、掃二維碼方式添加好友和關注公眾平臺,同時微信將內(nèi)容分享給好友以及將用戶看到的精彩內(nèi)容分享到微信朋友圈。 |
溫馨提示:喜歡本站的話,請收藏一下本站!