溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

創(chuàng)建微信公眾號自定義菜單欄的示例

發(fā)布時間:2021-01-20 14:15:44 來源:億速云 閱讀:184 作者:小新 欄目:移動開發(fā)

小編給大家分享一下創(chuàng)建微信公眾號自定義菜單欄的示例,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

剛開始開通微信公眾號的時候是抱著好奇的心態(tài),其實我那時也不是很了解,經(jīng)過查閱分析,前端是通過自定義菜單手動配置的,菜單不是通過后臺生成的,后面想要獲取事件信息的時候出現(xiàn)了點問題,所以我重新研究了下相關(guān)的文檔,分享給大家

其實生成菜單非常簡單,直接上代碼:

創(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ù)一定不要有,別問我怎么知道的。

以上是“創(chuàng)建微信公眾號自定義菜單欄的示例”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI