C#开发微信门户及应用(3)--文本消息和图文消息的应答

来源:互联网 发布:上交所龙虎榜数据 编辑:程序博客网 时间:2024/05/14 13:01
消息基类BaseMessage的实体类定义如下所示,它对日期构造了一个整形数值,并具备了一些常规的属性,并且还有一个重要的ToXML方法,用来给方法传递这些XML数据的。
复制代码
    /// <summary>
    /// 基础消息内容
    /// </summary>
    [XmlRoot(ElementName = "xml")]
    public class BaseMessage
    {
        /// <summary>
        /// 初始化一些内容,如创建时间为整形,
        /// </summary>
        public BaseMessage()
        {
            this.CreateTime = DateTime.Now.DateTimeToInt();
        }
        /// <summary>
        /// 开发者微信号
        /// </summary>
        public string ToUserName { get; set; }
        /// <summary>
        /// 发送方帐号(一个OpenID)
        /// </summary>
        public string FromUserName { get; set; }
        /// <summary>
        /// 消息创建时间 (整型)
        /// </summary>
        public int CreateTime { get; set; }
        /// <summary>
        /// 消息类型
        /// </summary>
        public string MsgType { get; set; }
        public virtual string ToXml()
        {
            this.CreateTime = DateTime.Now.DateTimeToInt();//重新更新
            return MyXmlHelper.ObjectToXml(this);
        }
    }
复制代码
回复的文本消息实体类代码如下所示,我们可以看到,它继承了很多通用的实体属性,并且还具备了一个ToXml的通用方法,我们需要把它转换为响应的XML的时候,就使用这个方法就可以了。
复制代码
    /// <summary>
    /// 回复文本消息
    /// </summary>
    [System.Xml.Serialization.XmlRoot(ElementName = "xml")]
    public class ResponseText : BaseMessage
    {
        public ResponseText()
        {
            this.MsgType = ResponseMsgType.Text.ToString().ToLower();
        }
        public ResponseText(BaseMessage info) : this()
        {
            this.FromUserName = info.ToUserName;
            this.ToUserName = info.FromUserName;
        }
        /// <summary>
        /// 内容
        /// </summary>       
        public string Content  { get; set; }
    }
复制代码
而图文消息对象类ResponseNews,它包含更多的信息定义
复制代码
    /// <summary>
    /// 回复图文消息
    /// </summary>
    [System.Xml.Serialization.XmlRoot(ElementName = "xml")]
    public class ResponseNews : BaseMessage
    {
        public ResponseNews()
        {
            this.MsgType = ResponseMsgType.News.ToString().ToLower();
            this.Articles = new List<ArticleEntity>();
        }
        public ResponseNews(BaseMessage info) : this()
        {
            this.FromUserName = info.ToUserName;
            this.ToUserName = info.FromUserName;
        }
        /// <summary>
        /// 图文消息个数,限制为10条以内
        /// </summary>
        public int ArticleCount
        {
            get
            {
                return this.Articles.Count;
            }
            set
            {
                ;//增加这个步骤才出来XML内容
            }
        }
        /// <summary>
        /// 图文列表。
        /// 多条图文消息信息,默认第一个item为大图,注意,如果图文数超过10,则将会无响应
        /// </summary>
        [System.Xml.Serialization.XmlArrayItem("item")]
        public List<ArticleEntity> Articles { get; set; }
    }
复制代码
而其中的图文列表集合中的对象,它也是一个实体类型,包含了一些图文的链接,标题等信息,不在赘述。
2、消息的回复处理
如对于文本消息,我们可以用以下的方式进行处理。
                ResponseText response = new ResponseText(info);
                response.Content = "抱歉,此功能暂未开通。";
                result = response.ToXml();
对于图文消息,我们可能需要录入更多的消息才能返回更好的效果。
注意图文的消息,图片的尺寸最好按照官方的标准,否则在手机上看起来不好看,官方的标准好像是宽高是(360,200)像素
复制代码
        /// <summary>
        /// 订阅或者显示公司信息
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        private string ShowCompanyInfo(BaseMessage info)
        {
            string result = "";
            //使用在微信平台上的图文信息(单图文信息)
            ResponseNews response = new ResponseNews(info);
            ArticleEntity entity = new ArticleEntity();
            entity.Title = "广州爱奇迪软件科技有限公司";
            entity.Description = "欢迎关注广州爱奇迪软件--专业的单位信息化软件和软件开发框架提供商,我们立志于为客户提供最好的软件及服务。\r\n";
            entity.Description += "我们是一家极富创新性的软件科技公司,从事研究、开发并销售最可靠的、安全易用的技术产品及优质专业的服务,帮助全球客户和合作伙伴取得成功。\r\n......(此处省略1000字,哈哈)";
            entity.PicUrl = "http://www.iqidi.com/WeixinImage/company.png";
            entity.Url = "http://www.iqidi.com";
            response.Articles.Add(entity);
            result = response.ToXml();
            return result;
        }

复制代码

http://s9ufsfo.blog.com
http://cw8yle1v.blog.com
http://m3e520.blog.com
http://2rhf27.blog.com
http://si2b5l8.blog.com
http://34n53v.blog.com
http://4g93gn.blog.com
http://s2uq04i.blog.com
http://b6j225hy.blog.com
http://ax0x48.blog.com
http://7i15v96.blog.com
http://dpa4q6o6.blog.com
http://3ad694.blog.com
http://266lwgx.blog.com
http://4o5fnyop.blog.com
http://vvdeso.blog.com
http://512xij.blog.com
http://n7z54r8h.blog.com
http://ab746w3.blog.com
http://4320laf.blog.com
http://s590iqh.blog.com
http://c7925bq.blog.com
http://tbq6o1.blog.com
http://2l1v65qd.blog.com
http://y1pt6j0u.blog.com
http://8jd4681.blog.com
http://80q33ch.blog.com
http://59857u.blog.com
http://3w4u8l0.blog.com
http://y440k4w.blog.com

0 0
原创粉丝点击