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

 更新时间:2014年01月24日 14:35:15   作者:  
微信公众平台服务号开放了自定义菜单API,本案例介绍C#开发微信公众号自定义菜单功能。在此基础上可以开发更完善的自定义菜单管理功能

复制代码 代码如下:

<%@ 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; }
        }
    }

 }

相关文章

  • C#编写网游客户端的实现

    C#编写网游客户端的实现

    本文主要介绍了C#编写网游客户端,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11
  • C#调用AForge实现摄像头录像的示例代码

    C#调用AForge实现摄像头录像的示例代码

    这篇文章主要介绍了C#调用AForge实现摄像头录像的示例代码,非常具有实用价值,需要的朋友可以参考下
    2017-09-09
  • C# 变量,常量数据类型详情

    C# 变量,常量数据类型详情

    这篇文章主要介绍了C# 变量,常量数据类型详情,常量是指在程序运行中不能改变的数据,定义一个常量与定义一个变量过程类似,只是多加一个关键词const
    2022-07-07
  • C#使用移位运算符获取汉字编码的示例代码

    C#使用移位运算符获取汉字编码的示例代码

    这篇文章主要为大家详细介绍了C#如何使用移位运算符获取汉字编码,文中的示例代码讲解详细,具有一定的借鉴价值,需要的小伙伴可以参考下
    2024-01-01
  • C# PLINQ 内存列表查询优化历程

    C# PLINQ 内存列表查询优化历程

    这篇文章主要介绍了C# PLINQ 内存列表查询优化历程的相关资料,需要的朋友可以参考下
    2016-03-03
  • C#中List.Contains(T)失效的解决方法

    C#中List.Contains(T)失效的解决方法

    这篇文章主要介绍了C#中List.Contains(T)失效的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-03-03
  • C#连接ODBC数据源的方法

    C#连接ODBC数据源的方法

    这篇文章主要介绍了C#连接ODBC数据源的方法,实例分析了C#连接ODBC操作数据库的技巧,非常具有实用价值,需要的朋友可以参考下
    2015-04-04
  • C# 绘制统计图大全(柱状图, 折线图, 扇形图)

    C# 绘制统计图大全(柱状图, 折线图, 扇形图)

    本篇文章介绍了C# 绘制统计图大全,其中包括状图, 折线图, 扇形图,有需要的同学可以了解一下。
    2016-11-11
  • 解析C#的扩展方法

    解析C#的扩展方法

    在本文中,主要对扩展方法进行了一些规则说明、声明方式,使用方式,以及对扩展方法的意义和扩展方法的原理进行了简单的解答。并在本文的最后给了一个枚举的扩展方法代码。
    2016-12-12
  • 详解如何将.NET应用转换成Window服务

    详解如何将.NET应用转换成Window服务

    这篇文章主要为大家详细介绍了如何将.NET8.0应用程序转换成Windows服务,文中的示例代码讲解详细,有需要的小伙伴可以跟随小编一起学习一下
    2024-01-01

最新评论