Nodejs 微信开发 自定义菜单

var map = require('../router-config').getMapping()
  , wechat = require('wechat')
  , fs = require('fs');

var API = wechat.API
  , appid = 'xxx'
  , secret = 'xxxx';
var api = new API(appid, secret);

var router = express.Router();
  router.use('/', wechat('wechatToken', function(req, res, next) {
    var menu = fs.readFileSync('./node-app/config/wechat-menu.json');
    if(menu) {
      menu = JSON.parse(menu);
    }
    api.createMenu(menu, function(err, result){});

    // 微信输入信息都在req.weixin上
    var message = req.weixin;
    var msgType = message.MsgType;
    if(msgType == 'event'){
      var eventType = message.Event;
      if(eventType) {
        eventType = eventType.toLowerCase();
      }
      if(eventType == 'subscribe') {
          // 新用户关注/订阅事件
          subscribeEvent(res);
      } else if(eventType == 'click') {
        clickEvent(message, res);
      } else {
        notFoundEvent(eventType, res);
      }
    }

 Nodejs 微信开发 自定义菜单

wechat-menu.json

{
  "button": [
    {
      "name": "我的账号",
      "sub_button": [
        {
          "type": "click",
          "name": "我的帐户",
          "key": "V1001_MY_ACCOUNT"
        },
        {
          "type": "click",
          "name": "已投项目",
          "key": "V1002_BID_PROJECTS"
        },
        {
          "type": "click",
          "name": "回款计划",
          "key": "V1003_RETURN_PLAN"
        },
        {
          "type": "click",
          "name": "交易明细",
          "key": "V1004_TRANS_DETAIL"
        },
        {
          "type": "click",
          "name": "注册/绑定",
          "key": "V1005_REGISTER_BIND"
        }
      ]
    },
    {
      "type": "view",
      "name": "马上投资",
      "url": "http://qiaole.sinaapp.com/"
    },
    {
      "name": "送钱活动",
      "sub_button": [
        {
          "type": "view",
          "name": "注册送红包",
          "url": "http://qiaole.sinaapp.com/"
        },
        {
          "type": "click",
          "name": "邀请好友一起赚钱",
          "key": "V1001_GOOD"
        },
        {
          "type": "view",
          "name": "加入我们",
          "url": "http://qiaole.sinaapp.com/"
        },
        {
          "type": "view",
          "name": "APP下载",
          "url": "http://qiaole.sinaapp.com/"
        }
      ]
    }
  ]
}

 Nodejs 微信开发 自定义菜单

Nodejs 微信开发 自定义菜单

相关推荐