node微信公众号开发--设置自定义菜单

var request = require("request");
const querystring = require("querystring");
request({
	timeout: 5000, // 设置超时
	method: 'GET', //请求方式
	url: 'https://api.weixin.qq.com/cgi-bin/token', //url
	qs: { //参数,注意get和post的参数设置不一样  
		grant_type: "client_credential",
		appid: "xxxxx",
		secret: "xxxxxxxx"
	}

}, function(error, response, body) {
	if(!error && response.statusCode == 200) {
		console.log(body);
		body = JSON.parse(body);
		if(body.access_token) {
			var token = body.access_token;
			setItem(token);
		}
	} else {
		console.log("error");
	}
});


var btn = {
			"button": [{
					"type": "click",
					"name": "今日歌曲",
					"key": "V1001_TODAY_MUSIC"
				},
				{
					"name": "菜单",
					"sub_button": [{
							"type": "view",
							"name": "搜索",
							"url": "http://www.soso.com/"
					},
					{
							"type": "click",
							"name": "赞一下我们",
							"key": "V1001_GOOD"
						}
					]
				}
			]
		}

function setItem(token) { 
	request({
		timeout: 5000, // 设置超时
		method: 'POST', //请求方式
		url: 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token=' + token, //url
		headers: {
             'Content-Type': 'application/x-www-form-urlencoded'
        },
		form:  JSON.stringify(btn)
		}, function(error, response, body) {
		if(!error && response.statusCode == 200) {
			console.log(body);
				
		} else {
			console.log("error");
		}
	});
}

  网上有些人说,测试账号,不能设置,实际上是可以设置的。。

官方给的配置json是有点问题的,需要删除掉

            {
"type":"miniprogram",
"name":"wxa",
"url":"http://mp.weixin.qq.com",
"appid":"wx286b93c14bbf93aa",
"pagepath":"pages/lunar/index"
},

猜你喜欢

转载自www.cnblogs.com/muamaker/p/9077237.html