剛開始開通微信公眾號的時候是抱著好奇的心態,其實我那時也不是很了解,經過查閱分析,前端是通過自定義菜單手動配置的,菜單不是通過後台生成的,後面想要取得事件資訊的時候出現了點問題,所以我重新研究了下相關的文檔,分享給大家
其實產生選單非常簡單,直接上程式碼:
官方文件位址:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141013
建立幾個實體類,用來產生建立選單必須的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创建 * @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");//写自己的要跳转的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("微信公众号创建菜单") @ApiImplicitParams({ }) @PostMapping("/WxCreateButton") public JsonResult wxCreateButton() { try { wxInfoService.createButton(); return JsonResult.success("创建成功"); } 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 (小程式:需要參數appid ,pagepath, url 等) 不同的內容參數不同,效果也不同,可以看對應的文件了解。
測試時會出現很多的小bug , 可以通過百度錯誤碼,網上有很多的回复;
總結:新建菜單不難,但一定要細心,內容的格式一定要正確,json的key一定不能錯,不能有的參數一定不要有,別問我怎麼知道的。
相關文章:
相關影片:
#以上是怎麼建立微信公眾號自訂選單列?這裡給了權威解答的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Atom編輯器mac版下載
最受歡迎的的開源編輯器