小編給大家分享一下創(chuàng)建微信公眾號自定義菜單欄的示例,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創(chuàng)新互聯(lián)主營秦淮網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,app軟件定制開發(fā),秦淮h5小程序開發(fā)搭建,秦淮網(wǎng)站營銷推廣歡迎秦淮等地區(qū)企業(yè)咨詢
剛開始開通微信公眾號的時(shí)候是抱著好奇的心態(tài),其實(shí)我那時(shí)也不是很了解,經(jīng)過查閱分析,前端是通過自定義菜單手動(dòng)配置的,菜單不是通過后臺(tái)生成的,后面想要獲取事件信息的時(shí)候出現(xiàn)了點(diǎn)問題,所以我重新研究了下相關(guān)的文檔,分享給大家
其實(shí)生成菜單非常簡單,直接上代碼:
創(chuàng)建幾個(gè)實(shí)體類,用來生成創(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; } }
/** * 微信公眾號多級菜單按鈕實(shí)體類 * * @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; } }
寫一個(gè)工具類直接獲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", "個(gè)人信息", "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 點(diǎn)擊(可以與click事件綁定),miniprogram (小程序:需要參數(shù)appid ,pagepath, url 等) 不同的內(nèi)容參數(shù)不同,效果也不同,可以看相應(yīng)的文檔了解一下。
測試時(shí)會(huì)出現(xiàn)很多的小bug , 可以通過百度錯(cuò)誤碼,網(wǎng)上有很多的回復(fù) ;
總結(jié):新建菜單不難,但一定要細(xì)心,內(nèi)容的格式一定要正確,json的key一定不能錯(cuò),不能有的參數(shù)一定不要有,別問我怎么知道的。
以上是“創(chuàng)建微信公眾號自定義菜單欄的示例”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享文章:創(chuàng)建微信公眾號自定義菜單欄的示例
文章分享:http://aaarwkj.com/article10/igcigo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、網(wǎng)站設(shè)計(jì)、移動(dòng)網(wǎng)站建設(shè)、、網(wǎng)站設(shè)計(jì)公司、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)