Пример #1
0
        /// <summary>
        /// 1.0 HttpGet评论分页 采用递归的方法直接在后台拼接好Ajax评论内容及分页Html,再将json返回到前台
        /// </summary>
        /// <param name="id">博文id</param>
        /// <returns></returns>
        public JsonResult WrapComment(int id)
        {
            int pageIndex = Request["pageIndex"] == null ? 1 : int.Parse(Request["pageIndex"]);
            int pageSize  = Request["pageSize"] == null ? 4 : int.Parse(Request["pageSize"]);

            //1.1 选出当前博文的该页内的一级评论
            List <Comment> topCmtList = commentService.GetPagedList(pageIndex, pageSize, c => c.CmtArtId == id && c.Status == 1 && c.ParentId == 0, c => c.SubTime, true);

            //1.2 创建评论框区域的大容器
            StringBuilder cmtHtmlStr = new StringBuilder("<ol class=\"commentlist\">");

            //1.3 递归获取各级评论,并追加到cmtHtmlString中
            PackCmt(topCmtList, cmtHtmlStr);

            //1.4 合上评论框区域的大容器,评论区域HTML拼接完毕
            cmtHtmlStr.Append("</ol>");

            //1.5 评论总Html
            string cmtHtmlString = cmtHtmlStr.ToString();

            //1.6 评论分页Html
            string cmtPagerString = string.Empty;

            //1.7 该篇博文的所有评论(连带回复)
            int totalCount = commentService.GetDataListBy(c => c.CmtArtId == id && c.Status == 1 && c.ParentId == 0).Count;//分页插件用

            if (totalCount > 0)
            {
                cmtPagerString = PagerHelper.GeneratePagerString(pageIndex, pageSize, totalCount);
                cmtPagerString = "<div class='pagination'>" + "<ul>" + cmtPagerString + "</ul>" + "</div>";
            }

            //1.8 构造json数据
            JsonModel jsonData = new JsonModel()
            {
                CoreData   = cmtHtmlString,
                Status     = 1,
                Message    = "成功",
                GotoUrl    = HttpContext.Request.Url.AbsoluteUri,
                PageNavStr = cmtPagerString
            };

            return(Json(jsonData, JsonRequestBehavior.AllowGet));
        }
Пример #2
0
        /// <summary>
        /// 1.0 根据id获取博文详情>
        /// 同时根据id获取上一篇下一篇>
        /// 获取游客cookie信息>
        /// 并判断是否是从邮件处链接过来(flag)>
        /// 同时为该收件人进行自动登录,以便其回复/评论
        /// </summary>
        /// <param name="id">ArticleId</param>
        public ActionResult Index(int id)
        {
            #region 0.0 从邮件过来的url?页码&页大小&该条评论Id()..,配合定位到评论锚点标记位
            string flag = Request["Flag"];
            flag = Common.Security.Base64TextUTF8Decode(flag);
            if (flag == "1")
            {
                ViewBag.Flag              = flag;
                ViewBag.Vid               = Common.Security.Base64TextUTF8Decode(Request["Vid"]); //游客id(收件人点击了链接)
                ViewBag.AnchorIndex       = Common.Security.Base64TextUTF8Decode(Request["AnchorIndex"]);
                ViewBag.AnchorSize        = Common.Security.Base64TextUTF8Decode(Request["AnchorSize"]);
                ViewBag.AnchorCmtRootId   = Common.Security.Base64TextUTF8Decode(Request["AnchorCmtRootId"]);
                ViewBag.AnchorCmtParentId = Common.Security.Base64TextUTF8Decode(Request["AnchorCmtParentId"]);
                ViewBag.AnchorCmtId       = Common.Security.Base64TextUTF8Decode(Request["AnchorCmtId"]);

                //为链接过来的收件人设置cookie
                //先将原来的cookie删除,再赋新值,Index后续会读取这个cookie
                HttpCookie visitorCookie = HttpContext.Request.Cookies["visitorCookie"];
                if (visitorCookie == null)
                {
                    visitorCookie = new HttpCookie("visitorCookie");
                }
                visitorCookie.Value   = ViewBag.Vid;              //存游客id值
                visitorCookie.Expires = DateTime.Now.AddDays(30); //设置过期时间,往后顺延30天
                HttpContext.Response.Cookies.Add(visitorCookie);
            }
            #endregion

            //获取文章详情------------------------------------------------------------------
            Article article = new Article();

            //1.1 根据博文id取数据,并判断是否为空
            article = articleService.GetEntity(id);

            //如果前台正在浏览这篇文章,这是后台却把这篇文章删了;此时浏览者刷新页面时就会出错
            //所以判断一下是否为null

            //1.2获取博文关键词  关键词可为空
            string[] keywords = null;
            if (!string.IsNullOrEmpty(article.Keywords))
            {
                //关键词规则约定:可以空格或,或,或、分开
                keywords = article.Keywords.Split(' ');
            }
            ViewData["keywords"] = keywords;

            //博文所属类别的所有博文:
            List <Article> allArt = articleService.GetDataListBy(a => a.CategoryId == article.CategoryId && a.Status == 1);

            //所有此类博文的id集合
            List <int> allArtIdsList = new List <int>();
            foreach (Article item in allArt)
            {
                allArtIdsList.Add(item.Id);
            }

            //1.3 文章底部:“上一篇”“下一篇”
            //如果allArtIdsList中只有一个元素(当前博文id),则不显示上一篇、下一篇
            //if allArtIdsList.count > 1
            //如果当前article.Id是第一个元素,则显示下一篇
            //如果当前article.Id是最后一个元素,则显示上一篇
            //如果当前article.Id不是第一个元素,也不是最后一个元素,则显示上一篇、下一篇
            int preIndex  = 0;
            int nextIndex = 0;

            if (allArtIdsList.Count > 1)
            {
                if (id == allArtIdsList[0])
                {
                    nextIndex = allArtIdsList[1];
                }
                else if (id == allArtIdsList.Last())
                {
                    preIndex = allArtIdsList[allArtIdsList.Count - 2];
                }
                else if (id != allArtIdsList[0] && id != allArtIdsList.Last())
                {
                    int curposition = allArtIdsList.IndexOf(id);//当前文章所在List的位置
                    preIndex  = allArtIdsList[curposition - 1];
                    nextIndex = allArtIdsList[curposition + 1];
                }
            }
            ViewBag.PreIndex  = preIndex;
            ViewBag.NextIndex = nextIndex;


            //1.4 此博文的所有评论总数为:
            ViewBag.CmtCount = commentService.GetDataListBy(c => c.CmtArtId == id && c.Status == 1).Count;

            //1.5 从浏览器获取访客cookie信息,
            int        visitorId    = 0;
            string     visitorName  = String.Empty;
            string     visitorEmail = String.Empty;
            HttpCookie cookie       = HttpContext.Request.Cookies["visitorCookie"];
            if (cookie != null)
            {
                if (int.TryParse(cookie.Value, out visitorId))
                {
                    List <Visitor> visitorlist = visitorService.GetDataListBy(v => v.Id == visitorId && v.Status == 1);
                    if (visitorlist != null && visitorlist.Count != 0)
                    {
                        visitorName  = visitorlist[0].VisitorName;
                        visitorEmail = visitorlist[0].VisitorEmail;
                    }
                }
            }

            //将这三个参数绑定到评论框上的input标签中
            ViewBag.VisitorId    = visitorId;
            ViewBag.VisitorName  = visitorName;
            ViewBag.VisitorEmail = visitorEmail;

            //1.6 百度分享的分享内容:
            ViewBag.Share = StringHelper.StringCut(article.ContentsRaw, 100); //新浪微博只能输入104个字

            return(View(article));
        }
Пример #3
0
        /// <summary>
        /// 2.0 jquery.datatable()插件 Ajax Get评论列表
        ///     按文章编号来排序吧
        /// </summary>
        /// <returns>datatable-JSON</returns>
        public JsonResult GetCommentsJson(jqDataTableParameter tableParam)
        {
            //0.0 全部数据   先按文章编号排序,再按时间排序
            List <Comment> DataSource = commentService.GetDataListBy(c => true, c => c.CmtArtId);
            //DataSource = DataSource.OrderBy(c => c.SubTime).ToList();


            //1.0 首先获取datatable提交过来的参数
            string echo      = tableParam.sEcho;                                                               //用于客户端自己的校验
            int    dataStart = tableParam.iDisplayStart;                                                       //要请求的该页第一条数据的序号
            int    pageSize  = tableParam.iDisplayLength == -1 ? DataSource.Count : tableParam.iDisplayLength; //每页容量(=-1表示取全部数据)
            string search    = tableParam.sSearch;

            //2.0 根据参数(起始序号、每页容量、查询参数)查询数据
            if (!String.IsNullOrEmpty(search))
            {
                var data = DataSource.Where(c => c.CmtText.Contains(search))
                           .Skip <Comment>(dataStart)
                           .Take(pageSize)
                           .Select(c => new
                {
                    Id         = c.Id,
                    ArticleId  = c.CmtArtId,
                    CmtText    = c.CmtText,
                    CmterName  = c.Visitor.VisitorName,
                    CmterEmail = c.Visitor.VisitorEmail,
                    SubTime    = c.SubTime.ToString(),
                    Status     = c.Status
                }).ToList();

                //3.0 构造datatable所需要的数据json对象...aaData里面应是一个二维数组:即里面是一个数组[["","",""],[],[],[]]
                return(Json(new
                {
                    sEcho = echo,
                    iTotalRecords = DataSource.Count(),
                    iTotalDisplayRecords = DataSource.Count(),
                    aaData = data
                }, JsonRequestBehavior.AllowGet));
            }
            else
            {
                var data = DataSource.Skip <Comment>(dataStart)
                           .Take(pageSize)
                           .Select(c => new
                {
                    Id         = c.Id,
                    ArticleId  = c.CmtArtId,
                    CmtText    = c.CmtText,
                    CmterName  = c.Visitor.VisitorName,
                    CmterEmail = c.Visitor.VisitorEmail,
                    SubTime    = c.SubTime.ToString(),
                    Status     = c.Status
                }).ToList();

                //3.0 构造datatable所需要的数据json对象...aaData里面应是一个二维数组:即里面是一个数组[["","",""],[],[],[]]
                return(Json(new
                {
                    sEcho = echo,
                    iTotalRecords = DataSource.Count(),
                    iTotalDisplayRecords = DataSource.Count(),
                    aaData = data
                }, JsonRequestBehavior.AllowGet));
            }
        }
Пример #4
0
        /// <summary>
        /// 2.0 Index页-Ajax获取首页博文列表;显示5条
        /// </summary>
        /// <param name="id">博文类别(UrlParameter.optional)</param>
        /// <returns>JSON</returns>
        public ActionResult WrapArtList(int id)
        {
            int pageIndex = Request["pageIndex"] == null ? 1 : int.Parse(Request["pageIndex"]);
            int pageSize  = Request["pageSize"] == null ? 8 : int.Parse(Request["pageSize"]);

            List <Article> articleList = new List <Article>();
            int            totalCount  = 0;

            if (id == 0) //取所有博文,对应于“首页”<Home/Index/0>
            {
                articleList = articleService.GetPagedList(pageIndex, pageSize, a => a.Status == 1, a => a.SubTime, true);

                totalCount = articleService.GetDataListBy(a => a.Status == 1).Count; //总条数
            }
            else  //取响应板块的博文,对应于“导航栏”<Home/Index/6>
            {
                articleList = articleService.GetPagedList(pageIndex, pageSize, a => a.CategoryId == id && a.Status == 1, a => a.SubTime, true);

                totalCount = articleService.GetDataListBy(a => a.CategoryId == id && a.Status == 1).Count; //总条数
            }

            //视图模型需要类别名称和博文基本信息
            List <ArticleViewModel> articleViewList = new List <ArticleViewModel>();

            foreach (Article item in articleList)
            {
                //1.0 获取博文类别名
                string categoryName = item.Category.Name;
                //2.0 获取博文关键词  关键词可为空
                string[]      keywords = null;
                List <string> keylist  = new List <string>();
                if (!string.IsNullOrEmpty(item.Keywords))
                {
                    //关键词规则约定:以空格分开
                    keywords = item.Keywords.Split(' ');

                    foreach (var word in keywords)
                    {
                        if (word != " ")
                        {
                            keylist.Add(word);
                        }
                    }
                }
                //取前五个关键词
                keywords = keylist.Take(5).ToArray();

                //3.0 获取评论总数:
                int commentCount = commentService.GetDataListBy(c => c.CmtArtId == item.Id && c.Status == 1).Count;

                //4.0 构造视图模型
                ArticleViewModel artViewModel = new ArticleViewModel()
                {
                    Id           = item.Id,
                    Title        = item.Title,
                    SubTime      = item.SubTime.ToShortDateString(),
                    CategoryName = categoryName,
                    ViewCount    = item.ViewCount,
                    CommentCount = commentCount,
                    Digg         = item.Digg,
                    Contents     = StringHelper.StringCut(item.ContentsRaw, 600), //ContentsRaw为不包含html标签的文本
                    Keywords     = keywords                                       //可能为空,为空则View中不显示
                };

                articleViewList.Add(artViewModel);
            }
            //5.0 构造分页html-json
            string PagerNavString = PagerHelper.GeneratePagerString(pageIndex, pageSize, totalCount);

            JsonModel jsonData = new JsonModel()
            {
                CoreData   = articleViewList,
                Status     = 1,
                Message    = "成功",
                GotoUrl    = HttpContext.Request.Url.AbsolutePath,
                PageNavStr = PagerNavString
            };

            return(Json(jsonData, JsonRequestBehavior.AllowGet));//Json方法默认只接受post请求
        }
Пример #5
0
        /// <summary>
        /// 5.0 获取侧边栏最新评论,取6条.
        /// </summary>
        /// <returns>List集合</returns>
        public ActionResult NewComments()
        {
            IBLL.ICommentService commentService = OperateHelper.Current.serviceSession.CommentService;

            List <Comment>          newCommentsList  = commentService.GetDataListBy(c => c.Status != 0, c => c.SubTime);
            List <CommentViewModel> cmtViewModelList = new List <CommentViewModel>(); //视图模型

            int j = 6;                                                                //取6条还是6条内

            //如果最新评论说超过6条,则:
            if (newCommentsList.Count > 0)
            {
                if (newCommentsList.Count < 6)
                {
                    j = newCommentsList.Count;
                }

                //newCommentsList = newCommentsList.Take<Comment>(j) as List<Comment>;
                newCommentsList = newCommentsList.OrderByDescending(c => c.SubTime).Take <Comment>(j).ToList();
                string iconUrl; //评论所有人的头像路径
                string defaultIconPath = ConfigurationManager.AppSettings["DefaultHeadIcon"];

                for (int i = 0; i < j; i++)
                {
                    //1.找评论所属游客的Id
                    int?cmtVisitorId = newCommentsList[i].VisitorId;   //Comment实体的VisitorId可空,因为被设计为:删除用户并不级联删除响应评论。
                    //2.如果这条评论所属的游客还存在数据库中
                    if (cmtVisitorId.HasValue)
                    {
                        //2.1 先找到它
                        int?headiconId = newCommentsList[i].Visitor.VisitorIconId;

                        //2.2 如果这个游客对应的头像没被删除
                        if (headiconId.HasValue)
                        {
                            //找出这个头像地址
                            iconUrl = newCommentsList[i].Visitor.HeadIcon.IconURL;
                        }
                        else
                        {
                            iconUrl = defaultIconPath;  //加上~才能显示吗?www.zynblog.com
                        }
                    }
                    else
                    {
                        iconUrl = defaultIconPath;  //否则就给这条评论加一个默认头像
                    }
                    //3.0 构造视图模型
                    CommentViewModel viewmodel = new CommentViewModel()
                    {
                        CmtId      = newCommentsList[i].Id,
                        CmtText    = newCommentsList[i].CmtText,
                        CmtArtId   = newCommentsList[i].CmtArtId,
                        CmtIconUrl = iconUrl
                    };

                    cmtViewModelList.Add(viewmodel);
                }
            }

            return(PartialView(cmtViewModelList));
        }
Пример #6
0
        /// <summary>
        /// 2.0 jquery.datatable()插件 Ajax Get文章列表
        /// </summary>
        /// <returns>datatable-JSON</returns>
        public JsonResult GetArchivesJson(jqDataTableParameter tableParam)
        {
            #region 1.0 场景一:服务端一次性取出所有数据,完全由客户端来处理这些数据.此时"bServerSide": false,
            ////1. 获取所有文章
            //List<Article> DataSource = articleService.GetDataListBy(a => true, a => a.Id);
            ////2. 构造aaData
            //var data = DataSource.Select(a => new object[]{
            //  a.Id,
            //  a.Title+ "  ("+a.SubTime.ToString()+")",
            //  (categoryService.GetDataListBy(c=>c.Id==a.CategoryId)[0]).Name,
            //  a.ViewCount,
            //  commentService.GetDataListBy(c=>c.CmtArtId==a.Id).Count,
            //  a.Digg,
            //  a.Status==1?"正常":"删除"

            //});
            ////3. 返回json,aaData是一个数组,数组里面还是字符串数组
            //return Json(new
            //{
            //    sEcho = 1,
            //    iTotalRecords = DataSource.Count,
            //    iTotalDisplayRecords = data.Count(),
            //    aaData = data
            //}, JsonRequestBehavior.AllowGet);
            #endregion

            #region 2.0 场景二:服务端处理分页后数据,客户端呈现,此时为true
            //客户端需要"bServerSide": true, 用mDataProp绑定字段,obj.aData.Id获取字段(.属性)

            //0.0 全部数据
            List <Article> DataSource = articleService.GetDataListBy(a => true);
            //DataSource = DataSource.OrderByDescending(a => a.SubTime).ToList();

            //1.0 首先获取datatable提交过来的参数
            string echo      = tableParam.sEcho;                                                               //用于客户端自己的校验
            int    dataStart = tableParam.iDisplayStart;                                                       //要请求的该页第一条数据的序号
            int    pageSize  = tableParam.iDisplayLength == -1 ? DataSource.Count : tableParam.iDisplayLength; //每页容量(=-1表示取全部数据)
            string search    = tableParam.sSearch;

            //2.0 根据参数(起始序号、每页容量、参训参数)查询数据
            if (!String.IsNullOrEmpty(search))
            {
                var data = DataSource.Where(a => a.Title.Contains(search) ||
                                            a.Keywords.Contains(search) ||
                                            a.Contents.Contains(search))
                           .Skip <Article>(dataStart)
                           .Take(pageSize)
                           .Select(a => new
                {
                    Id           = a.Id,
                    Title        = a.Title + "  (" + a.SubTime.ToString() + ")",
                    CategoryName = a.Category.Name,
                    ViewCount    = a.ViewCount,
                    CommentCount = commentService.GetDataListBy(c => c.CmtArtId == a.Id).Count,
                    Digg         = a.Digg,
                    Status       = a.Status
                }).ToList();

                //3.0 构造datatable所需要的数据json对象...aaData里面应是一个二维数组:即里面是一个数组[["","",""],[],[],[]]
                return(Json(new
                {
                    sEcho = echo,
                    iTotalRecords = DataSource.Count(),
                    iTotalDisplayRecords = DataSource.Count(),
                    aaData = data
                }, JsonRequestBehavior.AllowGet));
            }
            else
            {
                var data = DataSource.Skip <Article>(dataStart)
                           .Take(pageSize)
                           .Select(a => new
                {
                    Id           = a.Id,
                    Title        = a.Title + "  (" + a.SubTime.ToString() + ")",
                    CategoryName = a.Category.Name,
                    ViewCount    = a.ViewCount,
                    CommentCount = commentService.GetDataListBy(c => c.CmtArtId == a.Id).Count,
                    Digg         = a.Digg,
                    Status       = a.Status
                }).ToList();

                //3.0 构造datatable所需要的数据json对象...aaData里面应是一个二维数组:即里面是一个数组[["","",""],[],[],[]]
                return(Json(new
                {
                    sEcho = echo,
                    iTotalRecords = DataSource.Count(),
                    iTotalDisplayRecords = DataSource.Count(),
                    aaData = data
                }, JsonRequestBehavior.AllowGet));
            }
            #endregion
        }