100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > c# 微信公众号开发之自定义菜单栏

c# 微信公众号开发之自定义菜单栏

时间:2021-11-14 15:18:30

相关推荐

c# 微信公众号开发之自定义菜单栏

在微信公众号开启了第三方服务器之后,很多在微信平台上的配置都需要开发者通过微信提供的API,POST请求,将JSON字符串按格式,告知微信服务器

在这里介绍微信公众号的自定义菜单栏开发

先恭迎我们的TX文档 微信公众号——自定义菜单栏

官方文档里说明了一些需要注意的事项

在这里 我只使用了几个我业务中用到的类型 哈哈哈 本来在考虑要不要加在后台上,后来想想反正他们有事都找自己,直接写了一个get接口 要删要改,调接口

建立了一个菜单栏类

public class MenuModel{public MenuModel(string name = "", string type = "", string url = "", string key = "", string pagepath = "", string appid = ""){Sub_Button = new List<MenuModel>();if (name != ""){Name = name;}if (type != ""){Type = type;}if (url != ""){Url = url;}if (key != ""){Key = key;}if (pagepath != ""){Pagepath = pagepath;}if (appid != ""){Appid = appid;}}public List<MenuModel> Sub_Button { get; set; }/// <summary>/// 名字/// </summary>public string Name { get; set; }/// <summary>/// 类型/// </summary>public string Type { get; set; }/// <summary>/// 地址/// </summary>public string Url { get; set; }/// <summary>/// 菜单KEY值,用于消息接口推送/// </summary>public string Key { get; set; }/// <summary>/// 小程序页面地址/// </summary>public string Pagepath { get; set; }/// <summary>/// 小程序appid/// </summary>public string Appid { get; set; }//通过递归格式化所需json字符串public string ToJson(List<MenuModel> modelList){string result = "";foreach (var a in modelList){if (!a.Sub_Button.Any()){result += "{\"type\":\"" + a.Type + "\",\"name\":\"" + a.Name + "\",";if (a.Url != null){result += "\"url\":\"" + a.Url + "\",";}if (a.Key != null){result += "\"key\":\"" + a.Key + "\",";}if (a.Appid != null){result += "\"appid\":\"" + a.Appid + "\",";}if (a.Pagepath != null){result += "\"pagepath\":\"" + a.Pagepath + "\",";}string lastStr = result.Substring(result.Length - 1, 1);if (lastStr == ","){result = result.Substring(0, result.Length - 1);}result += "},";}else{result += "{\"name\":\"" + a.Name + "\",\"sub_button\":[";result += a.ToJson(a.Sub_Button);result += "},";}if (modelList.Count - 1 == modelList.IndexOf(a)){string lastStr = result.Substring(result.Length - 1, 1);if (lastStr == ","){result = result.Substring(0, result.Length - 1);}result += "]";}}return result;}}

[HttpGet]public HttpResponseMessage Menu(int type){string wxResult = "token异常";OfficialAccountsAccessToken accessToken = _officialAccountsAccessTokenService.GetFirst();string oaToken = "";accessToken.Changed = false;accessToken = WxApi.GetOfficiaAccountsACCESS_TOKEN(accessToken);if (accessToken != null && accessToken.access_token != null){if (accessToken.Changed){_officialAccountsAccessTokenService.Update(accessToken);}oaToken = accessToken.access_token;}//1 add 2 delif (type == 1){//添加自定义菜单string appid = WxConfig.APPID;MenuModel model = new MenuModel();//一级菜单1MenuModel menu_1 = new MenuModel("预约服务");MenuModel menu_1_1 = new MenuModel("随心套餐", "miniprogram", "小程序页面没有的话进入这个URL", "", "小程序发布页面路径", appid);menu_1.Sub_Button.Add(menu_1_1);model.Sub_Button.Add(menu_1);//一级菜单2MenuModel menu_2 = new MenuModel("注册有礼");//click类型中 下面的V2002_Service 是和消息事件有关系的MenuModel menu_2_2 = new MenuModel("联系客服", "click", "", "V2002_Service", "", "");menu_2.Sub_Button.Add(menu_2_2);model.Sub_Button.Add(menu_2);一级菜单3//MenuModel menu_3 = new MenuModel("新人体验", "miniprogram", "http://mp.", "", "/pages/new_comer_act/new_comer_act", appid);//model.Sub_Button.Add(menu_3);MenuModel menu_3 = new MenuModel("活动特惠");MenuModel menu_3_1 = new MenuModel("随心套餐", "miniprogram", "小程序页面没有的话进入这个URL", "", "小程序发布页面路径", appid);menu_3.Sub_Button.Add(menu_3_1);model.Sub_Button.Add(menu_3);string result = "{\"button\":[";result += model.ToJson(model.Sub_Button);result += "}";string url = "https://api./cgi-bin/menu/create?access_token=" + oaToken;wxResult = Core.Lib.HttpService.Post(result, url, false, 600);}else{//删除自定义菜单wxResult = Core.Lib.HttpService.Get("https://api./cgi-bin/menu/delete?access_token=" + oaToken);}return new HttpResponseMessage(){Content = new StringContent(wxResult, Encoding.GetEncoding("UTF-8"), "application/x-www-from-urlencoded")};}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。