using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;
using WeChatBiz;
using WeChatCommon;
namespace WeChat
{
public partial class Menu : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
#region 获取AccessToken
//自定义菜单token的获取 是用 下面的两个参数 获取的 不能直接用 公众平台的token
string to = new WeiXin().getGlobalAccessToken(CommomConst.APPID, CommomConst.SECRET);
//string to =GetData("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + CommomConst.APPID + "&secret=" + CommomConst.SECRET + "");
//获取json的token
//to = to.Substring(17, to.Length - 37);
#endregion
//删除菜单
string delResult = GetData("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + to);
//预约体验Url
string reserveTryUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + CommomConst.APPID + "&redirect_uri=" + CommomConst.HTTPURL + "/ReserveTry.aspx&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
#region 创建主菜单
string weixin1 = @" {
""button"":[
{
""type"":""click"",
""name"":""你好!"",
""key"":""Hello""
},
{
""type"":""view"",
""name"":""公司简介"",
""url"":""http://www.4ugood.net""
},
{
""name"":""产品介绍"",
""sub_button"":[
{
""type"":""click"",
""name"":""产品1"",
""key"":""P1""
},
{
""type"":""click"",
""name"":""产品2"",
""key"":""P2""
}]
}]
}
";
string i = GetPage("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + to, weixin1);
#endregion
#region 创建会员菜单
//签到Url
string signUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + CommomConst.APPID + "&redirect_uri=" + CommomConst.HTTPURL + "/Sign.aspx&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
//创建会员菜单
string menu1 = "";
menu1 += "{\n";
menu1 += "\"button\":[ \n";
menu1 += "{\n";
menu1 += "\"type\":\"view\",\n";
menu1 += "\"name\":\"用户绑定\",\n";
menu1 += "\"url\":\"" + bindUrl + "\"\n";
menu1 += "},\n";
menu1 += "{\n";
menu1 += "\"type\":\"view\",\n";
menu1 += "\"name\":\"课程表\",\n";
menu1 += "\"url\":\"" + CourseUrl + "\"\n";
menu1 += "},\n";
menu1 += "{\n";
menu1 += "\"type\":\"view\",\n";
menu1 += "\"name\":\"签到\",\n";
menu1 += "\"url\":\"" + signUrl + "\" \n";
menu1 += "}";
menu1 += "],\n";
menu1 += "\"matchrule\": { \n";
menu1 += "\"group_id\": \"100\" ";
menu1 += "}";
menu1 += "}";
string menuid1 = GetPage("https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token=" + to, menu1);
#endregion
////查询个性菜单
//string menuid = GetData("https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" + to);
//获取用户分组
//string groupid = getGroupId(to);
//{"id":0,"name":"未分组","count":8},
//{"id":1,"name":"黑名单","count":0},
//{"id":2,"name":"星标组","count":0},
//{"id":100,"name":"会员","count":0},
}
/// <summary>
/// 获取AccessToken
/// </summary>
/// <param name="url"> http请求方式: GET 接口URL: https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET </param>
/// <returns></returns>
private string GetData(string url)
{
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = "GET";
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
string content = reader.ReadToEnd();
reader.Close();
return content;
}
/// <summary>
/// 菜单创建
/// </summary>
/// <param name="posturl"> http请求方式: POST 接口URL: https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN </param>
/// <param name="postData">菜单数据Json</param>
/// <returns></returns>
public string GetPage(string posturl, string postData)
{
Stream outstream = null;
Stream instream = null;
StreamReader sr = null;
HttpWebResponse response = null;
HttpWebRequest request = null;
Encoding encoding = Encoding.UTF8;
byte[] data = encoding.GetBytes(postData);
// 准备请求...
try
{
// 设置参数
request = WebRequest.Create(posturl) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
outstream = request.GetRequestStream();
outstream.Write(data, 0, data.Length);
outstream.Close();
//发送请求并获取相应回应数据
response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
instream = response.GetResponseStream();
sr = new StreamReader(instream, encoding);
//返回结果网页(html)代码
string content = sr.ReadToEnd();
string err = string.Empty;
return content;
}
catch (Exception ex)
{
string err = ex.Message;
return string.Empty;
}
}
/// <summary>
/// 获取所有分组信息
/// </summary>
/// <param name="accessToken"></param>
/// <returns></returns>
private string getGroupId(string accessToken)
{
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("https://api.weixin.qq.com/cgi-bin/groups/get?access_token=" + accessToken);
myRequest.Method = "GET";
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
string content = reader.ReadToEnd();
reader.Close();
return content;
}
/// <summary>
/// 查询用户所在分组
/// </summary>
/// <param name="accessToken">调用接口凭证</param>
/// <param name="openid">用户的OpenID</param>
/// <returns></returns>
public string GetUserGroupId(string accessToken, string openid)
{
string url = string.Format("https://api.weixin.qq.com/cgi-bin/groups/getid?access_token={0}", accessToken);
string result = GetPage(url, "{\"openid\":" + openid + "}");
JavaScriptSerializer js = new JavaScriptSerializer();
Dictionary<string, object> list = new Dictionary<string, object>();
list = js.Deserialize<Dictionary<string, object>>(result);
if (list.ContainsKey("groupid"))
{
return list["groupid"].ToString();
}
return "";
}
}
}
微信公众号菜单创建
最新推荐文章于 2024-06-03 15:42:37 发布