订阅号
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WX_CaiDan.aspx.cs" Inherits="WX_CaiDan" %>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div>
<form runat="server" id="form1">
<div>
<asp:Label ID="Label3" runat="server" ></asp:Label>
</div>
<div class="Main">
<div class="CartTitle">
<h2>微信菜单设置</h2>
</div>
<div class="white">
添加菜单json:<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" CssClass="w300 h100">{"button":[{"name":"中心介绍","sub_button":[{"type":"view","name":"中心首页","url":"http://******/"},{"type":"view","name":"医生团队","url":"http://******/"},{"type":"view","name":"家医工作室","url":"http://******/"},{"type":"view","name":"特色诊疗","url":"http://******/"},{"type":"view","name":"病房介绍","url":"http://******/"}]},{"name":"中心动态","sub_button":[{"type":"view","name":"中心公告","url":"http://******/"},{"type":"view","name":"中心动态","url":"http://******/"},{"type":"view","name":"中医养生","url":"http://******/"},{"type":"view","name":"家医故事","url":"http://******/"},{"type":"view","name":"科普园地","url":"http://******/"}]},{"name":"中心服务","sub_button":[{"type":"view","name":"门诊预约","url":"http://******/"},{"type":"view","name":"报告查询","url":"http://******/"},{"type":"view","name":"智能服务","url":"http://******/"},{"type":"view","name":"疫苗预约","url":"http://******/"},{"type":"view","name":"老人体检","url":"http://******/"}]}]}</asp:TextBox><br />
添加菜单结果:<asp:Label ID="Label1" runat="server"></asp:Label><br />
<asp:Button ID="Button1" runat="server" Text="添加菜单" OnClick="Button1_Click" />
</div>
<div>
删除菜单结果:<asp:Label ID="Label2" runat="server"></asp:Label><br />
<asp:Button ID="Button2" runat="server" Text="删除所有菜单" OnClick="Button2_Click" />
</div>
</div>
</form>
</div>
</body>
</html>
-----------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using LitJson;
public partial class WX_CaiDan : System.Web.UI.Page
{
public string access_token = "";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
WebClient client = new WebClient();
string AccessTokenURL = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", "wx************************0", "d8df1**********************62f4b");
client.Encoding = System.Text.Encoding.UTF8;
var result2 = client.DownloadString(AccessTokenURL);
JsonData jd2 = JsonMapper.ToObject(result2);
access_token = (string)jd2["access_token"];
}
catch (Exception ex)
{
Label3.Text = ex.StackTrace;
}
///添加菜单 例子 {"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"}]}]}
string json = TextBox1.Text;
Label1.Text += HttpService.Post(json, "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + access_token, false, 10);
}
protected void Button2_Click(object sender, EventArgs e)
{
string url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + access_token;
Label2.Text = HttpService.Get(url);
}
}
参数说明
|
参数
|
是否必须
|
说明
|
|
button
|
是
|
一级菜单数组,个数应为1~3个
|
|
sub_button
|
否
|
二级菜单数组,个数应为1~5个
|
|
type
|
是
|
菜单的响应动作类型,view表示网页类型,click表示点击类型,miniprogram表示小程序类型
|
|
name
|
是
|
菜单标题,不超过16个字节,子菜单不超过60个字节
|
|
key
|
click等点击类型必须
|
菜单KEY值,用于消息接口推送,不超过128字节
|
|
url
|
view、miniprogram类型必须
|
网页 链接,用户点击菜单可打开链接,不超过1024字节。 type为miniprogram时,不支持小程序的老版本客户端将打开本url。
|
|
media_id
|
media_id类型和view_limited类型必须
|
调用新增永久素材接口返回的合法media_id
|
|
appid
|
miniprogram类型必须
|
小程序的appid(仅认证公众号可配置)
|
|
pagepath
|
miniprogram类型必须
|
小程序的页面路径
|
|
article_id
|
article_id类型和article_view_limited类型必须
|
发布后获得的合法 article_id
|