首頁  >  文章  >  微信小程式  >  怎麼建立微信公眾號自訂選單列?這裡給了權威解答

怎麼建立微信公眾號自訂選單列?這裡給了權威解答

php是最好的语言
php是最好的语言原創
2018-07-27 17:18:076003瀏覽

剛開始開通微信公眾號的時候是抱著好奇的心態,其實我那時也不是很了解,經過查閱分析,前端是通過自定義菜單手動配置的,菜單不是通過後台生成的,後面想要取得事件資訊的時候出現了點問題,所以我重新研究了下相關的文檔,分享給大家

其實產生選單非常簡單,直接上程式碼:

官方文件位址: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實作微信公眾平台帳號自訂選單類,公眾帳號

相關影片:

自訂選單(1)-PHP微信公眾平台開發影片教學

#

以上是怎麼建立微信公眾號自訂選單列?這裡給了權威解答的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn