微信小程序> 微信公众号开发创建菜单

微信公众号开发创建菜单

浏览量:521 时间: 来源:gavin.liao

微信公众号开发文章目录

1.微信公众号开发 - 环境搭建
2.微信公众号开发 - 配置表设计以及接入公众号接口开发
3.微信公众号开发 - token获取(保证同一时间段内只请求一次)
4.微信公众号开发 - 菜单按钮bean封装
5.微信公众号开发 - 创建菜单
6.微信公众号开发 - 事件处理和回复消息
7.微信公众号开发 - 发送Emoji表情

项目完整代码请访问github:https://github.com/liaozq0426/wx.git

在上篇文章中完成了菜单、按钮bean的封装,现在来实现微信公众号菜单的创建

向wx_cfg配置表中插入若干菜单记录

INSERT INTO `wx_cfg` (`id`, `type`, `name`, `value`, `parent_id`, `sort`, `platform`, `wx_type`, `enabled`, `desc`) VALUES ('4', 'app_url', 'appUrl', 'http://www.gavin.com', NULL, NULL, 'gavin', 'service', '1', '公众号域名');INSERT INTO `wx_cfg` (`id`, `type`, `name`, `value`, `parent_id`, `sort`, `platform`, `wx_type`, `enabled`, `desc`) VALUES ('5', 'menu', 'view1', '/transfer/index.html', NULL, '1', 'gavin', 'service', '1', '学习资料');INSERT INTO `wx_cfg` (`id`, `type`, `name`, `value`, `parent_id`, `sort`, `platform`, `wx_type`, `enabled`, `desc`) VALUES ('6', 'menu', 'view2', '/transfer/index.html', NULL, '2', 'gavin', 'service', '1', '在线题库');INSERT INTO `wx_cfg` (`id`, `type`, `name`, `value`, `parent_id`, `sort`, `platform`, `wx_type`, `enabled`, `desc`) VALUES ('7', 'menu', 'click1', '/transfer/index.html', NULL, '3', 'gavin', 'service', '1', '关于');INSERT INTO `wx_cfg` (`id`, `type`, `name`, `value`, `parent_id`, `sort`, `platform`, `wx_type`, `enabled`, `desc`) VALUES ('8', 'menu', 'view11', '/transfer/index.html', '5', '1', 'gavin', 'service', '1', 'JAVA');INSERT INTO `wx_cfg` (`id`, `type`, `name`, `value`, `parent_id`, `sort`, `platform`, `wx_type`, `enabled`, `desc`) VALUES ('9', 'menu', 'view12', '/transfer/index.html', '5', '2', 'gavin', 'service', '1', 'MySQL');

编写代码

创建WxMenuService接口

package com.gavin.service;import com.gavin.pojo.Menu;public interface WxMenuService {public Menu makeMenu(String platform) throws Exception;}

创建实现类WxMenuServiceImpl,代码如下

package com.gavin.service.impl;import java.util.ArrayList;import java.util.List;import java.util.Map;import org.apache.commons.lang3.StringUtils;import org.apache.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.gavin.cfg.WxCfgEnum;import com.gavin.pojo.AccessToken;import com.gavin.pojo.Button;import com.gavin.pojo.ClickButton;import com.gavin.pojo.ComplexButton;import com.gavin.pojo.Menu;import com.gavin.pojo.ViewButton;import com.gavin.pojo.WxApiCfg;import com.gavin.pojo.WxCfg;import com.gavin.service.WxCfgService;import com.gavin.service.WxMenuService;import com.gavin.service.WxTokenService;import com.gavin.util.EncryptionUtil;import com.gavin.util.WxUtil;import com.google.gson.Gson;@Servicepublic class WxMenuServiceImpl implements WxMenuService{private Logger logger = Logger.getLogger(this.getClass());@Autowiredprivate WxTokenService wxTokenService;@Autowiredprivate WxCfgService wxCfgService;/** * @title 根据平台创建微信公众号菜单 * @author gavin * @date 2019年5月23日 * @param platform * @return * @throws Exception */@Overridepublic Menu makeMenu(String platform) throws Exception {// 查询一级菜单System.out.println(platform);WxCfg parentParam = new WxCfg();parentParam.setType(WxCfgEnum.MENU.getType());parentParam.setEnabled(true);parentParam.setPlatform(platform);parentParam.setOrderBy("sort asc");ListWxCfg cfgList = this.wxCfgService.select(parentParam);if (cfgList.size()  0) {Menu menu = new Menu();ListButton buttons = new ArrayListButton();for (WxCfg cfg : cfgList) {WxCfg subParam = new WxCfg();subParam.setType(WxCfgEnum.MENU.getType());subParam.setParentId(cfg.getId());subParam.setEnabled(true);subParam.setPlatform(platform);// 查询二级菜单ListWxCfg subCfgList = this.wxCfgService.select(subParam);if (subCfgList.size()  0) {// 构建多级按钮ListButton subButtons = new ArrayListButton();for (WxCfg subCfg : subCfgList) {Button sub = createButton(subCfg);subButtons.add(sub);}ComplexButton complexButton = new ComplexButton();complexButton.setName(cfg.getDesc());complexButton.setSub_button(subButtons.toArray(new Button[subButtons.size()]));buttons.add(complexButton);} else {Button button = createButton(cfg
            
            

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎