c#を使用してパブリックプラットフォームのカスタムメニュー機能を開発

14150 ワード

 
  



    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
";
            postData += "\"button\":[ " + "\r
";
            postData += "{ " + "\r
";
            postData += "\"name\":\" \"," + "\r
";
            postData += "\"sub_button\":[" + "\r
";
            postData += "{ " + "\r
";
            postData += "   \"type\":\"click\"," + "\r
";
            postData += "   \"name\":\" \", " + "\r
";
            postData += "   \"key\":\"gtt_menu_001001\"" + "\r
";
            postData += "}," + "\r
";
            postData += "{ " + "\r
";
            postData += "   \"type\":\"click\"," + "\r
";
            postData += "   \"name\":\" \", " + "\r
";
            postData += "   \"key\":\"gtt_menu_001002\"" + "\r
";
            postData += "}," + "\r
";
            postData += "{ " + "\r
";
            postData += "   \"type\":\"click\"," + "\r
";
            postData += "   \"name\":\" \", " + "\r
";
            postData += "   \"key\":\"gtt_menu_001003\"" + "\r
";
            postData += "}," + "\r
";
            postData += "{ " + "\r
";
            postData += "   \"type\":\"click\"," + "\r
";
            postData += "   \"name\":\" \", " + "\r
";
            postData += "   \"key\":\"gtt_menu_001004\"" + "\r
";
            postData += " }]" + "\r
";
            postData += "}," + "\r
";
            postData += "{" + "\r
";
            postData += "\"name\":\" \", " + "\r
";
            postData += "\"sub_button\":[" + "\r
";
            postData += "{ " + "\r
";
            postData += "   \"type\":\"click\"," + "\r
";
            postData += "   \"name\":\" \", " + "\r
";
            postData += "   \"key\":\"gtt_menu_002001\"" + "\r
";
            postData += "}," + "\r
";
            postData += "{ " + "\r
";
            postData += "   \"type\":\"view\"," + "\r
";
            postData += "   \"name\":\" \", " + "\r
";
            postData += "   \"url\":\"http://www.aaa.com/apps/honor_list.aspx\"" + "\r
";
            postData += "}," + "\r
";
            postData += "{ " + "\r
";
            postData += "   \"type\":\"click\"," + "\r
";
            postData += "   \"name\":\" \", " + "\r
";
            postData += "   \"key\":\"gtt_menu_002003\"" + "\r
";
            postData += " }]" + "\r
";
            postData += "}," + "\r
";
            postData += "{" + "\r
";
            postData += "\"name\":\" \"," + "\r
";
            postData += "\"sub_button\":[" + "\r
";
            postData += "{ " + "\r
";
            postData += "   \"type\":\"view\"," + "\r
";
            postData += "   \"name\":\" \", " + "\r
";
            postData += "   \"url\":\"http://www.aaa.com/apps/aboutus.aspx\"" + "\r
";
            postData += "}," + "\r
";
            postData += "{ " + "\r
";
            postData += "   \"type\":\"view\"," + "\r
";
            postData += "   \"name\":\" \", " + "\r
";
            postData += "   \"url\":\"http://www.aaa.com/apps/contactus.aspx\"" + "\r
";
            postData += "}," + "\r
";
            postData += "{ " + "\r
";
            postData += "   \"type\":\"view\"," + "\r
";
            postData += "   \"name\":\" \", " + "\r
";
            postData += "   \"url\":\"http://www.aaa.com/apps/orders_chaxun.aspx\"" + "\r
";
            postData += "}," + "\r
";
            postData += "{ " + "\r
";
            postData += "   \"type\":\"view\"," + "\r
";
            postData += "   \"name\":\" \", " + "\r
";
            postData += "   \"url\":\"http://www.aaa.com/apps/feedback.aspx\"" + "\r
";
            postData += " }]" + "\r
";
            postData += "}]" + "\r
";
            postData += "}" + "\r
";

            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;
        }
        ///


        ///
        ///

        /// * 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; }
        }
    }

 }