当前位置:网站首页>微信公众号之底部菜单
微信公众号之底部菜单
2022-08-04 11:19:00 【youhebuke225】
专栏目录请点击
简介
- 我们可以观看官网的介绍 点击
- 他的核心是,我们需要调微信的api来进行创建菜单,且菜单的参数,在官网的例子中已经给出
- 调用的接口为 https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN
- 他是一个
post请求,参数放在请求体中(body) - 参数如下
{
"button":[
{
"type":"click",
"name":"今日歌曲",
"key":"V1001_TODAY_MUSIC"
},
{
"name":"菜单",
"sub_button":[
{
"type":"view",
"name":"搜索",
"url":"http://www.soso.com/"
},
{
"type":"miniprogram",
"name":"wxa",
"url":"http://mp.weixin.qq.com",
"appid":"wx286b93c14bbf93aa",
"pagepath":"pages/lunar/index"
},
{
"type":"click",
"name":"赞一下我们",
"key":"V1001_GOOD"
}]
}]
}
在这个参数中,他既有一级菜单,也有二级菜单,并且也有不同功能的按钮
代码
- 我们使用一个文件来保存我们定义的按钮,我们就把它叫做
menu.js,把他放在wechat文件夹下 - 写上如下代码,我们先写一个简单的菜单
module.exports = {
"button": [
{
"type": "click",
"name": "今日歌曲",
"key": "V1001_TODAY_MUSIC"
},
{
"name": "菜单",
"sub_button": [
{
"type": "view",
"name": "搜索",
"url": "http://www.soso.com/"
}]
}]
}
- 我们可以观看我们调用的接口,我们会发现他需要
ACCESS_TOKEN,所以我们把请求的放到了获取ACCESS_TOKEN的WeChat类中 - 我们在
libs文件夹下新建一个api.js的文件,专门用来管理接口,并写上如下的内容
const prefix = "htps://api.weixin.qq.com/cgi-bin/"
module.exports = {
menu:{
// 创建菜单 https://developers.weixin.qq.com/doc/offiaccount/Custom_Menus/Creating_Custom-Defined_Menu.html
create:prefix + "menu/create?",
// 删除菜单 https://developers.weixin.qq.com/doc/offiaccount/Custom_Menus/Deleting_Custom-Defined_Menu.html
delete:prefix +'menu/delete?',
// 获取菜单 https://developers.weixin.qq.com/doc/offiaccount/Custom_Menus/Querying_Custom_Menus.html
get:prefix +'menu/get?',
// 个性化菜单相关
}
}
- 他包括,创建菜单的方法,删除菜单的方法等,我们先写一个创建和删除的方法,并进行测试
class WeChat {
// ...
createMenu(body) {
// 创建菜单
return new Promise(async (resolve, reject) => {
try {
const {
access_token } = await this.fetchAccessToken()
const url = `${
api.menu.create}access_token=${
access_token}`
const res = await rp({
method: "POST", json: true, url, body }) // 请求数据
resolve(res)
} catch (error) {
reject(`createMenu方法处理问题${
error}`);
}
})
}
deleteMenu() {
return new Promise(async (resolve, reject) => {
try {
const {
access_token } = await this.fetchAccessToken()
const url = `${
api.menu.delete}access_token=${
access_token}`
const res = rp({
method: "GET", json: true, url })
resolve(res)
} catch (error) {
reject(`deleteMenu方法处理问题${
error}`)
}
})
}
}
- 上面的方法是菜单删除和创建的函数,我们测试一下这两个函数,于是我们在当前文件下写一个
立即执行函数
// 进行测试
(async () => {
const weChatApi = new WeChat()
let data = await weChatApi.deleteMenu()
console.log(data)
data = await weChatApi.createMenu(menu)
console.log(data)
})()
- 执行当前的js文件,如果没有报错,那么就是创建成功了,过两三分钟,可以看一下我们的公众号,发现底部已经有按钮了

其他的按钮类型
- 通过微信官网,我们可以发现微信提供的按钮类型有多种 点击,一共有12种菜单
- 同时,我们一定注意不同中的按钮的兼容性
边栏推荐
猜你喜欢

【Qt】解决 “由于找不到Qt5Cored.dll,无法继续执行代码”(亲测有效)

什么是 DevOps?看这一篇就够了!

深度强化学习与APS的一些感想

JUC (1) threads and processes, concurrency and parallelism, thread state, locks, producers and consumers

多行函数;group_by分组;having分组后筛选;单表查询总结

Leetcode刷题——构造二叉树(105. 从前序与中序遍历序列构造二叉树、106. 从中序与后序遍历序列构造二叉树)

数据库对象-视图;存储过程

职责链模式(responsibilitychain)

Digital management insight into retail and e-commerce operations - retail password

Leetcode刷题——543. 二叉树的直径、617. 合并二叉树(递归解决)
随机推荐
MySQL 45 讲 | 11 怎么给字符串字段加索引?
中介者模式(Mediator)
【LeetCode】653. 两数之和 IV - 输入 BST
喂,你知道节流是什么吗?
你知道吗?那些专属于代码的浪漫~
【Idea series】idea configuration
Win11文件类型怎么改?Win11修改文件后缀的方法
Graphical Hands-on Tutorial--ESP32 OTA Over-the-Air Upgrade (VSCODE+IDF)
MATLAB程序设计与应用 3.1 特殊矩阵
Leetcode brush - structure binary tree (105. Once upon a time sequence and the sequence structure binary tree traversal sequence, 106. From the sequence with the sequence structure binary tree travers
【LeetCode】899.有序队列
北京大学,新迎3位副校长!其中一人为中科院院士!
数字知识库及考学一体化平台
揭秘交换机市场内幕,“准工业级”猫腻你中招了吗?
Doing Homework HDU - 1074
【Qt】解决 “由于找不到Qt5Cored.dll,无法继续执行代码”(亲测有效)
datax oracle to oracle incremental synchronization
vector中函数emplace_back的实现原理
Difference between ArrayList and LinkedList
BOSS 直聘回应女大学生连遭两次性骚扰:高度重视求职者安全,可通过 App 等举报