In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Xiaobian shares with you how C#realizes the menu management of WeChat enterprise number. I believe most people still don't know much about it, so share this article for your reference. I hope you will gain a lot after reading this article. Let's find out together!
1. General introduction of menu
The event processing of the menu is shown as follows, including two operations: click and jump. In the future, the enterprise number may add some functions such as scanning code and taking photos like the public number. At present, there are only two.
The official menu definition interface includes the following three operations: menu creation, list acquisition and menu deletion, which are almost the same as the public account operation.
2. Entity class definition and interface definition processing of menu
We define the menu, including defining some of its attributes, including name, type, key,url, and a submenu reference to its own reference, so the menu can be constructed cyclically at multiple levels, although strictly speaking, the menu of the enterprise number is the same as the menu of the public number, three at the first level, five at the second level, and there is no three-level menu.
The UML diagram of the entity class is shown below.
菜单管理的创建操作,官方定义如下所示。
请求说明
Https请求方式: POST
https://qyapi.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN&agentid=1
请求包如下:
{ "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" } ] } ]}
参数说明
参数必须说明access_token是调用接口凭证agentid是企业应用的id,整型。可在应用的设置页面查看button是一级菜单数组,个数应为1~3个sub_button否二级菜单数组,个数应为1~5个type是菜单的响应动作类型,目前有click、view两种类型name是菜单标题,不超过16个字节,子菜单不超过40个字节keyclick类型必须菜单KEY值,用于消息接口推送,不超过128字节urlview类型必须网页链接,员工点击菜单可打开链接,不超过256字节
权限说明
管理员须拥有应用的管理权限,并且应用必须设置在回调模式。
返回结果
{ "errcode":0, "errmsg":"ok"}
根据上面官方的定义语义,我们菜单管理的C#管理接口定义如下所示。
/// /// 企业号菜单管理接口定义 /// public interface ICorpMenuApi { /// /// 获取菜单数据 /// /// 调用接口凭证 /// MenuListJson GetMenu(string accessToken, string agentid); /// /// 创建菜单 /// /// 调用接口凭证 /// 菜单对象 /// CommonResult CreateMenu(string accessToken, MenuListJson menuJson, string agentid); /// /// 删除菜单 /// /// 调用接口凭证 /// CommonResult DeleteMenu(string accessToken, string agentid); }
我们以创建菜单的实现为例来介绍微信企业号菜单的操作,其他的操作类似处理,都是返回一个公共的消息类,方便处理和读取,代码如下所示。
/// /// 创建菜单 /// /// 调用接口凭证 /// 菜单对象 /// public CommonResult CreateMenu(string accessToken, MenuListJson menuJson, string agentid) { var url = string.Format("http://www.php.cn/{0}&agentid={1}", accessToken, agentid); string postData = menuJson.ToJson(); return Helper.GetCorpExecuteResult(url, postData); }3、企业号菜单管理接口的调用和处理效果
调用的代码和效果图如下所示。
private void btnMenuCreate_Click(object sender, EventArgs e) { MenuJson productInfo = new MenuJson("产品介绍", new MenuJson[] { new MenuJson("软件产品介绍", ButtonType.click, "event-software") , new MenuJson("框架源码产品", ButtonType.click, "event-source") , new MenuJson("软件定制开发", ButtonType.click, "event-develop") }); MenuJson frameworkInfo = new MenuJson("框架产品", new MenuJson[] { new MenuJson("Win开发框架", ButtonType.click, "win"), new MenuJson("WCF开发框架", ButtonType.click, "wcf"), new MenuJson("混合式框架", ButtonType.click, "mix"), new MenuJson("Web开发框架", ButtonType.click, "web") ,new MenuJson("代码生成工具", ButtonType.click, "database2sharp") }); MenuJson relatedInfo = new MenuJson("相关链接", new MenuJson[] { new MenuJson("公司介绍", ButtonType.click, "event_company"), new MenuJson("官方网站", ButtonType.view, "http://www.php.cn/"), new MenuJson("联系我们", ButtonType.click, "event_contact"), new MenuJson("应答系统", ButtonType.click, "set-1"), new MenuJson("发邮件", ButtonType.view, "http://www.php.cn/") }); MenuListJson menuJson = new MenuListJson(); menuJson.button.AddRange(new MenuJson[] { productInfo, frameworkInfo, relatedInfo }); //Console.WriteLine(menuJson.ToJson()); if (MessageUtil.ShowYesNoAndWarning("您确认要创建菜单吗") == System.Windows.Forms.DialogResult.Yes) { ICorpMenuApi bll = new CorpMenuApi(); CommonResult result = bll.CreateMenu(token, menuJson, agentid); Console.WriteLine("创建菜单:" + (result.Success ? "成功" : "失败:" + result.ErrorMessage)); } } private void btnMenuGet_Click(object sender, EventArgs e) { ICorpMenuApi bll = new CorpMenuApi(); MenuListJson menu = bll.GetMenu(token, agentid); if (menu != null) { Console.WriteLine(menu.ToJson()); } }
调用代码的测试输出如下所示。
以上是"C#如何实现微信企业号的菜单管理 "这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.