Esempio n. 1
0
        private string GetWxHtml(Wx_App app, Wx_News news)
        {
            var html   = new HtmlDocument();
            var sysCfg = configService.GetConfig(app.Id);

            var content = news.Content.Replace(vars);
            var header  = sysCfg["header_html"];
            var footer  = sysCfg["footer_html"];
            var cover   = "<p><img src=\"" + news.ThumbWxUrl + "\" /></p>";

            html.LoadHtml("<div>" + header + cover + content + footer + "</div>");

            var nodes = html.DocumentNode.SelectNodes("./div/p | ./div/div");

            if (nodes != null)
            {
                foreach (HtmlNode item in nodes)
                {
                    item.InnerHtml = item.InnerHtml.Trim();
                    SetStyle(item, "padding:0.25em 0");

                    var subImgs = item.SelectNodes(".//img");
                    if (subImgs == null || subImgs.Count <= 0)
                    {
                        var attr = item.Attributes["style"];
                        if (attr == null || attr.Value.IndexOf("text-align:center") < 0)
                        {
                            SetStyle(item, "text-indent:2em");
                        }
                    }
                    if (!item.HasChildNodes)
                    {
                        item.Remove();
                    }
                }
            }

            var imgs = html.DocumentNode.SelectNodes("//img");

            if (imgs != null)
            {
                foreach (HtmlNode img in imgs)
                {
                    var src    = img.Attributes["src"].Value;
                    var result = WeChatApi.UploadImg(app.AppId, src);
                    if (result != null)
                    {
                        img.Attributes["src"].Value = result.url;
                    }
                    img.Attributes.Remove("width");
                    img.Attributes.Remove("height");

                    SetStyle(img, "width:100%!important;height:auto!important");
                }
            }
            return(html.DocumentNode.InnerHtml);
        }
        /// <summary>
        /// 创建图文消息素材
        /// </summary>
        /// <param name="fileid"></param>
        /// <returns></returns>
        public string CreateData(string fileid)
        {
            var file = Broker.Retrieve <sys_file>(fileid);
            var data = Broker.Retrieve <wechat_news_material>("SELECT * FROM wechat_news_material WHERE fileid = @id", new Dictionary <string, object>()
            {
                { "@id", fileid }
            });

            if (data == null)
            {
                var stream = ServiceContainer.Resolve <IStoreStrategy>(StoreConfig.Config.Type).GetStream(fileid);
                data = new wechat_news_material()
                {
                    Id     = Guid.NewGuid().ToString(),
                    fileid = fileid
                };
                data.media_url = WeChatApi.UploadImg(stream, file.name, file.content_type)?.url;
                data.local_url = SysFileService.GetDownloadUrl(fileid, false);
                CreateData(data);
            }
            return(data.media_url);
        }