Exemple #1
0
        /// <summary>
        /// 生成文章页
        /// </summary>
        /// <param name="Ids"></param>
        public void MakeArticlePage(string Ids)
        {
            int iSuccessCount = 0;                                                                 //成功条数
            int iFalseCount   = 0;                                                                 //失败条数

            var allCategoryList = mangae.db.TG_Category.OrderBy(m => m.ID).AsQueryable().ToList(); //所有栏目数据
            var allTempletList  = mangae.db.TG_Templet.OrderBy(m => m.ID).AsQueryable().ToList();  //所有的模板数据
            var listIds         = Ids.Split(',').Select(m => Convert.ToInt32(m)).ToList();
            var CategoryList    = allCategoryList.Where(m => listIds.Contains(m.ID)).ToList();     //获取生成栏目数据

            //获取栏目的ID集合
            var CategoryIds = CategoryList.Select(m => m.ID);
            //获取生成栏目下的所有文章
            List <TG_Article> ArticleList = mangae.db.TG_Article.Where(m => CategoryIds.Contains(m.iCategoryId) && m.bIsDeleted == false).ToList();

            //循环生成文章页
            foreach (var category in CategoryList)
            {
                //获取栏目下的文章模板
                TG_Templet templetArticle = allTempletList.Where(m => m.ID == category.iArticleTemplateId).SingleOrDefault();
                //获取当前栏目下的文章数据列表
                var CurrentArticleList = ArticleList.Where(m => m.iCategoryId == category.ID).ToList();
                //遍历当前栏目下的文章
                foreach (var article in CurrentArticleList)
                {
                    if (article.iTemplateId != 0)
                    {//获取文章是否有独立模板
                     //有则使用独立模板
                        templetArticle = allTempletList.FirstOrDefault(m => m.ID == article.iTemplateId);
                    }
                    if (templetArticle != null)
                    {//模板存在
                     //解析模板
                        string templetHtmlString = RazorHelper.ParseString(templetArticle.sTempletEnName, article);
                        string ArticlePath       = FuncHelper.Instance.GetHtmlPath(category, allCategoryList);
                        if (RazorHelper.MakeHtml(ArticlePath, article.ID.ToString(), templetHtmlString))
                        {//生成成功
                            iSuccessCount++;
                        }
                        else
                        {//生成失败
                            iFalseCount++;
                        }
                    }
                }
            }
            result.success = true;
            result.data    = new
            {
                iSuccessCount = iSuccessCount,
                iFalseCount   = iFalseCount,
                iTatolCount   = iSuccessCount + iFalseCount
            };
        }
        /// <summary>
        /// 生成模板文件
        /// </summary>
        private void MakeTempletHtml(TG_Templet templet)
        {
            var path = AppDomain.CurrentDomain.BaseDirectory + ConfigHelper.ReadAppSetting("sTemplatePath") + "\\";

            if (!Directory.Exists(path))
            {//不存在该目录
                Directory.CreateDirectory(path);
            }
            else
            {
                string sFileName = templet.sTempletEnName + ".cshtml";
                System.IO.File.WriteAllText(path + sFileName, templet.sTempletContent);
            }
        }
 public void Update(TG_Templet templet)
 {
     templet.sTempletEnName = templet.sTempletEnName.Trim();
     if (mangae.db.TG_Templet.Where(m => m.ID != templet.ID).Any(m => m.sTempletEnName == templet.sTempletEnName || m.sTempletName == templet.sTempletName))
     {//存在重复模板名称或者模板标识
         result.info = "存在重复模板名称或者模板标识";
     }
     else
     {
         mangae.Edit <TG_Templet>(templet);
         result.success = mangae.SaveChange();
         //生成模板文件
         MakeTempletHtml(templet);
     }
 }
 public void Insert(TG_Templet templet)
 {
     if (templet.bIsCompile == null)
     {
         templet.bIsCompile = false;
     }
     templet.sTempletEnName = templet.sTempletEnName.Trim();
     if (mangae.db.TG_Templet.Any(m => m.sTempletEnName == templet.sTempletEnName || m.sTempletName == templet.sTempletName))
     {//存在重复模板名称或者模板标识
         result.info = "存在重复模板名称或者模板标识";
     }
     else
     {//不存在添加
         templet.dInsertTime = DateTime.Now;
         mangae.Add <TG_Templet>(templet);
         result.success = mangae.SaveChange();
         //生成模板文件
         MakeTempletHtml(templet);
     }
 }
        /// <summary>
        /// 添加模板视图
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public ActionResult Add(int?ID)
        {
            var templet = new TG_Templet();

            templet.sTempletUrl = ConfigHelper.ReadAppSetting("sTemplatePath");
            if (ID != null)
            {
                templet = mangae.db.TG_Templet.Find(ID);
                //读取模板文件内容
                var    path      = AppDomain.CurrentDomain.BaseDirectory + ConfigHelper.ReadAppSetting("sTemplatePath") + "\\";
                string sFileName = templet.sTempletEnName + ".cshtml";
                if (System.IO.File.Exists(path + sFileName))
                {//文件存在
                    string Content = System.IO.File.ReadAllText(path + sFileName);
                    templet.sTempletContent = Content;
                }
                else
                {//不存在
                    templet.sTempletContent = templet.sTempletContent;
                }
            }
            return(View(templet));
        }