Example #1
0
        public string GetImages(string articleId)
        {
            //获取文章关联的图片
            ArticleImageBLL       articleImageBLL = new ArticleImageBLL();
            List <SqlDbParameter> parms           = new List <SqlDbParameter>();
            SqlDbParameter        parm            = new SqlDbParameter();

            parm.ColumnName     = "ArticleId";
            parm.ParameterName  = "ArticleId";
            parm.ParameterValue = articleId;
            parms.Add(parm);

            parm = new SqlDbParameter();
            parm.QualificationType = SqlDbParameter.QualificationSymbol.IsNull;
            parm.ColumnName        = "IsDelete";
            parm.ParameterName     = "IsDelete";
            parms.Add(parm);

            List <ArticleImageEntity> imageList = articleImageBLL.GetAllArticleImage(parms);

            IDictionary info = new Hashtable();

            if (imageList.Count == 0)
            {
                info.Add("data", null);
                return(JsonConvert.SerializeObject(info));
            }
            //封装属性url,saveName, name
            foreach (var image in imageList)
            {
                image.url = "/" + ConfigurationManager.AppSettings["AriticleImagePath"] + image.ImageFileName;
            }
            info.Add("data", imageList);
            return(JsonConvert.SerializeObject(info));
        }
        /// <summary>
        /// 文章详细页(前台)
        /// </summary>
        /// <returns></returns>
        public ActionResult Detail()
        {
            var routeData = Request.RequestContext.RouteData.Values;
            //string url = System.Web.HttpContext.Current.Request.Path;

            string routeMapValue = string.Empty;  //路由匹配的值,这里为主文件名
            int    i             = 0;

            foreach (KeyValuePair <string, object> pair in routeData)
            {
                if (i == 0)
                {
                    //regexStr = pair.Key;
                    routeMapValue = pair.Value.ToString();
                }
                ++i;
            }
            //string url = System.Web.HttpContext.Current.Request.Url.AbsolutePath;//获取当前url,以/开头

            string categoryKey     = string.Empty; //类别
            string noOfCategoryKey = string.Empty; //类别序数

            string          regexStr = @"\D+";
            Regex           regex    = new Regex(regexStr, RegexOptions.IgnoreCase);//抓取文章类别标识
            MatchCollection matchs   = regex.Matches(routeMapValue);

            foreach (Match match in matchs)
            {
                categoryKey = match.Groups[0].Value;
            }

            regexStr = @"\d+";
            regex    = new Regex(regexStr);//
            matchs   = regex.Matches(routeMapValue);
            foreach (Match match in matchs)
            {
                noOfCategoryKey = match.Groups[0].Value; //如果文章不属于任何类别,为文章主键
            }
            //期刊Id JournalId:如果请求中带期刊Id就使用,如果没有提供,则使用默认当前期刊Id
            string defaultJournalId = Request.QueryString["JournalId"];

            if (string.IsNullOrEmpty(defaultJournalId))
            {
                defaultJournalId = currentJournalEntity.JournalId.ToString();
            }

            ArticleBLL            articleBLL = new ArticleBLL();
            List <SqlDbParameter> parms      = new List <SqlDbParameter>();
            SqlDbParameter        parm       = new SqlDbParameter();

            if (categoryKey == "nocategory")
            {
                parm.ColumnName        = "A.Id";
                parm.ParameterName     = "Id";
                parm.ParameterValue    = noOfCategoryKey;//当前文章Id
                parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
                parms.Add(parm);
            }
            else //通过期刊ID,类别ID, 以及该类别下的序号获取文章(有局限性,即当文章没有类别时,无法通过此逻返回文章)
            {
                parm.ColumnName        = "JournalId";
                parm.ParameterName     = "JournalId";
                parm.ParameterValue    = defaultJournalId;//当前期刊Id
                parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
                parms.Add(parm);

                parm                   = new SqlDbParameter();
                parm.ColumnName        = "CategoryId";
                parm.ParameterName     = "CategoryId";
                parm.ParameterValue    = Constants.ChannelToCategory[categoryKey];;//文章类别
                parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
                parms.Add(parm);

                int _noOfCategory = 0;
                int.TryParse(noOfCategoryKey, out _noOfCategory);
                parm                   = new SqlDbParameter();
                parm.ColumnName        = "NoOfCategory";
                parm.ParameterName     = "NoOfCategory";
                parm.ParameterValue    = _noOfCategory;//该类别序号
                parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
                parms.Add(parm);
            }

            DataTable     dt      = articleBLL.GetArticleDataTable(parms);
            ArticleEntity article = new ArticleEntity();

            if (dt.Rows.Count > 0)
            {
                article.ArticleTitle   = dt.Rows[0]["ArticleTitle"].ToString();
                article.ArticleContent = dt.Rows[0]["ArticleContent"].ToString().Replace("<p>", "<p class=\"cntp\">");
                article.Id             = int.Parse(dt.Rows[0]["Id"].ToString());
                //获取文章关联的图片
                ArticleImageBLL articleImageBLL = new ArticleImageBLL();
                parms               = new List <SqlDbParameter>();
                parm                = new SqlDbParameter();
                parm.ColumnName     = "ArticleId";
                parm.ParameterName  = "ArticleId";
                parm.ParameterValue = article.Id;
                parms.Add(parm);

                parm = new SqlDbParameter();
                parm.QualificationType = SqlDbParameter.QualificationSymbol.IsNull;
                parm.ColumnName        = "IsDelete";
                parm.ParameterName     = "IsDelete";
                parms.Add(parm);

                List <ArticleImageEntity> articleList = articleImageBLL.GetAllArticleImage(parms);
                //TODO Config
                string imageDictory = ConfigurationManager.AppSettings["AriticleImagePath"];
                article.ImgFileArray = new ArrayList();
                foreach (var item in articleList)
                {
                    article.ImgFileArray.Add(imageDictory + item.ImageFileName);
                }
            }

            //1.详细页上的图标
            ViewBag.DetailImg = Constants.ListImage[categoryKey];//类别图片

            //2.详细页之返回各类别新闻列表页的url
            string detailReturnURL = "news_list_yw.html";

            if (categoryKey == "jt" || categoryKey == "gs")
            {
                detailReturnURL = string.Format("news_list_{0}.html", categoryKey); //TODO
            }
            else if (categoryKey.IndexOf("yw") > -1 || categoryKey.IndexOf("jiaojuguoqi") > -1)
            {
                string[] categoryList = categoryKey.Split('_');
                detailReturnURL = string.Format("news_list_{0}.html", categoryList[0]);
            }
            ViewBag.DetailReturnURL = detailReturnURL;

            //静态持久化
            if (Request.QueryString["static"] == "1")
            {
                string html      = CommentHelper.RenderViewToString(this.ControllerContext, @"~/Views/Home/Detail.cshtml", article);
                string outputDir = HttpContext.Server.MapPath(ConfigurationManager.AppSettings["HtmlOutput"]) + "\\" + currentJournalEntity.JournalName + "\\";
                if (!Directory.Exists(outputDir))
                {
                    Directory.CreateDirectory(outputDir);
                }
                System.IO.File.WriteAllText(outputDir + base.currentURL, html);
            }
            return(View(article));
        }
Example #3
0
        /// <summary>
        /// 保存文章的相关图片
        /// </summary>
        /// <param name="article"></param>
        public void ArticleImageSave(ArticleEntity article)
        {
            if (article.ImgFileList != null)
            {
                ArticleImageBLL articleImageBLL = new ArticleImageBLL();
                //为图片命名获取参数

                List <SqlDbParameter> parms = new List <SqlDbParameter>();
                SqlDbParameter        parm  = null;
                parm                = new SqlDbParameter();
                parm.ColumnName     = "ArticleId";
                parm.ParameterName  = "ArticleId";
                parm.ParameterValue = article.Id;
                parm.ColumnType     = DbType.Int32;
                parms.Add(parm);



                List <ArticleImageEntity> articleList = articleImageBLL.GetAllArticleImage(parms, "Id DESC");
                int j = 1;
                if (articleList.Count > 0)
                {
                    Match  numMatch = Regex.Match(articleList[0].ImageFileName, @"_(\d+)\."); //获取最大的编号
                    string num      = numMatch.Groups[1].Value;
                    int    _j       = 0;
                    if (int.TryParse(num, out _j))
                    {
                        j = ++_j;
                    }
                }

                for (int i = 0; i < article.ImgFileList.Count; i++)
                {
                    if (article.ImgFileList[i].Id != null) //New Add Picture
                    {
                        continue;
                    }
                    string savePath      = System.Web.HttpContext.Current.Server.MapPath("~");
                    string fromPath      = savePath + ConfigurationManager.AppSettings["UploadTmp"] + article.ImgFileList[i].ImgFileName;
                    string fileExtension = Path.GetExtension(fromPath); // 文件扩展名

                    string categoryFix = "other";
                    //根据文件类别+分类下序数命名新图片名
                    var firstKey = Constants.ChannelToCategory.FirstOrDefault(q => q.Value == article.CategoryId.Value).Key;
                    if (!string.IsNullOrEmpty(firstKey))
                    {
                        categoryFix = firstKey;
                    }
                    string newFileName = categoryFix + article.NoOfCategory.ToString().PadLeft(2, '0') + "_" + j + fileExtension; // 文件扩展名  //jt01_1.jpg
                    ++j;
                    string toPath = savePath + ConfigurationManager.AppSettings["AriticleImagePath"] + newFileName;
                    if (!Directory.Exists(savePath + ConfigurationManager.AppSettings["AriticleImagePath"]))
                    {
                        Directory.CreateDirectory(savePath + ConfigurationManager.AppSettings["AriticleImagePath"]);
                    }
                    System.IO.File.Copy(fromPath, toPath);
                    //TODO Insert ArticleImage
                    ArticleImageEntity articleImage = new ArticleImageEntity();
                    articleImage.ArticleId     = article.Id;
                    articleImage.ImageFileName = newFileName;
                    articleImageBLL.AddArticleImageEntity(articleImage);
                }
            }
        }