刚开始开通微信公众号的时候是抱着好奇的心态,其实我那时也不是很了解,经过查阅分析,前端是通过自定义菜单手动配置的,菜单不是通过后台生成的,后面想要获取事件信息的时候出现了点问题,所以我重新研究了下相关的文档,分享给大家
其实生成菜单非常简单,直接上代码:
官方文档地址:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141013
创建几个实体类,用来生成创建菜单必须的json:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
public class ViewEntity {
public String type;
public String name;
public String url;
public ViewEntity() {
super();
}
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;
}
}
|
登录后复制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
public class MenuEntity {
public String name;
public List<Object> sub_button;
public MenuEntity() {
super();
}
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 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;
public class WxCreateButtonUtils {
public static String getWxCreateButtonJson() {
JSONObject jsonObject = new JSONObject();
List<Object> lobjs = new ArrayList<>();
List<Object> firstvl = new ArrayList<>();
ViewEntity infoEntity = new ViewEntity("view", "个人信息", "http:
firstvl.add(infoEntity);
MenuEntity thirdEntity = new MenuEntity("我的",firstvl);
lobjs.add(thirdEntity);
jsonObject.put("button", lobjs);
System.out.println(jsonObject);
return JSON.toJSONString(jsonObject);
}
}
|
登录后复制
Controller:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | @ApiOperation("微信公众号创建菜单")
@ApiImplicitParams({
})
@PostMapping("/WxCreateButton")
public JsonResult wxCreateButton() {
try {
wxInfoService.createButton();
return JsonResult.success("创建成功");
} catch (Exception e) {
LOG.error(e.getMessage());
return JsonResult.failMsg(e.getMessage());
}
}
|
登录后复制
service:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | @Override
public void createButton() {
String weixin_jssdk_acceToken_url = "https:
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:
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中文网其他相关文章!