网站地图    收藏   

主页 > 后端 > 微信开发 >

分享微信开发的一些经验! - 微信公众平台开发

来源:自学PHP网    时间:2015-04-14 12:58 作者: 阅读:

[导读] 首先是微信开发登陆https: mp weixin qq com cgi-bin loginpage?t=wxm2-loginlang=zh_CN到开发者中心 开启服务器配置 这里写一个服务器url 这样写:using System;using System IO;using System Dat...

 

 到开发者中心 开启服务器配置 这里写一个服务器url 这样写:
 
using System;
using System.IO;
using System.Data;
using System.Text;
using System.Security;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cn.Com.Shijinet.Kunlun.Log; //日志
using System.Xml;
using ShandsWeixin.business;
using System.Threading;

namespace ShandsWeixin
{
    public partial class MessageReceiver : System.Web.UI.Page
    {
        Logger logger = Logger.GetLogger(typeof(MessageReceiver));
        protected void Page_Load(object sender, EventArgs e)
        {
            logger.Debug("start:" + DateTime.Now.ToString()+":" + DateTime.Now.Millisecond.ToString());
            logger.Debug("querystring:" + Request.Url);
            string signature = Request.QueryString["signature"];
            if (signature != null && signature != "")
            {
                string timestamp = Request.QueryString["timestamp"];
                string nonce = Request.QueryString["nonce"];
                string echostr = Request.QueryString["echostr"];
                string token = ""; //Token
                //token = System.Configuration.ConfigurationManager.AppSettings["token"];
                //token = "75a2f055b55357917a23fc42273ba5e1";
                string sha1Str = SHA1Encrypt(nonce + timestamp + token);
                if (string.Compare(nonce, timestamp) > 0)
                {
                    sha1Str = SHA1Encrypt(timestamp + nonce + token);
                }

                if (signature == null)
                {
                    signature = "";
                }
                //Thread.Sleep(800);

                if (signature.ToLower() == sha1Str)
                {
                    Response.Clear();
                    Response.Write(echostr);
                }
                logger.Debug("signature:" + signature);
                logger.Debug("timestamp:" + timestamp);
                logger.Debug("nonce:" + nonce);
                logger.Debug("echostr:" + echostr);
                logger.Debug("sha1Str:" + sha1Str);
            }
       //这个方法用来定义菜单或者自动回复
            if (Request.RequestType == "POST")
            {
                StreamReader reader = new StreamReader(Request.InputStream);
                String xmlData = reader.ReadToEnd();

                logger.Debug("xmlData:" + xmlData);

                XmlDocument msgXml = new XmlDocument();
                msgXml.LoadXml(xmlData);
                MessageProcess msgProcess = new MessageProcess();
                string returnMsg = msgProcess.ProcessMessage(msgXml);
                Response.Clear();
                Response.Write(returnMsg);
                logger.Debug("returnMsg:" + returnMsg);
                return;
            }
            logger.Debug("end:" + DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString());
           

        }
        public string SHA1Encrypt(string strIn)
        {
            byte[] tempByte = null;
            SHA1 sha1 = new SHA1CryptoServiceProvider();
            tempByte = sha1.ComputeHash(Encoding.UTF8.GetBytes(strIn));
            string strOut = "";

            strOut = BitConverter.ToString(tempByte).Replace("-", "");

            return strOut.ToLower();

        }
    }
}

 

然后写这个代码用来处理微信的消息类型
 
using System;
using System.Collections.Generic;
using System.Web;
using System.Xml;
using System.Text;
using ShandsWeixin.model;
using Cn.Com.Shijinet.Kunlun.Log;

namespace ShandsWeixin.business
{
    public class MessageProcess
    {
        Logger logger = Logger.GetLogger(typeof(MessageProcess));
        public string ProcessMessage(XmlDocument xmlData)
        {
            //转换xml消息为对象
            ReceiveMessage recMsg = ConvertReceiveMessage(xmlData);
            recMsg.SourceType = "weixin";

            //获取回复消息
            object replyMsg = GetReplyContentByMessage(recMsg);

            //转换回复消息为XML
            return ConvertReplyMessage(replyMsg);
        }

        public object GetReplyContentByMessage(ReceiveMessage recMessage)
        {
            logger.Debug(recMessage.MsgType);
            switch (recMessage.MsgType)
            {
                case "event":
                    switch (recMessage.Event)
                    {
                        //订阅关注
                        case "subscribe":
                            return MessageContent.getSubscribeMessage(recMessage);

                        //取消关注
                        case "unsubscribe":
                            return null;

                        //点击事件
                        case "CLICK":
                            return MessageContent.getClickMessage(recMessage);
                    }                

                    break;
                case "text":
                    //根据发送内容获取回复内容
                    return MessageContent.getMessageBySendMsg(recMessage);

                default:
                    return MessageContent.getDefaultMessage(recMessage);                

            }
            return null;
        }

        /// <summary>
        /// 转换接收到的XML为接收消息对象
        /// </summary>
        /// <param name="xmlData"></param>
        /// <returns></returns>
        public ReceiveMessage ConvertReceiveMessage(XmlDocument xmlData)
        { 
            ReceiveMessage recMsg = new ReceiveMessage();
            recMsg.FromUserName = MessageUtil.FormatMessage(xmlData.SelectSingleNode("/xml/FromUserName").InnerText);
            recMsg.ToUserName = MessageUtil.FormatMessage(xmlData.SelectSingleNode("/xml/ToUserName").InnerText);
            recMsg.MsgType = MessageUtil.FormatMessage(xmlData.SelectSingleNode("/xml/MsgType").InnerText);
           
            switch (recMsg.MsgType)
            {
                case "event":
                    recMsg.Event = MessageUtil.FormatMessage(xmlData.SelectSingleNode("/xml/Event").InnerText);
                    recMsg.EventKey = MessageUtil.FormatMessage(xmlData.SelectSingleNode("/xml/EventKey").InnerText);
                    recMsg.MsgId = MessageUtil.FormatMessage(xmlData.SelectSingleNode("/xml/MsgId").InnerText);
                    break;
                case "text":
                    recMsg.Content = MessageUtil.FormatMessage(xmlData.SelectSingleNode("/xml/Content").InnerText);
                    recMsg.MsgId = MessageUtil.FormatMessage(xmlData.SelectSingleNode("/xml/MsgId").InnerText);
                    break;
                case "image":
                    recMsg.PicUrl = MessageUtil.FormatMessage(xmlData.SelectSingleNode("/xml/PicUrl").InnerText);
                    recMsg.MediaId = MessageUtil.FormatMessage(xmlData.SelectSingleNode("/xml/MediaId").InnerText);
                    recMsg.ThumbMediaId = MessageUtil.FormatMessage(xmlData.SelectSingleNode("/xml/ThumbMediaId").InnerText);
                    recMsg.MsgId = MessageUtil.FormatMessage(xmlData.SelectSingleNode("/xml/MsgId").InnerText);
                    break;
                case "voice":
                    recMsg.MediaId = MessageUtil.FormatMessage(xmlData.SelectSingleNode("/xml/MediaId").InnerText);
                    recMsg.MsgFormat = MessageUtil.FormatMessage(xmlData.SelectSingleNode("/xml/Format").InnerText);
                    recMsg.MsgId = MessageUtil.FormatMessage(xmlData.SelectSingleNode("/xml/MsgId").InnerText);
                    break;
                case "video":
                    recMsg.MediaId = MessageUtil.FormatMessage(xmlData.SelectSingleNode("/xml/MediaId").InnerText);
                    recMsg.ThumbMediaId = MessageUtil.FormatMessage(xmlData.SelectSingleNode("/xml/ThumbMediaId").InnerText);
                    recMsg.MsgId = MessageUtil.FormatMessage(xmlData.SelectSingleNode("/xml/MsgId").InnerText);
                    break;

                case "location":
                    recMsg.Location_X = MessageUtil.FormatMessage(xmlData.SelectSingleNode("/xml/Location_X").InnerText);
                    recMsg.Location_Y = MessageUtil.FormatMessage(xmlData.SelectSingleNode("/xml/Location_Y").InnerText);
                    recMsg.PositionLabel = MessageUtil.FormatMessage(xmlData.SelectSingleNode("/xml/Label").InnerText);
                    recMsg.MapScale = MessageUtil.FormatMessage(xmlData.SelectSingleNode("/xml/Scale").InnerText);
                    recMsg.MsgId = MessageUtil.FormatMessage(xmlData.SelectSingleNode("/xml/MsgId").InnerText);
                    break;
                case "link":
                    recMsg.Title = MessageUtil.FormatMessage(xmlData.SelectSingleNode("/xml/Title").InnerText);
                    recMsg.Description = MessageUtil.FormatMessage(xmlData.SelectSingleNode("/xml/Description").InnerText);
                    recMsg.Url = MessageUtil.FormatMessage(xmlData.SelectSingleNode("/xml/Url").InnerText);
                    break;
                default:
                    recMsg.Content = MessageUtil.FormatMessage(xmlData.SelectSingleNode("/xml/Content").InnerText); 
                    break;
            
            }
            
            return recMsg;
        
        }

        /// <summary>
        /// 转换消息类为回复消息XML字符串
        /// </summary>
        /// <param name="replyMsg"></param>
        /// <returns></returns>
        public string ConvertReplyMessage(object replyMsg)
        {
            if (replyMsg == null)
            {
                return "";
            }
            StringBuilder sb = new StringBuilder();
            WeixinMessage wsMessage = (WeixinMessage)replyMsg;


            switch (wsMessage.MsgType)
            {
                case "text":

                    ReplyMessageText replyTxtMsg = (ReplyMessageText)replyMsg;
                    sb.Append(" <xml>");
                    sb.Append(" <ToUserName><![CDATA[" + replyTxtMsg.ToUserName + "]]></ToUserName>");
                    sb.Append(" <FromUserName><![CDATA[" + replyTxtMsg.FromUserName + "]]></FromUserName>"); ;
                    sb.Append(" <CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>");
                    sb.Append(" <MsgType><![CDATA[text]]></MsgType>");
                    sb.Append(" <Content><![CDATA[" + replyTxtMsg.Content + "]]></Content>");
                    break;
                case "news":
                    ReplyMessageArticle articleMsg = (ReplyMessageArticle)replyMsg;
                    sb.Append(" <xml>");
                    sb.Append(" <ToUserName><![CDATA[" + articleMsg.ToUserName + "]]></ToUserName>");
                    sb.Append(" <FromUserName><![CDATA[" + articleMsg.FromUserName + "]]></FromUserName>"); ;
                    sb.Append(" <CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>");
                    sb.Append(" <MsgType><![CDATA[news]]></MsgType>");
                  
                    sb.Append(" <ArticleCount>"+articleMsg.Articles.Count.ToString()+"</ArticleCount>");

                    if (articleMsg.Articles.Count > 0)
                    {
                        sb.Append(" <Articles>");
                        foreach(MessageArticle artMsg in articleMsg.Articles)
                        {
                            
                            sb.Append(" <item>");
                            sb.Append(" <Title><![CDATA["+artMsg.Title+"]]></Title> ");
                            sb.Append(" <Description><![CDATA["+artMsg.Description+"]]></Description>");
                            sb.Append(" <PicUrl><![CDATA["+artMsg.PicUrl+"]]></PicUrl>");
                            sb.Append(" <Url><![CDATA["+artMsg.Url+"]]></Url>");
                            sb.Append("  </item>");

                        }
                         sb.Append(" </Articles>");
                    }
                    break;
                case "image":
                    ReplyMessageArticle imageMsg = (ReplyMessageArticle)replyMsg;
                    sb.Append(" <xml>");
                    sb.Append(" <ToUserName><![CDATA[" + imageMsg.ToUserName + "]]></ToUserName>");
                    sb.Append(" <FromUserName><![CDATA[" + imageMsg.FromUserName + "]]></FromUserName>"); ;
                    sb.Append(" <CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>");
                    sb.Append(" <MsgType><![CDATA[image]]></MsgType>");

                    sb.Append(" <Image>");
                    foreach (MessageArticle artMsg in imageMsg.Articles)
                    {
                        sb.Append(" <MediaId><![CDATA[" + artMsg.PicUrl + "]]></MediaId>");
                    }
                    sb.Append(" </Image>");

                    break;
                case "music":
                    ReplyMessageMusic musicMsg = (ReplyMessageMusic)replyMsg;
                    sb.Append(" <xml>");
                    sb.Append(" <ToUserName><![CDATA[" + musicMsg.ToUserName + "]]></ToUserName>");
                    sb.Append(" <FromUserName><![CDATA[" + musicMsg.FromUserName + "]]></FromUserName>"); ;
                    sb.Append(" <CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>");
                    sb.Append(" <MsgType><![CDATA[music]]></MsgType>");
                    
                    sb.Append(" <Music>");
                    sb.Append(" <Title><![CDATA["+musicMsg.musics.Title+"]]></Title> ");
                    sb.Append(" <Description><![CDATA["+musicMsg.musics.Description+"]]></Description>");
                    sb.Append(" <MusicUrl><![CDATA["+musicMsg.musics.MusicUrl+"]]></MusicUrl>");
                    sb.Append(" <HQMusicUrl><![CDATA["+musicMsg.musics.HQMusicUrl+"]]></HQMusicUrl>");
                    sb.Append(" </Music>");

                    break;
                case "transfer_customer_service":
                     ReplyMessageText transferMsg = (ReplyMessageText)replyMsg;
                    sb.Append(" <xml>");
                    sb.Append(" <ToUserName><![CDATA[" + transferMsg.ToUserName + "]]></ToUserName>");
                    sb.Append(" <FromUserName><![CDATA[" + transferMsg.FromUserName + "]]></FromUserName>"); ;
                    sb.Append(" <CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>");
                    sb.Append(" <MsgType><![CDATA[transfer_customer_service]]></MsgType>");
                                  
                    break;

                default:
                    ReplyMessageText defaultMsg = (ReplyMessageText)replyMsg;
                    sb.Append(" <xml>");
                    sb.Append(" <ToUserName><![CDATA[" + defaultMsg.ToUserName + "]]></ToUserName>");
                    sb.Append(" <FromUserName><![CDATA[" + defaultMsg.FromUserName + "]]></FromUserName>"); ;
                    sb.Append(" <CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>");
                    sb.Append(" <MsgType><![CDATA[text]]></MsgType>");
                   // sb.Append(" <Content><![CDATA[" + replyMsg. + "]]></Content>");                
                    break;

            }

            sb.Append(" </xml>");
            logger.Debug(sb.ToString());
            return sb.ToString(); ;
        
        }
    }
}

 

好了 最后写这个代码 用来定义自动回复消息
 
using System;
using System.Collections.Generic;
using System.Web;
using ShandsWeixin.model;
using System.Text;
using System.Security.Cryptography;
using Cn.Com.Shijinet.Kunlun.Log;

namespace ShandsWeixin.business
{
    public static class MessageContent
    {

        public static Logger logger = Logger.GetLogger(typeof(MessageContent));
        /// <summary>
        /// 默认消息
        /// </summary>
        /// <param name="recMessage"></param>
        /// <returns></returns>
        public static object getDefaultMessage(ReceiveMessage recMessage)
        {
            return getSubscribeMessage(recMessage);

        }

        public static string getSiteUrl()
        {
            string siteUrl = System.Configuration.ConfigurationManager.AppSettings["siteUrl"];
            if (siteUrl == null)
            {
                siteUrl = "http://m..cn/";
            }
            return siteUrl;
        }

        public static object getClickMessage(ReceiveMessage recMessage)
        {
            switch (recMessage.EventKey)
            {
                case "ABOUT":
                    return getAboutKaiyuan(recMessage);   

                case "CALLCENTER":
                    return getCallCenterMsg(recMessage); 

                case "供需":
                    return getgongxu(recMessage);
               
                default:
                    return MessageContent.getDefaultMessage(recMessage);
            }
        }

        /// <summary>
        /// 关注订阅
        /// </summary>
        /// <param name="recMessage"></param>
        /// <returns></returns>
        public static object getSubscribeMessage(ReceiveMessage recMessage)
        {
            return getMutipleAritcleMessage(recMessage);
        }

        /// <summary>
        /// 取消关注
        /// </summary>
        /// <param name="recMessage"></param>
        /// <returns></returns>
        public static object getUnubscribeMessage(ReceiveMessage recMessage)
        {
            ReplyMessageText replyTextMsg = new ReplyMessageText();
            replyTextMsg.Content = "";
            return replyTextMsg;
        }

        /// <summary>
        /// 根据回复文本内容回复消息
        /// </summary>
        /// <param name="recMessage"></param>
        /// <returns></returns>
        public static object getMessageBySendMsg(ReceiveMessage recMessage)
        {
            if (recMessage.Content.Contains("$"))
            {
                return getPriceService(recMessage);
            }
            else if(recMessage.Content.Contains("@"))
            {
                return getSupplyService(recMessage);
            }
            else
            {
                return getPurchaseService(recMessage);
            }
        }

        /// <summary>
        /// 价格信息
        /// </summary>
        /// <param name="recMessage"></param>
        /// <returns></returns>
        public static object getPriceService(ReceiveMessage recMessage)
        {
            ReplyMessageArticle replyArticleMsg = new ReplyMessageArticle();
            replyArticleMsg.CreateTime = DateTime.Now.Ticks.ToString();
            replyArticleMsg.ToUserName = recMessage.FromUserName;
            replyArticleMsg.FromUserName = recMessage.ToUserName;
            replyArticleMsg.MsgType = "news";
            string openid = recMessage.FromUserName;
            MessageArticle msgArticle = new MessageArticle();
            string keyword = System.Web.HttpUtility.UrlEncode(recMessage.Content.Substring(1, recMessage.Content.Length - 1), System.Text.Encoding.GetEncoding("GB2312"));
            msgArticle.Url = "http://m..cn/price/?keywords=" + keyword;
            logger.Debug(msgArticle.Url);
            msgArticle.PicUrl = "http://wx..cn/img/price.jpg";
            msgArticle.Title = recMessage.Content.Substring(1, recMessage.Content.Length - 1) + "的价格信息";
            msgArticle.Description = "这是花木网您需要查询" + recMessage.Content.Substring(1, recMessage.Content.Length - 1) + "的最新的价格信息";
            replyArticleMsg.Articles = new List<MessageArticle>();
            replyArticleMsg.Articles.Add(msgArticle);
            return replyArticleMsg;
        }

        public static object getSupplyService(ReceiveMessage recMessage)
        {
            ReplyMessageArticle replyArticleMsg = new ReplyMessageArticle();
            replyArticleMsg.CreateTime = DateTime.Now.Ticks.ToString();
            replyArticleMsg.ToUserName = recMessage.FromUserName;
            replyArticleMsg.FromUserName = recMessage.ToUserName;
            replyArticleMsg.MsgType = "news";
            string openid = recMessage.FromUserName;
            MessageArticle msgArticle = new MessageArticle();
            string keyword = System.Web.HttpUtility.UrlEncode(recMessage.Content.Substring(1, recMessage.Content.Length - 1), System.Text.Encoding.GetEncoding("GB2312"));
            msgArticle.Url = "http://m..cn/sell/?keywords=" + keyword;
            msgArticle.PicUrl = "http://wx..cn/img/supply.jpg";
            msgArticle.Title = recMessage.Content.Substring(1, recMessage.Content.Length - 1) + "的供应信息";
            msgArticle.Description = "这是花木网您需要查询" + recMessage.Content.Substring(1, recMessage.Content.Length - 1) + "的最新的供应信息";
            replyArticleMsg.Articles = new List<MessageArticle>();
            replyArticleMsg.Articles.Add(msgArticle);
            return replyArticleMsg;
        }

        public static object getPurchaseService(ReceiveMessage recMessage)
        {
            ReplyMessageArticle replyArticleMsg = new ReplyMessageArticle();
            replyArticleMsg.CreateTime = DateTime.Now.Ticks.ToString();
            replyArticleMsg.ToUserName = recMessage.FromUserName;
            replyArticleMsg.FromUserName = recMessage.ToUserName;
            replyArticleMsg.MsgType = "news";
            string openid = recMessage.FromUserName;
            MessageArticle msgArticle = new MessageArticle();
            string keyword = System.Web.HttpUtility.UrlEncode(recMessage.Content, System.Text.Encoding.GetEncoding("GB2312"));
            msgArticle.Url = "http://m..cn/buy/?keywords=" + keyword;
            logger.Debug(msgArticle.Url);
            msgArticle.PicUrl = "http://wx..cn/img/purchase.jpg";
            msgArticle.Title = recMessage.Content + "的采购信息";
            msgArticle.Description = "这是花木网您需要查询" + recMessage.Content + "的最新的采购信息";
            replyArticleMsg.Articles = new List<MessageArticle>();
            replyArticleMsg.Articles.Add(msgArticle);
            return replyArticleMsg;
        }

        public static object getTransferService(ReceiveMessage recMessage)
        {
            ReplyMessageText replyArticleMsg = new ReplyMessageText();
            replyArticleMsg.CreateTime = DateTime.Now.Ticks.ToString();
            replyArticleMsg.ToUserName = recMessage.FromUserName;
            replyArticleMsg.FromUserName = recMessage.ToUserName;
            replyArticleMsg.MsgType = "transfer_customer_service";
            return replyArticleMsg;
        }

        /// <summary>
        /// 多图文消息(关注获取)
        /// </summary>
        /// <param name="recMessage"></param>
        /// <returns></returns>
        public static object getMutipleAritcleMessage(ReceiveMessage recMessage)
        {
            ReplyMessageArticle replyArticleMsg = new ReplyMessageArticle();
            replyArticleMsg.CreateTime = DateTime.Now.Ticks.ToString();
            replyArticleMsg.ToUserName = recMessage.FromUserName;
            replyArticleMsg.FromUserName = recMessage.ToUserName;
            replyArticleMsg.MsgType = "news";
            replyArticleMsg.Articles = new List<MessageArticle>();
            string openid = recMessage.FromUserName;
            MessageArticle msgArticle = new MessageArticle();

            msgArticle.Url = "http://m.huamu.cn";
            msgArticle.PicUrl = "http://m..com/ShandsWeixin/img/kyzzz.jpg";
            msgArticle.Title = "多图文标题1";
            msgArticle.Description = "多图文内容1";
            replyArticleMsg.Articles.Add(msgArticle);

            msgArticle = new MessageArticle();
            msgArticle.Url = "http://m..cn";
            msgArticle.PicUrl = "http://m..com/ShandsWeixin/img/AirHotel.jpg";
            msgArticle.Title = "多图文标题1";
            msgArticle.Description = "多图文内容2";
            replyArticleMsg.Articles.Add(msgArticle);

            msgArticle = new MessageArticle();
            msgArticle.Url = "http://m..cn";
            msgArticle.PicUrl = "http://m..com/ShandsWeixin/img/weixin120_2.jpg";
            msgArticle.Title = "多图文标题3";
            msgArticle.Description = "多图文内容3\r\n多图文内容3";

            replyArticleMsg.Articles.Add(msgArticle);
            return replyArticleMsg;
        }

        public static string GetMD5(string str)
        {
            string strRe = "";

            strRe = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToString();

            return strRe.ToLower().Trim();
        }

        public static string GetMD5(string CryptoString, Encoding encoding)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] fromData = encoding.GetBytes(CryptoString);
            byte[] targetData = md5.ComputeHash(fromData);
            string byte2String = null;

            for (int i = 0; i < targetData.Length; i++)
            {
                byte2String += targetData[i].ToString("x");
            }

            return byte2String;
        }

        public static WeixinMember.Members GetMemberByExtenalId(string sourceType, string extenalOpenId)
        {
            WeixinMember.Members wxMember = null;
            logger.Debug("wxMember.sourceType:" + sourceType + ";extenalOpenId" + extenalOpenId);
            try
            {
                if (sourceType == null || sourceType == "")
                {
                    sourceType = "weixin";
                }
                WeixinMember.SourceType bindExtenalService = new ShandsWeixin.WeixinMember.SourceType();
                wxMember = bindExtenalService.GetMemberByOpenID(sourceType, extenalOpenId);
                logger.Debug("wxMember.ResultCode:" + wxMember.ResultCode);



            }
            catch (Exception ex)
            {
                logger.Debug("wxMember.e:" + ex.Message);
            }
            return wxMember;
        }


        public static object getVerifGwMessage(ReceiveMessage recMessage)
        {

            ReplyMessageArticle replyArticleMsg = new ReplyMessageArticle();
            replyArticleMsg.CreateTime = DateTime.Now.Ticks.ToString();
            replyArticleMsg.ToUserName = recMessage.FromUserName;
            replyArticleMsg.FromUserName = recMessage.ToUserName;
            replyArticleMsg.MsgType = "verifygw";

            return replyArticleMsg;
        }
    }
}

比如这样
{
     "button":[
     { 
            "name":"商祺会",
           "sub_button":[
           { 
               "type":"click",
               "name":"加入商祺会",
               "key":"REGIST"
            },
           { 
               "type":"click",
               "name":"我的礼遇",
               "key":"RECEPTION"
            },
           { 
               "type":"click",
               "name":"余额积分",
               "key":"BANLANCE"
            },
           { 
               "type":"click",
               "name":"我的订单",
               "key":"RESERVATION"
            },
            {
               "type":"view",
               "name":"关于商祺会",
               "url":"http://m..com/Content.aspx"
            }
            ]
      },
      {
            "name":"酒店预订",
           "sub_button":[
            {
               "type":"click",
               "name":"主题房抢购",
               "key":"THEME"               
            },
            {
               "type":"click",
               "name":"智造",
               "key":"EBKY"
            },
            {
               "type":"click",
               "name":"订房立减80元",
               "key":"RESERVE"
            },
            {
               "type":"click",
               "name":"精品自由行",
               "key":"FREELINE"
            },
            {
               "type":"click",
               "name":"电话预订",
               "key":"CALLCENTER"
            }]
      },
      {
           "name":"更多...",
           "sub_button":[
            {
                "type":"click",
                "name":"关注有礼",
                "key":"LOTTERY"
            },
            {
                "type":"click",
                "name":"酒店加盟",
                "key":"JOIN"
            },
            {
               "type":"click",
               "name":"关怀",
               "key":"CARE"
            },
            {
               "type":"click",
               "name":"人才招募",
               "key":"CAREER"
            },
            {
               "type":"click",
               "name":"关于",
               "key":"ABOUT"
            }
           ]
       }]
 }

最后是自动获取openid outher2 ?????是自己的
jObject 引用了northonsoftjson

private string GetToken(string code)
    {
        string res = "";
        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("https://api.weixin.qq.com/sns/oauth2/access_token?appid=??????&secret=?????&code=code&grant_type=authorization_code");
        req.Method = "GET";
        using (WebResponse wr = req.GetResponse())
        {
            HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
            string content = reader.ReadToEnd();
            var jObject = JObject.Parse(content);
            res = jObject["openid"].ToString();
        }
        return res;
    }
  

  

 

 
  
 
  
 
 
 

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论