微信企业号平台开发之获取菜单,创建菜单和删除菜单

Posted Jason_davis

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微信企业号平台开发之获取菜单,创建菜单和删除菜单相关的知识,希望对你有一定的参考价值。

技术分享

//获取菜单

protected void Button2_Click(object sender, EventArgs e)
{
Wechat.Config.Model.Request.MenuGet ss = new Model.Request.MenuGet();
ss.agentid = "0";
var ar = ss.Send();
//for (int i = 0; i < ar.menu.button.Count; i++)
//{
// Response.Write(ar.menu.button[i].name.ToString());
//}

ListBox1.Items.Add(JsonConvert.SerializeObject(ar));
}

//创建菜单

protected void Button3_Click(object sender, EventArgs e)
{
Wechat.Config.Model.Request.MenuCreate ss = new Model.Request.MenuCreate();
ss.agentid = "0";
List<Wechat.Config.Model.Request.MenuItem> list = new List<Wechat.Config.Model.Request.MenuItem>();
List<Wechat.Config.Model.Request.MenuItem> list1 = new List<Wechat.Config.Model.Request.MenuItem>();
Wechat.Config.Model.Request.MenuItem m;
m = new Wechat.Config.Model.Request.MenuItem();
m.key = "";
m.name = "本地新闻";
m.type = "view";
m.sub_button = null;
m.url = "http://www.baidu.com";
//ss.button.Add(m);
list.Add(m);
m = new Wechat.Config.Model.Request.MenuItem();
m.key = "http://www.baidu.com";
m.name = "今日天气";
m.type = "click";
m.sub_button = null;
m.url = "http://www.baidu.com";
//ss.button.Add(m);
list.Add(m);
m = new Wechat.Config.Model.Request.MenuItem();
m.key = "http://www.baidu.com";
m.name = "菜单";
m.type = "click";
m.sub_button = list1;
m.url = "00011";
//ss.button.Add(m);
list.Add(m);
ss.button = list;

m = new Wechat.Config.Model.Request.MenuItem();
m.key = "http://www.baidu.com";
m.name = "关于...";
m.type = "click";
m.sub_button = null;
m.url = "00011";
list1.Add(m);


m = new Wechat.Config.Model.Request.MenuItem();
m.key = "http://www.baidu.com";
m.name = "赞一下我们";
m.type = "click";
m.sub_button = null;
m.url = "00011";
list1.Add(m);
var ar = ss.Send();
ListBox1.Items.Add(JsonConvert.SerializeObject(ar));
}
/// <summary>
/// 删除菜单,其实是关闭自定义菜单
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button4_Click(object sender, EventArgs e)
{
Wechat.Config.Model.Request.MenuDelete ss = new Model.Request.MenuDelete();
ss.agentid = "0";
var ar = ss.Send();
ListBox1.Items.Add(JsonConvert.SerializeObject(ar));
}

 

用到的类

class MenuGet : OperationRequestBase<MenuGetResult, HttpGetRequest>
{
private string url = "https://qyapi.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN&agentid={0}";
protected override string Url()
{
return string.Format(url, agentid);
}


/// <summary>
/// 企业应用的id,整型。可在应用的设置页面查看
/// </summary>
/// <returns></returns>
[IsNotNull]
public string agentid { get; set; }
}

 

 

class MenuCreate : OperationRequestBase<OperationResultsBase, HttpPostRequest>
{
private string url = "https://qyapi.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN&agentid={0}";
protected override string Url()
{
return string.Format(url, agentid);
}

/// <summary>
/// 企业应用的id,整型。可在应用的设置页面查看
/// </summary>
/// <returns></returns>
[IsNotNull]
public string agentid { private get; set; }

/// <summary>
/// 一级菜单数组,个数应为1~3个
/// </summary>
/// <returns></returns>
public List<MenuItem> button { get; set; }
}

 

class MenuDelete : OperationRequestBase<OperationResultsBase, HttpGetRequest>
{
private string url = "https://qyapi.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN&agentid={0}";
protected override string Url()
{
return string.Format(url, agentid);
}

/// <summary>
/// 企业应用的id,整型。可在应用的设置页面查看
/// </summary>
/// <returns></returns>
[IsNotNull]
public string agentid { private get; set; }

 

以上是关于微信企业号平台开发之获取菜单,创建菜单和删除菜单的主要内容,如果未能解决你的问题,请参考以下文章

微信订阅号开发,获取openid的方式

微信订阅号开发,获取openid的方式

微信公众平台开发教程第1篇-新手解惑

C#微信公众号开发 -- 自定义菜单创建

《C#微信开发系列-自定义菜单管理》

微信公众平台开发之个性菜单创建