FYJ.Blogs开发系列(四)-后台文章Controller
FYJ.Blogs开发系列(五)-前台基页Controller
FYJ.Blogs开发系列(六)-前台主页Controller
FYJ.Blogs开发系列(七)-前台文章Controller
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 | using Blogs.Common; using Blogs.Entity; using Blogs.UI.Admin.Filters; using Blogs.ViewModel; using Blogs.ViewModel.MyBlog; using FYJ.Pager; using FYJ.Common; using Microsoft.Practices.Unity; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Reflection; using System.Text.RegularExpressions; using System.Web; using System.Web.Mvc; namespace Blogs.UI.Admin.Areas.MyBlog.Controllers { [LogFilter] [ExceptionFilter] [AuthenFilter] public class ArticleController : Controller { public ActionResult Index() { int blogID = UserInfo.BlogID; string userID = UserInfo.UserID; var blogs = BlogDal.GetEntities().Where(c => c.userID == userID).OrderByDescending(c => c.blogOrder).Select(c => new { c.blogTitle, c.blogID }).ToList(); //下面的ViewData["BlogSelect"] 中的BlogSelect 不能与 @Html.DropDownList("ddlBlogSelect", ViewData["BlogSelect"] as IEnumerable<SelectListItem>) ddlBlogSelect 一样 //但是用DataTable.AsDataView() 就可以? 不然默认值选不中 ViewData[ "BlogSelect" ] = new SelectList(blogs, "blogID" , "blogTitle" , blogID); var categorys = categoryDal.GetcategorysTreeTable(blogID + "" ).AsDataView(); var topics = TopicDal.GetEntities().Where(c => c.blogID == blogID); ViewData[ "categorySelect" ] = new SelectList(categorys, "categoryID" , "categoryDisplay" ); ViewData[ "TopicSelect" ] = new SelectList(topics, "topicID" , "topicDisplay" ); var siteCategorys = SiteCategoryDal.GetcategorysTreeTable().AsDataView(); ViewData[ "siteCategorySelect" ] = new SelectList(siteCategorys, "categoryID" , "categoryDisplay" ); return View(); } #region 依赖属性 [Dependency] public Blogs.IDAL.IBlog BlogDal { get ; set ; } [Dependency] public Blogs.IDAL.ICategory categoryDal { get ; set ; } [Dependency] public Blogs.IDAL.IArticle ArticleDal { get ; set ; } [Dependency] public Blogs.IDAL.IArticleView ArticleViewDal { get ; set ; } [Dependency] public Blogs.IDAL.ITopic TopicDal { get ; set ; } [Dependency] public Blogs.IDAL.ITag TagDal { get ; set ; } [Dependency] public Blogs.IDAL.ITheme ThemeDal { get ; set ; } [Dependency] public Blogs.IDAL.ISiteCategory SiteCategoryDal { get ; set ; } [Dependency] public Blogs.IDAL.IComment CommentDal { get ; set ; } [Dependency] public Blogs.IDAL.IFileRelation FileRelationDal { get ; set ; } #endregion [LogFilter( "查询文章" , "查询了文章列表" , true )] public JsonResult GetList(GridPager pager, string queryStr) { int blogID = UserInfo.BlogID; pager.CurrentPage = Convert.ToInt32(Request[ "page" ]); pager.PageSize = Convert.ToInt32(Request[ "rows" ]); pager.OrderColumn = Request[ "sort" ]; pager.Order = Request[ "order" ]; List<Func<blog_view_article, bool >> wheres = new List<Func<blog_view_article, bool >>(); if (!String.IsNullOrWhiteSpace(Request[ "ddlBlogSelect" ])) { int sblogID = Int32.Parse(Request[ "ddlBlogSelect" ]); wheres.Add(c => c.blogID == sblogID); } if (!String.IsNullOrWhiteSpace(Request[ "ddlsiteCategorySelect" ])) { int id = Int32.Parse(Request[ "ddlsiteCategorySelect" ]); wheres.Add(c => c.siteCategoryID == id); } if (!String.IsNullOrWhiteSpace(Request[ "ddlcategorySelect" ])) { int id = Int32.Parse(Request[ "ddlcategorySelect" ]); wheres.Add(c => c.categoryID == id); } if (!String.IsNullOrWhiteSpace(Request[ "ddlTopicSelect" ])) { int id = Int32.Parse(Request[ "ddlTopicSelect" ]); wheres.Add(c => c.topicID == id); } if (!String.IsNullOrWhiteSpace(Request[ "ddlarticleIsOriginalSelect" ])) { bool b = (Int32.Parse(Request[ "ddlarticleIsOriginalSelect" ]) == 1); wheres.Add(c => c.articleIsOriginal == b); } if (!String.IsNullOrWhiteSpace(Request[ "StartDate" ])) { wheres.Add(c => c.articleDatetime.Date >= Convert.ToDateTime(Request[ "StartDate" ]).Date); } if (!String.IsNullOrWhiteSpace(Request[ "EndDate" ])) { wheres.Add(c => c.articleDatetime.Date <= Convert.ToDateTime(Request[ "EndDate" ]).Date); } if (!String.IsNullOrWhiteSpace(Request[ "articleTitle" ])) { wheres.Add(c => c.articleTitle.ToLower().Contains(Request[ "articleTitle" ].Trim().ToLower())); } IEnumerable<Blogs.Entity.blog_view_article> list = ArticleViewDal.GetList( ref pager, wheres.ToArray()); var json = new { total = pager.TotalRows, rows = (from r in list select new { ID = r.articleID, r.articleID, r.siteCategoryDisplay, r.categoryDisplay, r.topicDisplay, r.articleTitle, r.articlePic, r.articleIsPic, r.articleIsOriginal, r.articleIsDisabled, r.articleIsTop, r.articleClickTimes, r.articleCommentTimes, r.articleThemeDisplay, r.articlePostBy, r.themeDisplay, articleDatetime = r.articleDatetime.ToString( "yyyy-MM-dd HH:mm:ss" ), ADD_DATE = r.ADD_DATE.ToString( "yyyy-MM-dd HH:mm:ss" ), UPDATE_DATE = r.UPDATE_DATE.ToString( "yyyy-MM-dd HH:mm:ss" ), isDisableComment = CommentDal.isDisableComment(r.articleCommentLimit), isAllowAnonymousComment = CommentDal.isDisabledAnonymousComment(r.articleCommentLimit), isVerifyComment = CommentDal.isVerifyComment(r.articleCommentLimit) }).ToArray() }; return Json(json, JsonRequestBehavior.AllowGet); } public ActionResult GetCategoryJson() { string blogID = Request[ "blogID" ]; var dt = categoryDal.GetcategorysTreeTable(blogID + "" ); List< object > categorys = new List< object >(); categorys.Add( new { categoryID = "" , categoryDisplay = "--请选择--" }); foreach (DataRow dr in dt.Rows) { categorys.Add( new { categoryID = Convert.ToInt32(dr[ "categoryID" ]), categoryDisplay = dr[ "categoryDisplay" ].ToString() }); } return Json(categorys, JsonRequestBehavior.AllowGet); } public ActionResult GetTopicJson() { int blogID = Convert.ToInt32(Request[ "blogID" ]); var topics = TopicDal.GetEntities().Where(c => c.blogID == blogID).ToList(); List< object > list = new List< object >(); foreach (blog_tb_topic topic in topics) { list.Add( new { topicID = topic.topicID, topicDisplay = topic.topicDisplay }); } list.Insert(0, new { topicID = "" , topicDisplay = "--请选择--" }); return Json(list, JsonRequestBehavior.AllowGet); } [HttpPost] public JsonResult ChangeArticle(FormCollection collection) { string [] ids = Request[ "ids" ].Split( new char [] { ',' }, StringSplitOptions.RemoveEmptyEntries); int result = ArticleDal.ChangeArticle(ids, Request[ "blogID" ], Request[ "categoryID" ]); if (result > 0) { return Json( new { code = 1, message = "操作成功" }, JsonRequestBehavior.AllowGet); } return Json( new { code = -1, message = "操作成功" }, JsonRequestBehavior.AllowGet); } [HttpPost] public JsonResult ChangeState(FormCollection collection) { if (collection[ "fieldName" ] == "blogID" ) { int result = 0; string [] ids = Request[ "ids" ].Split( new char [] { ',' }, StringSplitOptions.RemoveEmptyEntries); foreach ( string id in ids) { result += ArticleDal.ChangeBlog(id, Request[ "blogID" ]); } if (result > 0) { return Json( new { code = 1, message = "操作成功" }, JsonRequestBehavior.AllowGet); } } else { if (ArticleDal.Update(Request[ "ids" ], Request[ "fieldName" ], Request[ "state" ]) > 0) { return Json( new { code = 1, message = "操作成功" }, JsonRequestBehavior.AllowGet); } } return Json( new { code = -1, message = "操作失败" }, JsonRequestBehavior.AllowGet); } [HttpPost] public ActionResult TagString(FormCollection collection) { var v = TagDal.GetEntities().Where(c => c.blogID == Convert.ToInt32(Request[ "blogID" ])); string tagString = "" ; foreach (var item in v) { tagString += item.tagDisplay + "," ; } tagString = tagString.TrimEnd( ',' ); return Content(tagString); } [HttpPost] public ActionResult Attachment(FormCollection collection) { //IDictionary<string, string> dic = new Dictionary<string, string>(); //dic.Add("userID", "8dc2885cd7bb44aa9344557dbc0c1630"); //dic.Add("ak", "afbf3d192908477d9e24b3e351bc4ebe"); //dic.Add("action", "getFileUrl"); //dic.Add("objectTag", "attachment"); //dic.Add("objectID", Request["articID"]); //string par = ""; //string sign = FYJ.Common.HttpHelper.Sign(dic, "493d453149f943e0b515f068e2d029d6", out par, "sign"); //string url = "http://my.fyj.me/service/file.action?" + par + "&sign=" + sign; //string json = FYJ.Common.HttpHelper.DoGet(url); //DataTable dt = FYJ.Common.JsonHelper.JsonToDataTable(json); //if (dt != null && dt.Rows.Count > 0) //{ // foreach (DataRow dr in dt.Rows) // { // str += "<p><a href='" + dr["fileUrl"] + "' target='_blank'>" + dr["fileName"] + "</a></p>"; // } //} return Content( "" ); } public ActionResult Edit( string id) { if (id == null ) { return null ; } int objectID; blog_tb_article model = new blog_tb_article { blogID = UserInfo.BlogID, ADD_DATE = DateTime.Now, UPDATE_DATE = DateTime.Now, articleDatetime = DateTime.Now }; if ((!String.IsNullOrEmpty(id)) && id.ToString() != "0" ) { objectID = Convert.ToInt32(id); model = ArticleDal.GetEntities().Where(c => c.articleID == objectID).FirstOrDefault(); string tagString = "" ; foreach (var item in model.blog_tb_tagArticle) { tagString += item.blog_tb_tag.tagDisplay + "," ; } tagString = tagString.TrimEnd( ',' ); ViewData[ "tagString" ] = tagString; model.UPDATE_DATE = DateTime.Now; blog_tb_article_content contentModel = model.blog_tb_article_content.FirstOrDefault(); //加载临时正文 string tempContent = ArticleDal.ReadTempContent(id); if (!String.IsNullOrEmpty(tempContent)) { contentModel.articleContent = tempContent; } ViewData[ "content" ] = contentModel; } else { string lastArticleID = ArticleDal.LoadLastArticleID(); if (!String.IsNullOrEmpty(lastArticleID)) { objectID = Convert.ToInt32(lastArticleID); string tempContent = ArticleDal.ReadTempContent(lastArticleID); blog_tb_article_content contentModel = new blog_tb_article_content(); contentModel.articleContent = tempContent; contentModel.UPDATE_DATE = DateTime.Now; ViewData[ "content" ] = contentModel; } else { objectID = Convert.ToInt32(ArticleDal.NewID()); } } int blogID = model.blogID; string userID = UserInfo.UserID; var siteCategorys = SiteCategoryDal.GetcategorysTreeTable().AsDataView(); var categorys = categoryDal.GetcategorysTreeTable(blogID + "" ).AsDataView(); var topics = TopicDal.GetEntities().Where(c => c.blogID == blogID); var tags = TagDal.GetEntities().Where(c => c.blogID == blogID); var blogs = BlogDal.GetEntities().Where(c => c.userID == userID); ViewData[ "siteCategorySelect" ] = new SelectList(siteCategorys, "categoryID" , "categoryDisplay" ); ViewData[ "categorySelect" ] = new SelectList(categorys, "categoryID" , "categoryDisplay" ); ViewData[ "TopicSelect" ] = new SelectList(topics, "topicID" , "topicDisplay" ); ViewData[ "TagSelect" ] = new SelectList(tags, "tagID" , "tagDisplay" ); ViewData[ "blogSelect" ] = new SelectList(blogs, "blogID" , "blogTitle" ); List<blog_tb_theme> list = ThemeDal.GetEntities().ToList(); list.Insert(0, new blog_tb_theme { themeID = "" , themeDisplay = "--请选择--" }); ViewData[ "Theme" ] = new SelectList(list, "themeID" , "themeDisplay" , "" ); if (tags.Count() > 0) { } ViewData[ "objectID" ] = objectID; //两个上传控件的ID ViewData[ "uploadID1" ] = GetUploadModel1(objectID + "" ); ViewData[ "uploadID2" ] = GetUploadModel2(objectID + "" ); Blogs.ViewModel.MyBlog.Article m = model.CloneProperties<Blogs.ViewModel.MyBlog.Article>(); m.IsDisableComment = CommentDal.isDisableComment(model.articleCommentLimit); m.IsDisabledAnonymouComment = CommentDal.isDisabledAnonymousComment(model.articleCommentLimit); m.IsVerifyComment = CommentDal.isVerifyComment(model.articleCommentLimit); return View(m); } private UploadModel GetUploadModel1( string objectID) { UploadModel model = new UploadModel { ID = Guid.NewGuid().ToString( "N" ), AllowMulti = false , IsImage = true , ObjectType = "blog" , ObjectTag = "mainPic" , UploadDir = "pic" , ThumHeight = 360, ThumWidth = 480, ThumPassSizeKB = 1024, ObjectID = objectID, IsSinglePic = true ,FileTag= "blog" }; IEnumerable<blog_tb_fileRelation> relations = FileRelationDal.GetEntities().Where(c => c.objectID == objectID && c.objectTag == "mainPic" ).ToList(); if (relations != null ) { List<FileLink> links = new List<FileLink>(); foreach (blog_tb_fileRelation relation in relations) { blog_tb_file file = relation.blog_tb_file; string fileName = file.fileName; if (String.IsNullOrEmpty(fileName)) { fileName = Path.GetFileName(file.fileUrl); } links.Add( new FileLink(relation.relationID,file.fileID, fileName, file.fileUrl)); } model.Files = links; //首张图片 model.FirstImage = links.FirstOrDefault(); } return model; } private UploadModel GetUploadModel2( string objectID) { UploadModel model = new UploadModel { ID = Guid.NewGuid().ToString( "N" ), UploadDir = "file" , AllowMulti = true , ObjectType = "blog" , ObjectTag = "attachment" , IsImage = false , ObjectID = objectID,FileTag= "blog" }; IEnumerable<blog_tb_fileRelation> relations = FileRelationDal.GetEntities().Where(c => c.objectID == objectID && c.objectTag == "attachment" ).ToList(); if (relations != null ) { List<FileLink> links = new List<FileLink>(); foreach (blog_tb_fileRelation relation in relations) { blog_tb_file file = relation.blog_tb_file; string fileName = file.fileName; int size = (file.fileSize == null ? 0 : ( int )file.fileSize.Value); if (String.IsNullOrEmpty(fileName)) { fileName = Path.GetFileName(file.fileUrl); } links.Add( new FileLink(relation.relationID,file.fileID, fileName, file.fileUrl) { Size = size }); } model.Files = links; } return model; } private void MyUpdateModel( object obj, FormCollection collection) { string [] keys = collection.AllKeys; PropertyInfo[] pis = obj.GetType().GetProperties(); foreach (PropertyInfo pi in pis) { string name = pi.Name; if (keys.Contains(name)) { string value = collection[name]; if (value == "true,false" ) { pi.SetValue(obj, true ); } else { pi.SetValue(obj, value.ConvertValue(pi.PropertyType)); } } } } [HttpPost] public JsonResult Edit( string id, FormCollection collection) { int articleID = Convert.ToInt32(Request[ "objectID" ]); string userID = UserInfo.UserID; Article m = new Article(); MyUpdateModel(m, collection); CommentLimit articleCommentLimit = 0; if (m.IsDisableComment) { articleCommentLimit |= CommentLimit.禁止回复; } if (m.IsVerifyComment) { articleCommentLimit |= CommentLimit.需要审核; } if (m.IsDisabledAnonymouComment) { articleCommentLimit |= CommentLimit.禁止匿名用户回复; } AttachmentLimit attachmentLimit = 0; if (!String.IsNullOrEmpty(Request[ "chkAttachmentLimit" ])) { if (Request[ "chkAttachmentLimit" ].Contains( "1" )) { attachmentLimit |= AttachmentLimit.禁止未登录用户下载; } if (Request[ "chkAttachmentLimit" ].Contains( "2" )) { attachmentLimit |= AttachmentLimit.禁止未回复用户下载; } if (Request[ "chkAttachmentLimit" ].Contains( "3" )) { attachmentLimit |= AttachmentLimit.禁止下载; } } blog_tb_article model = new blog_tb_article(); if (String.IsNullOrEmpty(id) || id == "0" ) //新增 { //UpdateModel(model); MyUpdateModel(model, collection); model.articleID = articleID; model.ADD_DATE = DateTime.Now; model.UPDATE_DATE = DateTime.Now; Blogs.Entity.blog_tb_article_content contentModel = new blog_tb_article_content(); contentModel.UPDATE_DATE = DateTime.Now; MyUpdateModel(contentModel, collection); contentModel.contentID = Guid.NewGuid().ToString( "N" ); model.blog_tb_article_content = new blog_tb_article_content[] { contentModel }; //设置图片 SetPic(model); model.articleCommentLimit = ( int )articleCommentLimit; model.articleAttachmentLimit = ( int )attachmentLimit; ArticleDal.InsertEntity(model); } else { model = ArticleDal.GetEntities().Where(c => c.articleID == articleID).FirstOrDefault(); model.articlePic = Request[ "fileUrlName_" + Request[ "uploadID1" ]]; model.articleThumbPic = Request[ "thumUrlName_" + Request[ "uploadID1" ]]; MyUpdateModel(model, collection); model.UPDATE_DATE = DateTime.Now; Blogs.Entity.blog_tb_article_content contentModel = model.blog_tb_article_content.FirstOrDefault(); MyUpdateModel(contentModel, collection); contentModel.UPDATE_DATE = DateTime.Now; //设置图片 SetPic(model); model.articleCommentLimit = ( int )articleCommentLimit; model.articleAttachmentLimit = ( int )attachmentLimit; ArticleDal.UpdateEntity(model); } //处理标签 必须后处理 因为之前还没有插入articleID if (!String.IsNullOrEmpty(Request[ "txt_tag" ])) { TagDal.UpdateTag(Request[ "blogID" ], articleID + "" , Request[ "txt_tag" ].TrimEnd( ',' )); } return Json( new { code = 1, message = "操作成功" }, JsonRequestBehavior.AllowGet); } private void SetPic(blog_tb_article model) { string fileUrl = Request[ "lastImageName_mainPic_" + model.articleID]; if (!String.IsNullOrEmpty(fileUrl)) { model.articlePic = fileUrl; //model.articleThumbPic = fileUrl; } } [HttpPost] public JsonResult Delete(FormCollection collection) { ArticleDal.DeleteEntity(collection[ "ids" ].Split( new char [] { ',' }, StringSplitOptions.RemoveEmptyEntries)); return Json( new { code = 1, message = "删除成功" }, JsonRequestBehavior.AllowGet); } [HttpPost] public JsonResult CreateCatelog(FormCollection collection) { ArticleDal.CreateCatelog(collection[ "ids" ]); return Json( new { code = 1, message = "生成目录成功" }, JsonRequestBehavior.AllowGet); } public ActionResult View( int id) { var model = ArticleDal.GetEntities().Where(c => c.articleID == id).FirstOrDefault(); return View(model); } [HttpPost] public JsonResult SaveTempContent(FormCollection collection) { string articleID = Request[ "objectID" ]; string articleContent = Request[ "articleContent" ]; ArticleDal.SaveTempContent(articleID, articleContent); return Json( new { code = 1, message = DateTime.Now.ToString( "HH:mm:ss" ) + "草稿保存成功" }, JsonRequestBehavior.AllowGet); } public ActionResult ueditorEdit( int id) { var model = ArticleDal.GetEntities().Where(c => c.articleID == id).FirstOrDefault(); blog_tb_article_content contentModel = model.blog_tb_article_content.FirstOrDefault(); //加载临时正文 string tempContent = ArticleDal.ReadTempContent(id + "" ); if (!String.IsNullOrEmpty(tempContent)) { contentModel.articleContent = tempContent; } ViewData[ "content" ] = contentModel; Blogs.ViewModel.MyBlog.Article m = model.CloneProperties<Blogs.ViewModel.MyBlog.Article>(); return View(m); } } } |