Exemplo n.º 1
0
        /// <summary>
        /// 处理文本消息
        /// </summary>
        private void GetWXContent(RequestXML requestXML, int id)
        {
            try
            {
                if (requestXML == null)
                {
                    return;
                }
                if (string.IsNullOrEmpty(requestXML.Content) || string.IsNullOrEmpty(requestXML.ToUserName))
                {
                    return;
                }

                string strXml = string.Empty;
                string where = string.Format("Types='WxAmAway' and ServerId='{0}' and Keywords='{1}'", requestXML.ToUserName, requestXML.Content);
                WxAmAway      wxaway = new WxAmAwayBLL().GetModel(where);
                List <WxNews> ls     = null;
                if (wxaway == null)
                {
                    wxaway = new WxAmAway();
                }
                ls = new WxNewsBLL().GetList("IsShow=1 and groupid='" + wxaway.GroupId + "'");
                List <WXMesage> msgList = new List <WXMesage>();
                if (ls == null || ls.Count == 0)
                {
                    string config             = System.Configuration.ConfigurationManager.AppSettings["wxsearchapi"];
                    string api                = string.Format(config, id, HttpUtility.UrlEncode(requestXML.Content));
                    string json               = WxHelper.HttpGet(api);
                    JavaScriptSerializer jss  = new JavaScriptSerializer();
                    List <Message>       list = jss.Deserialize <List <Message> >(json);
                    if (list != null && list.Count > 0)
                    {
                        foreach (Message model in list)
                        {
                            WXMesage msg = new WXMesage();
                            msg.Title       = FilterHandler.FilterHtml(model.bestFragment); //图文标题
                            msg.Description = FilterHandler.FilterHtml(model.bestFragment); //图文简介
                            msg.Url         = model.wapUrl;                                 //图文推送连接
                            msg.PicUrl      = MessageBLL.GetPicUrl(model);                  //图文背景
                            msg.Title       = msg.Title.Length >= 36 ? msg.Title.Substring(0, 36) : msg.Title;
                            msg.Description = msg.Description.Length >= 36 ? msg.Description.Substring(0, 36) : msg.Description;
                            if (string.IsNullOrEmpty(msg.PicUrl))
                            {
                                msg.PicUrl = model.headImgUrl;
                            }
                            msg.MsgType = "news";
                            if (!string.IsNullOrEmpty(msg.Title))
                            {
                                msgList.Add(msg);
                            }
                        }
                        if (msgList.Count > 0)
                        {
                            msgList = msgList.OrderByDescending(p => p.PicUrl).Take(7).ToList();
                            msgList.Add(new WXMesage()
                            {
                                Title = "查看更多", Url = WebSiteConfig.WsqUrl + "/f/s-" + id
                            });
                        }
                    }
                    else
                    {
                        log4net.LogHelper.WriteInfo(this.GetType(), "json转换失败" + msgList.Count);
                    }
                }
                foreach (WxNews model in ls)
                {
                    WXMesage msg     = new WXMesage();
                    string   imgpath = string.Empty;
                    if (model.PathFile != null && model.PathFile.IndexOf("http://") > -1)
                    {
                        imgpath = string.IsNullOrEmpty(model.PathFile) ? "" : model.PathFile;
                    }
                    else
                    {
                        imgpath = string.IsNullOrEmpty(model.PathFile) ? "" : WebSiteConfig.SourceContent + model.PathFile;
                    }
                    msg.Content     = model.Content;
                    msg.MsgType     = model.MsgType;
                    msg.Title       = model.Title; //图文标题
                    msg.Url         = model.Urls;  //图文推送连接
                    msg.PicUrl      = imgpath;     //图文背景
                    msg.Title       = msg.Title.Length >= 36 ? msg.Title.Substring(0, 36) : msg.Title;
                    msg.Description = model.Description.Length >= 36 ? model.Description.Substring(0, 36) : model.Description;
                    msg.MediaId     = model.mediaUrl;
                    msgList.Add(msg);
                }
                //log4net.LogHelper.WriteInfo(this.GetType(), "msgList.Count=>" + msgList.Count);
                if (msgList.Where(p => p.MsgType == "news").Count() > 0)//图文
                {
                    strXml = AmAwayApi.GetNewsXml(requestXML.FromUserName, requestXML.ToUserName, msgList.Take(10).ToList());
                }
                if (msgList.Where(p => p.MsgType == "text").Count() > 0)//文本
                {
                    strXml = AmAwayApi.GetTextXml(requestXML.FromUserName, requestXML.ToUserName, msgList[0].Content);
                }
                if (msgList.Where(p => p.MsgType == "image").Count() > 0)//图片
                {
                    strXml = AmAwayApi.GetImageXml(requestXML.FromUserName, requestXML.ToUserName, msgList[0].MediaId);
                }
                if (msgList.Where(p => p.MsgType == "audio").Count() > 0)//语音
                {
                    strXml = AmAwayApi.GetVoiceXml(requestXML.FromUserName, requestXML.ToUserName, msgList[0].MediaId);
                }
                if (msgList.Where(p => p.MsgType == "video").Count() > 0)//视频
                {
                    strXml = AmAwayApi.GetVideoXml(requestXML.FromUserName, requestXML.ToUserName, msgList[0].MediaId, msgList[0].Title, msgList[0].Description);
                }
                if (!string.IsNullOrEmpty(strXml))
                {
                    HttpContext.Current.Response.Output.Write(strXml);
                }
            }
            catch (Exception ex)
            {
                log4net.LogHelper.WriteError(this.GetType(), ex);
            }
        }