一日一技:微信开发自定义菜单
2023-03-14 09:32:23 时间
概述
自定义菜单能够帮助公众号丰富界面,让用户更好更快地理解公众号的功能。
文档定义
接口调用请求说明
http请求方式:POST(请使用https协议) https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN
click和view的请求示例
- {
- "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"
- }]
- }]
- }
实现方式
1、定义菜单json文档
- {
- "button": [
- {
- "name": "运营提升",
- "sub_button": [
- {
- "type": "view",
- "name": "免费试用",
- "url": "https://acc.bqool.cn/freetrial"
- },
- {
- "type": "view",
- "name": "新 AI调价",
- "url": "https://www.bqool.cn/products/repricing-central/"
- },
- {
- "type": "view",
- "name": "热 卖家酷",
- "url": "https://www.bqool.cn/bigcentral/"
- },
- {
- "type": "view",
- "name": "热 选品酷",
- "url": "https://www.bqool.cn/products/chrome-extension/"
- },
- {
- "type": "view",
- "name": "更多产品..",
- "url": "https://www.bqool.cn/"
- }
- ]
- },
- {
- "name": "酷仔部落",
- "sub_button": [
- {
- "type": "view",
- "name": "限时福利",
- "url": "https://mp.weixin.qq.com/mp/video?__biz=MzIzMjc1NTY3MQ==&mid=100007240&sn=04973c15f924fc78a78fec50cd75d706&vid=wxv_1500216251938308102&idx=1&vidsn=a27a0d1d9503802f9f9095534d4d2507&fromid=1&xtrack=1&scene=0&subscene=10000&clicktime=1600054770&enterid=1600054770#wechat_redirect"
- },
- {
- "type": "view",
- "name": "成功案例",
- "url": "https://www.bqool.cn/user-stories/"
- },
- {
- "type": "view",
- "name": "教学视频",
- "url": "https://www.bqool.cn/video-tutorial/"
- },
- {
- "type": "view",
- "name": "部落文章",
- "url": "https://mp.weixin.qq.com/mp/homepage?__biz=MzIzMjc1NTY3MQ==&hid=1&sn=db4536a338f0e128ce2e5db089f381e1&scene=18"
- }
- ]
- },
- {
- "name": "权益中心",
- "sub_button": [
- {
- "type": "view",
- "name": "签到抽奖",
- "url": "http://u135069.s.24hwpro.com/web/game/game_id/1220189859"
- },
- {
- "type": "view",
- "name": "商务合作",
- "url": "https://h5.eqxiul.com/ls/EyOfmUXk"
- },
- {
- "type": "view",
- "name": "电商服务",
- "url": "https://www.bqool.cn/seller-services/"
- },
- {
- "type": "view",
- "name": "联系酷仔",
- "url": "https://admin.qidian.qq.com/template/blue/mp/menu/qr-code-jump.html?linkType=0&env=ol&kfuin=2852161981&fid=95&key=71d05b6c5e0a545f0be22ec831f9d7af&cate=1&type=16&ftype=1&_type=wpa&qidian=true"
- }
- ]
- }
- ]
- }
2、读取json文件
- /// <summary>
- /// 自定义菜单
- /// </summary>
- /// <param name="OPENID"></param>
- /// <param name="template_id"></param>
- /// <returns></returns>
- [BQoolException]
- [HttpPost]
- public ApiResult CustomMenus(string CustomMenusPwd)
- {
- if (!string.IsNullOrWhiteSpace(CustomMenusPwd) && CustomMenusPwd.Trim() == MvcTools.GetAppSetting("WeixinCustomMenus"))
- {
- CustomMenusParam param = new CustomMenusParam() { Account = "sys", UpdateUser = "sys" };
- param.Jsonstr = WeiXinHelper.ReadAccess(HttpRuntime.AppDomainAppPath.ToString() + "/App_Data/WeChat/Custom_Menus.json");
- ApiResult result = _weChatAlertsService.CustomMenus(param, AppId, Appsecret);
- return result;
- }
- else
- {
- return new ApiResult() { Success = false, Code = ApiResultCode.InvalidError, ErrorMessage = ApiResultCode.ErrorMessages[ApiResultCode.InvalidError] };
- }
- }
3、post提交数据到微信服务器
- private static Tuple<WxBaseInfo, string, string> CustomMenus(string AppId, string Appsecret, string jsonstr, bool isResh)
- {
- var accessToken = TryGetAccessToken(AppId, Appsecret, isResh);
- string url = APIConfig.Custom_Menus(accessToken);
- string str = APIConfig.CreatePostHttpResponse(url, jsonstr);
- _logger.Debug("自定义菜单\r\n" + "输入:" + url + "\r\n" + jsonstr + "\r\n输出:" + str);
- return new Tuple<WxBaseInfo, string, string>(JsonConvert.DeserializeObject<WxBaseInfo>(str), url, jsonstr);
- }
- public static string WEIXIN_URL = "https://api.weixin.qq.com/";
- public static string Custom_Menus(string token)
- {
- return String.Format(WEIXIN_URL + "cgi-bin/menu/create?access_token={0}", token);
- }
备注:一般情况下,不管是创建或者修改菜单,使用 "cgi-bin/menu/create这个接口即可。
相关文章
- LibreOffice 7.5 发布:漂亮的新应用图标和酷炫功能
- elementary OS 7 发布
- Windows 应用兼容层 Wine 8.1 发布:默认启用“Windows 10”前缀
- 微软正测试新功能:当 Windows 11 有新的小组件可用时会提醒通知
- 解析分布式存储选型和应用九个典型问题
- ClickHouse在自助行为分析场景的实践应用
- Chrome DevTools 远程调试安卓网页的原理
- Uni-app + Vue3 页面如何跳转及传参?
- 微软证实系统还原点会损坏 Windows 11 22H2 版本应用程序
- 巧用 Transition 实现短视频 APP 点赞动画
- 初学者试试,HarmonyOS应用开发者基础认证
- 媒体实测微软 Windows 开发工具包 2023:存在不兼容 HDR 显示器、某些应用无法运行等问题
- 快速了解Navigator API SetAppBadge
- 微软 Windows 11 Dev 预览版 Build 25276 发布,应用兼容问题对话框 UI 改进
- 基于Next.js、Prisma、Postgres和Fastfy构建全栈APP
- 开始菜单搜索框变圆角,微软 Windows 11 Beta 预览版 22621.1095 和 22623.1095 发布
- 2022-2023 十大应用开发趋势
- 观远数据发布业内首部《移动BI白皮书》,深入业务数字化场景重新定义移动BI
- Windows 10 学院:不借助第三方工具如何卸载 Windows 10 预装应用
- 正处高质量发展期,我国大数据产业突破1.3万亿元