如何使用c#开发公众平台自定义菜单功能

这篇文章主要介绍如何使用c#开发公众平台自定义菜单功能,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

代码如下:

如何使用c#开发公众平台自定义菜单功能

<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="cm.aspx.cs"Inherits="guotaotao_weixin.cm"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>test</title>
</head>
<body>
<formid="form1"runat="server">
<p>
<asp:LiteralID="lt_msg"runat="server"></asp:Literal>
</p>
</form>
</body>
</html>

复制代码 代码如下:

usingSystem;
usingSystem.Collections;
usingSystem.Configuration;
usingSystem.Data;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.HtmlControls;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Xml.Linq;
namespaceguotaotao_weixin
{
publicpartialclasscm:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
if(!IsPostBack)
{
weixinwx=newweixin();
//lt_msg.Text=wx.GetAccessToken();
lt_msg.Text=wx.DelMenu();
lt_msg.Text+=wx.SetMenu();
}
}
}
}

复制代码 代码如下:

namespaceguotaotao_weixin{

publicpartialclasscm{
///<summary>
///form1控件。
///</summary>
///<remarks>
///自动生成的字段。
///若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。
///</remarks>
protectedglobal::System.Web.UI.HtmlControls.HtmlFormform1;
///<summary>
///lt_msg控件。
///</summary>
///<remarks>
///自动生成的字段。
///若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。
///</remarks>
protectedglobal::System.Web.UI.WebControls.Literallt_msg;
}
}

复制代码 代码如下:

usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.HtmlControls;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Xml.Linq;
usingguotaotao_weixin.database;
usingguotaotao.database;
namespaceguotaotao_weixin
{
publicclassguotaotao
{
privatestring_content="";
privatestring_FromUserName="";
publicintmsgType=0;
publicintisFirst=0;
publicintpid=0;
#region"构造函数"
publicguotaotao()
{
}
publicguotaotao(stringcontent)
{
this._content=content;
}
publicguotaotao(stringcontent,stringFromUserName)
{
this._content=content;
this._FromUserName=FromUserName;
}
#endregion

publicstringcreateMenuDate()
{
stringpostData="{"+"\r\n";
postData+="\"button\":["+"\r\n";
postData+="{"+"\r\n";
postData+="\"name\":\"产品\","+"\r\n";
postData+="\"sub_button\":["+"\r\n";
postData+="{"+"\r\n";
postData+="\"type\":\"click\","+"\r\n";
postData+="\"name\":\"无糖系列\","+"\r\n";
postData+="\"key\":\"gtt_menu_001001\""+"\r\n";
postData+="},"+"\r\n";
postData+="{"+"\r\n";
postData+="\"type\":\"click\","+"\r\n";
postData+="\"name\":\"干果系列\","+"\r\n";
postData+="\"key\":\"gtt_menu_001002\""+"\r\n";
postData+="},"+"\r\n";
postData+="{"+"\r\n";
postData+="\"type\":\"click\","+"\r\n";
postData+="\"name\":\"休闲系列\","+"\r\n";
postData+="\"key\":\"gtt_menu_001003\""+"\r\n";
postData+="},"+"\r\n";
postData+="{"+"\r\n";
postData+="\"type\":\"click\","+"\r\n";
postData+="\"name\":\"儿童系列\","+"\r\n";
postData+="\"key\":\"gtt_menu_001004\""+"\r\n";
postData+="}]"+"\r\n";
postData+="},"+"\r\n";
postData+="{"+"\r\n";
postData+="\"name\":\"活动\","+"\r\n";
postData+="\"sub_button\":["+"\r\n";
postData+="{"+"\r\n";
postData+="\"type\":\"click\","+"\r\n";
postData+="\"name\":\"抽奖\","+"\r\n";
postData+="\"key\":\"gtt_menu_002001\""+"\r\n";
postData+="},"+"\r\n";
postData+="{"+"\r\n";
postData+="\"type\":\"view\","+"\r\n";
postData+="\"name\":\"获奖名单\","+"\r\n";
postData+="\"url\":\"http://www.aaa.com/apps/honor_list.aspx\""+"\r\n";
postData+="},"+"\r\n";
postData+="{"+"\r\n";
postData+="\"type\":\"click\","+"\r\n";
postData+="\"name\":\"优惠券\","+"\r\n";
postData+="\"key\":\"gtt_menu_002003\""+"\r\n";
postData+="}]"+"\r\n";
postData+="},"+"\r\n";
postData+="{"+"\r\n";
postData+="\"name\":\"帮助\","+"\r\n";
postData+="\"sub_button\":["+"\r\n";
postData+="{"+"\r\n";
postData+="\"type\":\"view\","+"\r\n";
postData+="\"name\":\"关于我们\","+"\r\n";
postData+="\"url\":\"http://www.aaa.com/apps/aboutus.aspx\""+"\r\n";
postData+="},"+"\r\n";
postData+="{"+"\r\n";
postData+="\"type\":\"view\","+"\r\n";
postData+="\"name\":\"联系我们\","+"\r\n";
postData+="\"url\":\"http://www.aaa.com/apps/contactus.aspx\""+"\r\n";
postData+="},"+"\r\n";
postData+="{"+"\r\n";
postData+="\"type\":\"view\","+"\r\n";
postData+="\"name\":\"查询订单\","+"\r\n";
postData+="\"url\":\"http://www.aaa.com/apps/orders_chaxun.aspx\""+"\r\n";
postData+="},"+"\r\n";
postData+="{"+"\r\n";
postData+="\"type\":\"view\","+"\r\n";
postData+="\"name\":\"留言反馈\","+"\r\n";
postData+="\"url\":\"http://www.aaa.com/apps/feedback.aspx\""+"\r\n";
postData+="}]"+"\r\n";
postData+="}]"+"\r\n";
postData+="}"+"\r\n";
returnpostData;
}
}
}

菜单中的URL地址换成自己的

复制代码 代码如下:

usingSystem;
usingSystem.Web.Security;
usingSystem.Xml;
usingaaa_weixin.database;
usingNewtonsoft.Json;
namespaceaaa_weixin
{
publicclassweixin
{
privatestringToken="";//微信里面开发者模式Token
privatestringdevlopID="";//微信里面开发者模式:开发者ID
privatestringdevlogPsw="";//微信里面开发者模式:开发者密码
publicstringAccessToken="";//获取的通行证
publicvoidAuth()
{
stringechoStr=System.Web.HttpContext.Current.Request.QueryString["echoStr"];
if(CheckSignature())
{
if(!string.IsNullOrEmpty(echoStr))
{
System.Web.HttpContext.Current.Response.Write(echoStr);
System.Web.HttpContext.Current.Response.End();
}
}
}
publicstringGetAccessToken()//获取通行证
{
stringurl_token="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+devlopID+"&secret="+devlogPsw;
stringresult=func.webRequestGet(url_token);
accessTokendeserializedProduct=(accessToken)JsonConvert.DeserializeObject(result,typeof(accessToken));
this.AccessToken=deserializedProduct.access_Token;
returnthis.AccessToken;
}
publicstringGetMenu()//获取当前菜单情况
{
stringurl_Menu_Get="https://api.weixin.qq.com/cgi-bin/menu/get?access_token="+this.AccessToken;
stringoutput=func.webRequestGet(url_Menu_Get);
//wxErrdeserializedProduct=(wxErr)JsonConvert.DeserializeObject(output,typeof(wxErr));
//returndeserializedProduct.errmsg;
returnoutput;
}
publicstringSetMenu()//设置最新菜单
{
stringurl_Menu_Create="https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+this.GetAccessToken();
aaagtt=newaaa();
stringpostData=gtt.createMenuDate();
stringresult=func.webRequestPost(url_Menu_Create,postData);
returnresult;
}
publicstringDelMenu()//删除菜单
{
stringurl_Menu_Delete="https://api.weixin.qq.com/cgi-bin/menu/delete?access_token="+this.GetAccessToken();
stringresult=func.webRequestGet(url_Menu_Delete);
returnresult;
}
///<summary>
///验证微信签名
///</summary>
///*将token、timestamp、nonce三个参数进行字典序排序
///*将三个参数字符串拼接成一个字符串进行sha1加密
///*开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。
///<returns></returns>
privateboolCheckSignature()
{
stringsignature=System.Web.HttpContext.Current.Request.QueryString["signature"];
stringtimestamp=System.Web.HttpContext.Current.Request.QueryString["timestamp"];
stringnonce=System.Web.HttpContext.Current.Request.QueryString["nonce"];
string[]ArrTmp={Token,timestamp,nonce};
Array.Sort(ArrTmp);//字典排序
stringtmpStr=string.Join("",ArrTmp);
tmpStr=FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr,"SHA1");
tmpStr=tmpStr.ToLower();
if(tmpStr==signature)
{
returntrue;
}
else
{
returnfalse;
}
}
}
publicclasswxErr
{
privateintErrcode;
publicinterrcode
{
get{returnErrcode;}
set{Errcode=value;}
}
privatestringErrmsg;
publicstringerrmsg
{
get{returnErrmsg;}
set{Errmsg=value;}
}
}
publicclassaccessToken
{
privatestringaccess_token;
publicstringaccess_Token
{
get{returnaccess_token;}
set{access_token=value;}
}
privateintexpires_in;
publicintexpires_In
{
get{returnexpires_in;}
set{expires_in=value;}
}
}
}

以上是“如何使用c#开发公众平台自定义菜单功能”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注恰卡编程网行业资讯频道!

发布于 2021-03-13 15:39:05
收藏
分享
海报
0 条评论
159
上一篇:微信开发之如何使用Senparc.Weixin.MP SDK 下一篇:c#使用微信接口开发微信门户应用中微信消息的处理和应答的示例
目录

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码