欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-25 17:35:33
复制代码 代码如下:<%@ page language="c#" autoeventwireup="true" codebehind="cm.aspx.cs" inh...

复制代码 代码如下:

<%@ page language="c#" autoeventwireup="true" codebehind="cm.aspx.cs" inherits="guotaotao_weixin.cm" %>

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>test</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:literal id="lt_msg" runat="server" ></asp:literal>
    </div>
    </form>
</body>
</html>

复制代码 代码如下:

using system;
using system.collections;
using system.configuration;
using system.data;
using system.linq;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.htmlcontrols;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.xml.linq;

namespace guotaotao_weixin
{
    public partial class cm : system.web.ui.page
    {
        protected void page_load(object sender, eventargs e)
        {
            if (!ispostback)
            {
                weixin wx = new weixin();
                //lt_msg.text = wx.getaccesstoken();
                lt_msg.text = wx.delmenu();

                lt_msg.text += wx.setmenu();
            }
        }
    }
}

复制代码 代码如下:

namespace guotaotao_weixin {

   
    public partial class cm {

        /// <summary>
        /// form1 控件。
        /// </summary>
        /// <remarks>
        /// 自动生成的字段。
        /// 若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。
        /// </remarks>
        protected global::system.web.ui.htmlcontrols.htmlform form1;

        /// <summary>
        /// lt_msg 控件。
        /// </summary>
        /// <remarks>
        /// 自动生成的字段。
        /// 若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。
        /// </remarks>
        protected global::system.web.ui.webcontrols.literal lt_msg;
    }
}

复制代码 代码如下:

using system;
using system.data;
using system.configuration;
using system.linq;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.htmlcontrols;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.xml.linq;
using guotaotao_weixin.database;
using guotaotao.database;

namespace guotaotao_weixin
{
    public class guotaotao
    {
        private string _content = "";
        private string _fromusername = "";
        public int msgtype = 0;
        public int isfirst = 0;
        public int pid = 0;

        #region "构造函数"
            public guotaotao()
            {
            }
            public guotaotao(string content)
            {
                this._content = content;
            }
            public guotaotao(string content,string fromusername)
            {
                this._content = content;
                this._fromusername = fromusername;
            }
        #endregion

        
        public string createmenudate()
        {
            string postdata = "{" + "\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";

            return postdata;
        }

    }
}


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

复制代码 代码如下:

using system;
using system.web.security;
using system.xml;
using aaa_weixin.database;
using newtonsoft.json;

namespace aaa_weixin
{
    public class weixin
    {
        private string token = ""; //微信里面开发者模式token
        private string devlopid = "";//微信里面开发者模式:开发者id
        private string devlogpsw = "";//微信里面开发者模式: 开发者密码
        public string accesstoken = ""; //获取的通行证

        public void auth()
        {
            string echostr = 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();
                }
            }
        }

        public string getaccesstoken() //获取通行证
        {           
            string url_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + devlopid + "&secret=" + devlogpsw;
            string result = func.webrequestget(url_token);
            accesstoken deserializedproduct = (accesstoken)jsonconvert.deserializeobject(result, typeof(accesstoken));
            this.accesstoken  = deserializedproduct.access_token;
            return this.accesstoken;
        }

        public string getmenu() //获取当前菜单情况
        {
            string url_menu_get = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" + this.accesstoken;
            string output = func.webrequestget(url_menu_get);
            //wxerr deserializedproduct = (wxerr)jsonconvert.deserializeobject(output, typeof(wxerr));
            //return deserializedproduct.errmsg;
            return output;
        }

        public string setmenu() //设置最新菜单
        {
            string url_menu_create = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + this.getaccesstoken();

            aaa gtt = new aaa();
            string postdata = gtt.createmenudate();
            string result = func.webrequestpost(url_menu_create,postdata);

            return result;
        }

        public string delmenu() //删除菜单
        {
            string url_menu_delete = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + this.getaccesstoken();
            string result = func.webrequestget(url_menu_delete);

            return result;
        }
        /// <summary>
        /// 验证微信签名
        /// </summary>
        /// * 将token、timestamp、nonce三个参数进行字典序排序
        /// * 将三个参数字符串拼接成一个字符串进行sha1加密
        /// * 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。
        /// <returns></returns>
        private bool checksignature()
        {
            string signature = system.web.httpcontext.current.request.querystring["signature"];
            string timestamp = system.web.httpcontext.current.request.querystring["timestamp"];
            string nonce = system.web.httpcontext.current.request.querystring["nonce"];
            string[] arrtmp = { token, timestamp, nonce };
            array.sort(arrtmp);     //字典排序
            string tmpstr = string.join("", arrtmp);
            tmpstr = formsauthentication.hashpasswordforstoringinconfigfile(tmpstr, "sha1");
            tmpstr = tmpstr.tolower();
            if (tmpstr == signature)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }

    public class wxerr
    {
        private int errcode;
        public int errcode
        {
            get { return errcode; }
            set { errcode = value; }
        }

        private string errmsg;
        public string errmsg
        {
            get { return errmsg; }
            set { errmsg = value; }
        }
    }
    public class accesstoken
    {
        private string access_token;
        public string access_token
        {
            get { return access_token; }
            set { access_token = value; }
        }
        private int expires_in;
        public int expires_in
        {
            get { return expires_in; }
            set { expires_in = value; }
        }
    }

 }