public IActionResult Save(cms_template dto)
        {
            if (dto.pid == 0)
            {
                dto.pid = 1;
            }
            dto.template_mode = dto.pid;
            dto.file_name     = dto.file_name ?? "";

            var user = RequestHelper.AdminInfo();

            if (dto.id == 0)
            {
                dto.insert_id   = user.Id;
                dto.insert_time = DateTime.Now;
                dto.edit_id     = user.Id;
                dto.edit_time   = dto.insert_time;
                dto             = _templateApp.InsertTemplate(dto);
            }
            else
            {
                dto.edit_id   = user.Id;
                dto.edit_time = DateTime.Now;
                dto           = _templateApp.UpdateTemplate(dto);
            }
            if (dto != null)
            {
                TemplateManagerCache.AddTemplate(dto);
            }
            GenerateTemplate.Create(dto.template_mode, dto.template_file, dto.template_content);
            return(Success("模板保存成功"));
        }
Exemplo n.º 2
0
 public static void ClearTemplates()
 {
     _defaultTemplates[1] = new cms_template();
     _defaultTemplates[2] = new cms_template();
     _defaultTemplates[3] = new cms_template();
     _templates.Clear();
 }
Exemplo n.º 3
0
        public cms_template InsertTemplate(cms_template dto)
        {
            var count = Sqldb.Select <cms_template>().Where(s => s.pid == dto.pid && s.is_default == 1).Count();

            if (count == 0)
            {
                dto.is_default = 1;
            }

            Sqldb.Insert(dto).ExecuteAffrows();
            return(dto);
        }
Exemplo n.º 4
0
        public cms_template GetModel(int id)
        {
            if (id == 0)
            {
                return(new cms_template());
            }
            var model = Sqldb.Select <cms_template>().Where(s => s.id == id).First();

            if (model == null)
            {
                model = new cms_template();
            }
            return(model);
        }
Exemplo n.º 5
0
        public cms_template UpdateTemplate(cms_template dto)
        {
            var oldTemp = Sqldb.Select <cms_template>()
                          .Where(s => s.id == dto.id).First(s => new cms_template
            {
                id            = s.id,
                template_name = s.template_name,
                template_file = s.template_file,
                is_default    = s.is_default
            });

            if (oldTemp == null)
            {
                return(null);
            }
            dto.is_default = oldTemp.is_default;
            Sqldb.Update <cms_template>().SetSource(dto).IgnoreColumns(s => new { s.insert_id, s.insert_time }).ExecuteAffrows();
            return(dto);
        }
 /// <summary>
 /// 添加栏目
 /// </summary>
 /// <param name="channel"></param>
 public static void AddTemplate(cms_template template)
 {
     if (template.is_default == 1)
     {
         if (template.template_mode == 2)
         {
             _defaultTemplates[2] = template;
         }
         else if (template.template_mode == 3)
         {
             _defaultTemplates[3] = template;
         }
     }
     if (_templates.ContainsKey(template.id))
     {
         _templates[template.id] = template;
         return;
     }
     _templates.TryAdd(template.id, template);
 }
Exemplo n.º 7
0
        public IActionResult Save(cms_template dto)
        {
            if (dto.pid == 0)
            {
                dto.pid = 1;
            }
            if (dto.template_content.IsEmpty())
            {
                return(Error("模板内容不能为空"));
            }
            dto.template_mode = dto.pid;
            dto.file_name     = dto.file_name ?? "";

            var user = RequestHelper.AdminInfo();

            dto.update_by   = user.LoginName;
            dto.update_time = DateTime.Now;
            if (dto.id == 0)
            {
                dto.create_by   = user.LoginName;
                dto.create_time = DateTime.Now;

                dto = _templateApp.InsertTemplate(dto);
            }
            else
            {
                dto = _templateApp.UpdateTemplate(dto);
            }
            if (dto != null)
            {
                TemplateManagerCache.AddTemplate(dto);
                RenderDocumentCache.Clear();
            }
            GenerateTemplate.Create(dto.template_mode, dto.template_file, dto.template_content);
            return(Success("模板保存成功"));
        }
Exemplo n.º 8
0
        /// <summary>
        /// 生成静态文档,channelId和contentId都为0的时候生成首页
        /// </summary>
        /// <param name="contentId">文章id</param>
        /// <param name="channelId">栏目id</param>
        /// <param name="channelPage">栏目页的分页页面</param>
        /// <returns></returns>
        public GenerateResultDto GenerateHtml(int contentId, int channelId, int channelPage)
        {
            GenerateResultDto ret = new GenerateResultDto();

            try
            {
                //获取模板信息
                cms_template templateModel = null;
                ChannelModel channelModel  = null;
                ContentModel contentModel  = null;
                if (channelId == 0 && contentId == 0)
                {
                    templateModel = TemplateManagerCache.GetHomeTemplate();
                    RenderMode    = 1;
                }
                else if (channelId > 0)
                {
                    channelModel  = generateApp.GetChannel(channelId);
                    templateModel = ChannelManagerCache.GetChannelTemplate(channelId);
                    if (channelModel == null)
                    {
                        ret.Message = "栏目数据查找失败";
                        return(ret);
                    }
                    RenderMode = 2;
                }
                else if (contentId > 0)
                {
                    contentModel  = generateApp.GetContentInfo(contentId);
                    channelModel  = generateApp.GetChannel(contentModel.channel_id);
                    templateModel = ChannelManagerCache.GetContentTemplate(contentModel.channel_id);

                    if (contentModel == null)
                    {
                        ret.Message = "内容数据查找失败";
                        return(ret);
                    }

                    RenderMode = 3;
                }

                if (templateModel == null || templateModel.id == 0)
                {
                    throw new Exception("找不到模板");
                }

                //加载模板 先取缓存,没有再初始化一个并且加入缓存
                this.Document = RenderDocumentCache.GetRenderDocument(templateModel.id);
                if (this.Document == null)
                {
                    string templateFile = Path.Combine(GlobalContext.WebRootPath, templateModel.template_file);
                    this.Document = new TemplateDocument(templateModel.template_content, GlobalContext.WebRootPath, templateFile);
                    RenderDocumentCache.AddRenderDocument(templateModel.id, this.Document);
                }

                #region 公用数据
                //加入基本信息
                this.Document.Variables.SetValue("this", this);
                //站点基本信息
                var site = SiteManagerCache.GetSiteInfo();
                this.Document.Variables.SetValue("site", site);
                //添加所有导航条数据
                var channels = generateApp.GetChannelTree();
                this.Document.Variables.SetValue("channels", channels);

                //解析模板中的栏目文章模板
                ElementCollection <Template> templateContents = this.Document.GetChildTemplatesByName("contents");
                foreach (Template template in templateContents)
                {
                    //从第几条数据开始
                    string startNum = template.Attributes.GetValue("startNum", "1");
                    string total    = template.Attributes.GetValue("total", "10");
                    //根据模板块里定义的type属性条件取得新闻数据
                    var data = generateApp.GetContentSummary(template.Attributes.GetValue("channelIndex"), int.Parse(startNum), int.Parse(total));
                    //设置变量newsdata的值
                    template.Variables.SetValue("contents", data);
                }

                //解析模板中的channel标签
                ElementCollection <Template> templateChannel = this.Document.GetChildTemplatesByName("channel");
                foreach (Template template in templateChannel)
                {
                    string channelIndex = template.Attributes.GetValue("channelIndex", "#");
                    //根据模板块里定义的type属性条件取得新闻数据
                    var channel = ChannelManagerCache.GetChannelByIndex(channelIndex);
                    if (channel != null)
                    {
                        ChannelModel inChannelModel = generateApp.GetChannel(channel.id);
                        //设置变量newsdata的值
                        template.Variables.SetValue("channel", inChannelModel);
                    }
                }
                #endregion

                //渲染首页
                if (RenderMode == 1)
                {
                    string contentFilePath = Path.Combine(GlobalContext.WebRootPath, "index.html");
                    using (var filestream = new FileStream(contentFilePath, FileMode.Create, FileAccess.ReadWrite))
                    {
                        string renderHtml = this.Document.GetRenderText();

                        using (StreamWriter writer = new StreamWriter(filestream, Encoding.UTF8))
                        {
                            writer.WriteLine(renderHtml);
                            writer.Flush();
                        }
                    }

                    ret.Status = true;
                    return(ret);
                }
                //栏目数据
                if (RenderMode == 2)
                {
                    //获取当前栏目下的文章列表
                    //解析文章列表模板设置数据源 添加id为contents 模板
                    Tag element  = this.Document.GetChildTagById("contents");
                    int total    = int.Parse(element.Attributes.GetValue("total", "8"));
                    int startNum = int.Parse(element.Attributes.GetValue("startNum", "1"));//从第几条开始
                    //跳过多少条数据
                    int skipNum         = (channelPage - 1) * total + (startNum - 1);
                    var channelContents = generateApp.GetContentSummaryByChannelId(channelId, skipNum, total);


                    this.Document.Variables.SetValue("contents", channelContents);
                    this.Document.Variables.SetValue("channel", channelModel);

                    string renderHtml = this.Document.GetRenderText();
                    ret.Status = true;
                    ret.Html   = renderHtml;
                    return(ret);
                }
                //文章数据
                if (RenderMode == 3)
                {
                    this.Document.Variables.SetValue("channel", channelModel);
                    this.Document.Variables.SetValue("content", contentModel);
                    //渲染内容数据
                    string renderHtml = this.Document.GetRenderText();
                    renderHtml = HtmlPlayerHandler.CreateVideo(renderHtml);
                    ret.Status = true;
                    ret.Html   = renderHtml;
                    return(ret);
                }
            }
            catch (Exception ex)
            {
                LoggerHelper.Exception(ex);
            }

            return(ret);
        }
Exemplo n.º 9
0
 /// <summary>
 /// 返回渲染后的模板文件
 /// </summary>
 /// <param name="content"></param>
 /// <param name="template"></param>
 /// <returns></returns>
 public (bool genStatus, string contentHtml) GenerateContentHtml(ContentModel content, cms_template template)
 {
     try
     {
         this.LoadTemplate(template.template_content);
         this.InitPageTemplate(content);
         return(true, this.Document.GetRenderText());
     }
     catch (Exception ex)
     {
         LogNHelper.Exception(ex);
     }
     return(false, "");
 }