资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

如何使用c#开发公众平台自定义菜单功能-创新互联

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

网站设计制作过程拒绝使用模板建站;使用PHP+MYSQL原生开发可交付网站源代码;符合网站优化排名的后台管理系统;成都做网站、网站设计、外贸营销网站建设收费合理;免费进行网站备案等企业网站建设一条龙服务.我们是一家持续稳定运营了10年的成都创新互联网站建设公司。

代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="cm.aspx.cs" Inherits="guotaotao_weixin.cm" %>



    test


    
    

          

    

复制代码 代码如下:

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 {
        /// 
        /// form1 控件。
        /// 
        /// 
        /// 自动生成的字段。
        /// 若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。
        /// 
        protected global::System.Web.UI.HtmlControls.HtmlForm form1;
        /// 
        /// lt_msg 控件。
        /// 
        /// 
        /// 自动生成的字段。
        /// 若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。
        /// 
        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\":\"/tupian/20230522/honor_list.aspx%5C"" + "%5Cr%5Cn";
            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\":\"/tupian/20230522/aboutus.aspx%5C"" + "%5Cr%5Cn";
            postData += "}," + "\r\n";
            postData += "{ " + "\r\n";
            postData += "   \"type\":\"view\"," + "\r\n";
            postData += "   \"name\":\"联系我们\", " + "\r\n";
            postData += "   \"url\":\"/tupian/20230522/contactus.aspx%5C"" + "%5Cr%5Cn";
            postData += "}," + "\r\n";
            postData += "{ " + "\r\n";
            postData += "   \"type\":\"view\"," + "\r\n";
            postData += "   \"name\":\"查询订单\", " + "\r\n";
            postData += "   \"url\":\"/tupian/20230522/orders_chaxun.aspx%5C"" + "%5Cr%5Cn";
            postData += "}," + "\r\n";
            postData += "{ " + "\r\n";
            postData += "   \"type\":\"view\"," + "\r\n";
            postData += "   \"name\":\"留言反馈\", " + "\r\n";
            postData += "   \"url\":\"/tupian/20230522/feedback.aspx%5C"" + "%5Cr%5Cn";
            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 = "/tupian/20230522/token
            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 = "/tupian/20230522/get
            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 = "/tupian/20230522/create
            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 = "/tupian/20230522/delete
            string result = func.webRequestGet(url_Menu_Delete);
            return result;
        }
        /// 
        /// 验证微信签名
        /// 
        /// * 将token、timestamp、nonce三个参数进行字典序排序
        /// * 将三个参数字符串拼接成一个字符串进行sha1加密
        /// * 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。
        /// 
        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#开发公众平台自定义菜单功能-创新互联
本文地址:http://cdkjz.cn/article/djsipp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220