3.2. 订阅号后台自定义菜单
- 若是订阅号后台设置为开发者模式,在订阅号正式发布以前调用一次便可
- 若是你的公众号没有认证是没有这个接口权限的,咱们须要用微信公众号平台提供的测试号
- 代码
// 自定义微信菜单
const express = require("express")
const request = require("request")
const app = express()
app.use("/menu", (req, res, next) => {
//1 获取access_token
let appid = "wx80aec7a5cef167";
let appsecret = "3fc079b274881b813436a078d3810";
let tokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + appsecret;
request(tokenUrl, (err, response, body) => {
//body是字符串
let json = JSON.parse(body);
console.log(json.access_token);
let data = {
"button": [
{
"type": "click",
"name": "今日歌曲",
"key": "V1001_TODAY_MUSIC"
},
{
"type": "scancode_push",
"name": "扫码",
"key": "rselfmenu_0_1"
},
{
"name": "菜单",
"sub_button": [
{
"type": "view",
"name": "搜索",
"url": "http://www.soso.com/"
},
{
"type": "view",
"name": "视频",
"url": "http://v.qq.com/"
},
{
"type": "click",
"name": "赞一下咱们",
"key": "V1001_GOOD"
}]
}
]
}
request({
method: "post",
url: "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + json.access_token,
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json"
}
}, (err, response, body) => {
if(err){
res.send(err)
}else{
res.send(body)
}
})
})
})
// 启动server
const server = app.listen(3002, function () {
const host = server.address().address;
const port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
免责说明
- 本博客中的文章摘自网上的众多博客,仅做为本身知识的补充和整理,并分享给其余须要的coder,不会用于商用。
- 由于不少博客的地址看完没有及时作保存,因此不少不会在这里标明出处,很是感谢各位大牛的分享,也但愿你们理解。