公众号开发文档:https://developers.weixin.qq.com/doc/offiaccount/Custom_Menus/Creating_Custom-Defined_Menu.html
本例子使用的接口是:
https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN
拿到以企业认证好的公众号,在基本配置里获取,添加ip 白名单
本例子是从数据库中读取的公众号信息
流程:
1.按公众号id 查询公众号主菜单
2.按主菜单,查询子菜单
3.拼装数据,重用性小,没有进行再封装
public AjaxResult syncPubMenu(Integer pubId) {
WeChatPubVo pubVo = pubService.selectById(pubId);
if (StringUtils.isNull(pubVo)) {
return AjaxResult.error("公众号不存在");
}
if (!pubVo.getStatus().equals(BaseConstant.ONE)) {
return AjaxResult.error("公众号状态禁用,请启用后再同步");
}
WeChatPubMenuDto dto = new WeChatPubMenuDto();
dto.setPubId(pubVo.getId());
dto.setPid(BaseConstant.ZERO);
dto.setStatus(BaseConstant.ONE);
List<WeChatPubMenuVo> pubMainMenuVoList = service.selectList(dto);
if (pubMainMenuVoList.size() == BaseConstant.ZERO) {
return AjaxResult.error("查询公众号菜单为空");
}
List<Map<String, Object>> mapList = new ArrayList<>();
for (WeChatPubMenuVo vo : pubMainMenuVoList) {
WeChatPubMenuDto menuDto = new WeChatPubMenuDto();
menuDto.setPid(vo.getId());
menuDto.setPubId(pubVo.getId());
menuDto.setStatus(BaseConstant.ONE);
List<WeChatPubMenuVo> menuVoList = service.selectList(menuDto);
List<Map<String, Object>> mapSubList = new ArrayList<>();
for (WeChatPubMenuVo subVo : menuVoList) {
Map<String, Object> subMap = new HashMap<>();
subMap.put("type", subVo.getType());
subMap.put("name", subVo.getName());
if (subVo.getType().equals("view")) {
subMap.put("url", subVo.getTypeContent());
} else if (subVo.getType().equals("click")) {
subMap.put("key",subVo.getTypeContent());
} else if (subVo.getType().equals("miniprogram")) {
subMap.put("url",subVo.getTypeContent());
subMap.put("appid",subVo.getAppid());
subMap.put("pagepage",subVo.getPagepath());
}
mapSubList.add(subMap);
}
/**
* 如果主菜单有子菜单,主菜单结构:name, sub_button
* 否则跟普通菜单一样
*/
Map<String, Object> map = new HashMap<>();
map.put("name", vo.getName());
if (mapSubList.size() == BaseConstant.ZERO) {
map.put("type", vo.getType());
if (vo.getType().equals("view")) {
map.put("url", vo.getTypeContent());
} else if (vo.getType().equals("click")) {
map.put("key",vo.getTypeContent());
} else if (vo.getType().equals("miniprogram")) {
map.put("url",vo.getTypeContent());
map.put("appid",vo.getAppid());
map.put("pagepage",vo.getPagepath());
}
} else {
map.put("sub_button", mapSubList);
}
mapList.add(map);
}
Map<String, Object> matchruleMap = new HashMap<>();
matchruleMap.put("tar_id", "2");
Map<String, Object> menuList = new HashMap<>();
menuList.put("button", mapList);
menuList.put("matchrule", matchruleMap);
BasePubTokenEntity tokenEntity = new BasePubTokenEntity();
BeanUtils.copyProperties(pubVo, tokenEntity);
return PubMenu.syncMenu(tokenEntity, menuList);
}
public static AjaxResult syncMenu(BasePubTokenEntity entity, Map<String, Object> menuList) {
String method = "同步公众号菜单 ";
String url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=";
log.info(method + "--------------------start");
try {
log.info(method + " {}", JsonUtils.marshal(menuList));
} catch (Exception e) {
e.printStackTrace();
}
AjaxResult ar = BaseWeChatPub.getAccessToken(entity);
if (ar.isError()) {
log.error(method + ",返回信息{}", ar);
return AjaxResult.error(ar.getCode(), ar.getMsg());
}
url += ar.getData();
try {
String body = OkHttpUtils.getInstance().postJson(url, JsonUtils.toJsonWithGson(menuList));
log.debug(method + "返回信息:{}", body);
if (StringUtils.isEmpty(body)) {
return AjaxResult.error("返回数据为空");
}
return BaseWeChatPub.getResult(body);
} catch (Exception e) {
e.printStackTrace();
log.error(method + e.getMessage());
}
return AjaxResult.error();
}
14:37:08.670 [http-nio-7090-exec-14] INFO c.a.c.e.t.p.PubMenu - [syncMenu,33] - 同步公众号菜单 --------------------start
14:37:08.672 [http-nio-7090-exec-14] INFO c.a.c.e.t.p.PubMenu - [syncMenu,36] - 同步公众号菜单 {
"button" : [ {
"name" : "商城",
"type" : "view",
"url" : "http://test.qq.com/store"
}, {
"name" : "计划",
"sub_button" : [ {
"name" : "计划1",
"type" : "view",
"url" : "http://test.qq.com"
} ]
}, {
"name" : "个人中心",
"type" : "view",
"url" : "http://test.qq.com/login"
} ],
"matchrule" : {
"tar_id" : "2"
}
}
14:37:09.471 [http-nio-7090-exec-14] DEBUG c.a.c.e.t.p.PubMenu - [syncMenu,51] - 同步公众号菜单 返回信息:{"errcode":0,"errmsg":"ok"}