使用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; }
        }
    }

}

(0)

相关推荐

  • Android仿微信菜单(Menu)(使用C#和Java分别实现)

    本篇是对安卓菜单使用编程方式实现,当然可以使用XML的方式完成同样的功能,基本Java和C#写法都是一致的,所以使用XML的方式在本篇中使用Java演示,需要注意的是,对于如果不是VS开发的话,那么资源文件名称必须以小写开头,否则会报错. 运行效果 C#实现 using Android.App; using Android.OS; using Android.Views; using Android.Widget; namespace MenuDemo { [Activity(Label = "

  • asp.net(C#)生成无限级别菜单

    首先,创建数据库表的代码如下: 无限级树的数据库表代码 复制代码 代码如下: if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[work_sysmenu]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[work_sysmenu] GO CREATE TABLE [dbo].[work_sysmenu] ( [flowid] [

  • c# winform读取xml文件创建菜单的代码

    复制代码 代码如下: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using WinformMenu.Helper;using System.Xml; namespace WinformMen

  • C#正则表达式获取下拉菜单(select)的相关属性值

    给几个在C#中,使用正则表达式取页面下拉菜单(select)中的值示例: 复制代码 代码如下: //取html中全部 select 的 name Regex reg_name = new Regex(@"(?<=<select name=\"").*?(?=\"")"); //取html中全部<select>项的值 Regex reg_select = new Regex("(?is)<select nam

  • C#自定义控件添加右键菜单的方法

    C#自定义控件添加右键菜单非常简单,主要用到控件,像control定义右键菜单,用items.add()叠加右键菜单内容,用click事件处理函数. 1.control是要定义右键菜单的控件. private void control_MouseDown(object sender, MouseEventArgs e) {    if (e.Button == MouseButtons.Right)    {        ContextMenu menu = new rightClickMen

  • C#微信开发之自定义菜单管理

    自定义菜单管理 ①接口说明 微信服务号聊天窗口下面的菜单项(有的公众号有启用有的则没有),这个可以在编辑模式简单配置,也可以在开发模式代码配置.微信公众平台开发者文档:微信公众号开发平台创建自定义菜单,可以看到创建菜单的一些注意事项,下面的使用网页调试工具调试该接口,只是调试接口是否可以正常调用,并不是直接创建菜单,根据微信自定义菜单管理文档菜单事件可分为两种: click: 用户点击click类型按钮后,微信服务器会通过消息接口推送消息类型为event 的结构给开发者(参考消息接口指南),并且

  • C#实现Winform动态添加菜单的方法

    本文实例讲述了C#实现Winform动态添加菜单的方法.分享给大家供大家参考.具体分析如下: 最近在做WINFORM开发,一直都在为主界面的点击事件及动态加载菜单苦脑.现在已解决这个问题了,可以实现数据库或都XML等配置完成动态生成菜单及事件加载.代码如下: private void Form1_Load(object sender, EventArgs e) { //添加菜单一 ToolStripMenuItem subItem; subItem = AddContextMenu("入库&qu

  • C#递归读取XML菜单数据的方法

    本文实例讲述了C#递归读取XML菜单数据的方法.分享给大家供大家参考.具体分析如下: 最近在研究一些关于C#的一些技术,纵观之前的开发项目的经验,做系统时显示系统菜单的功能总是喜欢把数据写在数据库表,然后直接读取加载到菜单树上显示. 现在想把菜单数据都放在XML里,然后递归读取XML. 由于项目使用WCF,实体类使用了两个,一个是业务逻辑层中的实体,一个是调用业务逻辑层递归方法后进行数据实体的转换,XML读取方法写在业务逻辑层中. 思路: 1.先读取XML里所有的菜单 2.根据用户的权限显示所属

  • c# TreeView添加右键快键菜单有两种方法

    一种就是使用TreeView的ContextMenuStrip属性,添加一个新ContextMenuStrip,这个方法非常的简答直接,缺点是右键菜单是整个控件响应的,也就是说即使没有右键选中节点也是会触发快捷菜单的显示 这种方法里获取哪一个的node选中是通过这个方法: 复制代码 代码如下: TreeNode curNode = this.trvFolder.GetNodeAt(e.X, e.Y) 另一种是创建ContextMenuStrip,并且使用TreeView的NodeMouseCli

  • 基于C#实现的仿windows左侧伸缩菜单效果

    本文所述为基于C#实现的折叠菜单,风格仿照Windows打开我的电脑后左侧的伸缩菜单效果,并且同样是蓝色的效果,看着和windows的效果一样漂亮,可以实现折叠.展开等功能.这在学习C#界面编程的时候能用上,其主要实现代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq

随机推荐