微信公眾號平臺接口開發(fā) 菜單管理的實(shí)現(xiàn)
官方菜單功能介紹


請求接口:https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN


新增菜單管理類
public class MenuFirstLayerModel
{
public string name { get; set; }
public List<MenuTwoLayerModel> sub_button { get; set; }
}
public class MenuTwoLayerModel
{
public string type { get; set; }
public string name { get; set; }
public string key { get; set; }
public string url { get; set; }
}
public class WXMenu
{
public List<MenuFirstLayerModel> button { get; set; }
public string Create()
{
try
{
var requestUri = string.Format(@"https://api.weixin.qq.com/cgi-bin/menu/create?access_token={0}", WeCharBase.AccessToken);
return WeCharBase.Post(requestUri, new StringContent(JsonConvert.SerializeObject(new
{
button = button
})));
}
catch (Exception ex)
{
return ex.Message;
}
}
}
新增控制器MenuController.cs
public ActionResult ViewMenu()
{
return View();
}
public ActionResult CreateMenu()
{
var wxMenu = new WXMenu()
{
button = new List<MenuFirstLayerModel>()
{
new MenuFirstLayerModel()
{
name="掃碼",
sub_button = new List<MenuTwoLayerModel>()
{
new MenuTwoLayerModel()
{
type = "scancode_waitmsg",
name = "掃碼帶提示",
key = "rselfmenu_0_0"
},
new MenuTwoLayerModel()
{
type = "scancode_push",
name = "掃碼推事件",
key = "rselfmenu_0_1"
}
},
},
new MenuFirstLayerModel()
{
name = "發(fā)圖",
sub_button = new List<MenuTwoLayerModel>()
{
new MenuTwoLayerModel()
{
type = "pic_sysphoto",
name = "系統(tǒng)拍照發(fā)圖",
key = "rselfmenu_1_0"
},
new MenuTwoLayerModel()
{
type = "pic_photo_or_album",
name = "拍照或者相冊發(fā)圖",
key = "rselfmenu_1_1"
},
new MenuTwoLayerModel()
{
type = "pic_weixin",
name = "微信相冊發(fā)圖",
key = "rselfmenu_1_2"
}
}
},
new MenuFirstLayerModel()
{
name = "其他",
sub_button = new List<MenuTwoLayerModel>()
{
new MenuTwoLayerModel()
{
type = "location_select",
name = "發(fā)送位置",
key = "rselfmenu_2_0"
},
new MenuTwoLayerModel()
{
type = "click",
name = "今日歌曲",
key = "V1001_TODAY_MUSIC"
},
new MenuTwoLayerModel()
{
type = "view",
name = "百度",
url = "http://www.baidu.com"
}
}
}
}
};
return Content(wxMenu.Create());
}
新增視圖ViewMenu.cshtml
<script type="text/javascript" language="javascript">
$(document).ready(function () {
$("#btnCreate").click(function () {
$.ajax({
type: "POST",
url: "/Menu/CreateMenu",
data: { id: $("#textContent").val() },
success: function (responseTest) {
$("#resultMesage").text(responseTest);
}
});
});
});
</script>
<table>
<tr>
<td>
<div class="title">菜單創(chuàng)建</div>
<textarea id="textContent" name="textContent" rows="20" cols="100">
button = new[]
{
new
{
name="掃碼",
sub_button = new[]
{
new
{
type = "scancode_waitmsg",
name = "掃碼帶提示",
key = "rselfmenu_0_0"
},
new
{
type = "scancode_push",
name = "掃碼推事件",
key = "rselfmenu_0_1"
}
}
},
new
{
name = "發(fā)圖",
sub_button = new[]
{
new
{
type = "pic_sysphoto",
name = "系統(tǒng)拍照發(fā)圖",
key = "rselfmenu_1_0"
},
new
{
type = "pic_photo_or_album",
name = "拍照或者相冊發(fā)圖",
key = "rselfmenu_1_1"
},
new
{
type = "pic_weixin",
name = "微信相冊發(fā)圖",
key = "rselfmenu_1_2"
}
}
},
new
{
name = "其他",
sub_button = new[]
{
new
{
type = "location_select",
name = "發(fā)送位置",
key = "rselfmenu_2_0"
},
new
{
type = "click",
name = "今日歌曲",
key = "V1001_TODAY_MUSIC"
},
new
{
type = "view",
name = "百度",
url = "http://www.baidu.com"
}
}
}
}
</textarea>
</td>
<td><input class="btncss" id="btnCreate" type="button" value=" 創(chuàng)建 " /></td>
<td id="resultMesage" class="resultMesage"></td>
</tr>
</table>
有效代碼寫完了,看看效果


成功了哦。
相關(guān)文章
JavaScript獲取當(dāng)前網(wǎng)頁標(biāo)題(title)的方法
這篇文章主要介紹了JavaScript獲取當(dāng)前網(wǎng)頁標(biāo)題(title)的方法,涉及javascript中document.title方法的使用,需要的朋友可以參考下2015-04-04
layui 地區(qū)三級聯(lián)動 form select 渲染的實(shí)例
今天小編就為大家分享一篇layui 地區(qū)三級聯(lián)動 form select 渲染的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09
ToolTip 通過Js實(shí)現(xiàn)代替超鏈接中的title效果
ToolTip 通過Js實(shí)現(xiàn)代替超鏈接中的title效果,需要的朋友可以參考下。2011-04-04
Javascript 類型轉(zhuǎn)換、封閉函數(shù)及常見內(nèi)置對象操作示例
這篇文章主要介紹了Javascript 類型轉(zhuǎn)換、封閉函數(shù)及常見內(nèi)置對象操作,結(jié)合實(shí)例形式分析了JavaScript類型顯示轉(zhuǎn)換、隱式轉(zhuǎn)換、變量作用域、封閉函數(shù)及常用內(nèi)置對象相關(guān)操作技巧,需要的朋友可以參考下2019-11-11
微信小程序onLaunch異步,首頁onLoad先執(zhí)行?
這篇文章主要介紹了微信小程序onLaunch異步,首頁onLoad先執(zhí)行? 文章底部給大家介紹了小程序_onLaunch異步回調(diào)數(shù)據(jù)加載問題的兩種解決方案,需要的朋友可以參考下2018-09-09
Asp.Net alert彈出提示信息的幾種方法總結(jié)
本篇文章主要是對Asp.Net alert彈出提示信息的幾種方法進(jìn)行了總結(jié)介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01
js監(jiān)聽鼠標(biāo)事件控制textarea輸入字符串的個數(shù)
一個js控制textarea輸入字符串的個數(shù)的腳本,當(dāng)鼠標(biāo)按下抬起時判斷輸入字符數(shù),很簡單,但很實(shí)用2014-09-09

