How to create a custom menu bar for WeChat official account? Here is the authoritative answer

php是最好的语言
Release: 2018-07-27 17:18:07
Original
6052 people have browsed it

When I first opened the WeChat public account, I was curious. In fact, I didn’t know much about it at the time. After consulting and analyzing, I found that the front end was manually configured through a custom menu, and the menu was not generated through the backend. I will think about it later. There was a problem when getting event information, so I re-study the relevant documents and share them with you

In fact, generating the menu is very simple, just enter the code:

Official Document address: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141013

Create several entity classes to generate the json necessary to create the menu:

/**
 * 微信公众号菜单 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;
	}
}
Copy after login
/**
 * 微信公众号多级菜单按钮实体类
 * 
 * @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;
	}

}
Copy after login

Write a tool class to directly obtain 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);
	}
}
Copy after login

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());
		}
	}
Copy after login

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);
	}
Copy after login

Successful feedback: {"errcode":0,"errmsg": "ok"}apache php mysql

#json There are many types of types, such as click (can be bound to the click event), miniprogram (mini program: requires parameters appid, pagepath, url, etc.) different Different content parameters have different effects. You can check the corresponding documents to learn more.

There will be many small bugs during testing. You can use Baidu error codes. There are many replies on the Internet;

Summary: It is not difficult to create a new menu, but you must be careful and the format of the content must be Correct, the key of json must not be wrong, and there must be no parameters that cannot be included. Don't ask me how I know.

Related articles:

WeChat public platform develops QR codes and creates menus

php implements WeChat public platform account custom menu classes , Public account

Related videos:

Custom menu (1)-PHP WeChat public platform development video tutorial

The above is the detailed content of How to create a custom menu bar for WeChat official account? Here is the authoritative answer. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template