你的位置:首页 > ASP.net教程

[ASP.net教程]微信开发第二篇

接上篇,看Response

  /// <summary>
        /// 获取用户发送的消息
        /// </summary>
        /// <param name="postString"></param>
        private void Response        {
            //使用                       
           
           
            Request            request            request            request            request

 

   
            ///对消息的不同类型进行赋值
            if (request            {
                //赋值文本信息内容
                request
            }
            if (request            {
                ///赋值地理位置纬度,经度,地图缩放比例,地理位置说明
                request                request                request                request            }
            if (request            {
                ///赋值事件名称和事件key值
                request                request
            }
            ResponseMsg(request

}

 

request

 /// <summary>
    /// 接收消息的实体类
    /// </summary>
    public class Request    {
        private String toUserName = String.Empty;        /// <summary>
        /// 本公众号
        /// </summary>
        public String ToUserName{get;set;}        /// <summary>
        /// 用户微信号
        /// </summary>
        public String FromUserName{get;set;}        /// <summary>
        /// 创建时间
        /// </summary>
        public String CreateTime{get;set;}        /// <summary>
        /// 信息类型
        /// </summary>
        public String MsgType{get;set;}        /// <summary>
        /// 信息内容
        /// </summary>
        public String Content{get;set;}        /*以下为事件类型的消息特有的属性*/
        /// <summary>
        /// 事件名称
        /// </summary>
        public String EventName{get;set;}
        /// <summary>
        /// 事件值
        /// </summary>
        public string EventKey { get; set; }
        /*以下为图文类型的消息特有的属性*/
        /// <summary>
        /// 图文消息的个数
        /// </summary>
        public int ArticleCount { get; set; }
        /// <summary>
        /// 图文消息的标题
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// 图文消息的简介
        /// </summary>
        public string Description { get; set; }
        /// <summary>
        /// 图文消息图片的链接地址
        /// </summary>
        public string PicUrl { get; set; }
        /// <summary>
        /// 图文消息详情链接地址
        /// </summary>
        public string Url { get; set; }
        /// <summary>
        /// 图文消息集合
        /// </summary>
        public List<Request
        /*以下为地理位置类型的消息特有的属性*/
        /// <summary>
        /// 地理位置纬度
        /// </summary>
        public String Location_X { get; set; }        /// <summary>
        /// 地理位置经度
        /// </summary>
        public String Location_Y { get; set; }        /// <summary>
        /// 地图缩放比例
        /// </summary>
        public String Scale { get; set; }        /// <summary>
        /// 地图位置说明
        /// </summary>
        public String Label { get; set; }
     
    }

 

继续关注  ResponseMsg(request

 private void ResponseMsg(Request        {
            string MsgType = request
            try
            {
                //根据消息类型判断发送何种类型消息
                switch (MsgType)
                {
                    case "text":
                        SendTextCase(request                        break;
                    case "event"://发送事件消息
                        if (!string.IsNullOrWhiteSpace(request                        {
                            SendWelComeMsg(request                        }
                        else if (!string.IsNullOrWhiteSpace(request                        {
                            SendEventMsg(request                        }
                        break;
                    case "location"://发送位置消息
                        SendMapMsg(request                        break;
                    default:
                        break;                }
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write(ex.ToString());
            }
        }

 

先来关注发送文本消息,SendTextCase(request

 /// <summary>
        /// 发送文本
        /// </summary>
        /// <param name="request        private void SendTextCase(Request        {
                    string responseContent = FormatText
                    HttpContext.Current.Response.ContentType = "text/                    HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
                    HttpContext.Current.Response.Write(responseContent);
                    HttpContext.Current.Response.End();
        }

 

FormatText

  /// <summary>
        /// 返回格式化的        /// </summary>
        /// <param name="p1">公众号</param>
        /// <param name="p2">用户号</param>
        /// <param name="p3">回复内容</param>
        /// <returns></returns>
        private string FormatText        {
            return "<        }

 

这样就能实现消息的应答,如果用户点击的按钮,如下代码:

    case "event"://发送事件消息
                        if (!string.IsNullOrWhiteSpace(request                        {
                            SendWelComeMsg(request                        }
                        else if (!string.IsNullOrWhiteSpace(request                        {
                            SendEventMsg(request                        }
                        break;

 

 /// <summary>
        /// 发送响应事件消息
        /// </summary>
        /// <param name="request        private void SendEventMsg(Request        {
            string keyStr = request
            switch (keyStr)
            {
                case "mypay":
                    SendPayDetails(request                    break;
                case "tianqiyubao":
                    SendWeaterMessage(request                    break;
                case "kaixinyixiao":
                    SendKaiXinMessage(request                    break;
                case  "updateMessage":
                    SendUpdateMessage(request                    break;
                case "yuangonghuodong":
                    SendYuanGongHuoDong(request                    break;
                case "yuangongtongzhi":
                    SendYuanGongTongZhi(request                    break;
                case "youwenbida":
                    SendWenti(request                    break;
               case "mywen":
                    SendWentiList(request                    break;
               case "PhoneSerices":
                    SendKeFuMessage(request                    break;
                default:
                    String responseContent = String.Empty;
                    responseContent = FormatText                    HttpContext.Current.Response.ContentType = "text/                    HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
                    HttpContext.Current.Response.Write(responseContent);
                    HttpContext.Current.Response.End();
                    break;
            }
        }

 

SendWelComeMsg(request

  /// <summary>
        /// 发送关注时的图文消息
        /// </summary>
        /// <param name="request        private void SendWelComeMsg(Request        {
            String responseContent = String.Empty;            string newdate = DateTime.Now.Subtract(new DateTime(1970, 1, 1, 8, 0, 0)).TotalSeconds.ToString();
            string PUrlfileName = "http://www.deqiaohr.com.cn/weixin/welcome.jpg";            responseContent = string.Format(Message_News_Main, request                string.Format(Message_News_Item, "欢迎关注德桥员工服务中心", "苏州德桥人力资源创立于2002年...", PUrlfileName, "http://www.deqiaohr.com.cn/weixin/WxGsjianjie.aspx"));
            HttpContext.Current.Response.ContentType = "text/            HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
            HttpContext.Current.Response.Write(responseContent);
            HttpContext.Current.Response.End();
        }

 

Message_News_Main 和Message_News_Item是图文消息格式化

  /// <summary>
        /// 返回图文消息主体
        /// </summary>
        public static string Message_News_Main
        {
            get
            {
                return @"<                            <ToUserName><![CDATA[{0}]]></ToUserName>
                            <FromUserName><![CDATA[{1}]]></FromUserName>
                            <CreateTime>{2}</CreateTime>
                             <MsgType><![CDATA[news]]></MsgType>
                             <ArticleCount>{3}</ArticleCount>
                             <Articles>
                             {4}
                           </Articles>
                           </            }
        }
        /// <summary>
        /// 返回图文消息项
        /// </summary>
        public static string Message_News_Item
        {
            get
            {
                return @"<item>
                            <Title><![CDATA[{0}]]></Title>
                            <Description><![CDATA[{1}]]></Description>
                            <PicUrl><![CDATA[{2}]]></PicUrl>
                            <Url><![CDATA[{3}]]></Url>
                            </item>";
            }
        }

 

点击查看全文,直接跳转至自己写的网页,关于网页图片自适应的问题,把图片的宽度设置为100%,即可,如果让网页在屏幕中自适应,记得在网页中一定要添加

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;"/>