public async Task <ActionResult> Index([Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")] int page = 1, [Range(1, 50, ErrorMessage = "页大小必须在0到50之间")] int size = 15)
        {
            var list = await NoticeService.GetPagesFromCacheAsync <DateTime, NoticeDto>(page, size, n => n.Status == Status.Display, n => n.ModifyDate, false);

            ViewData["page"] = new Pagination(page, size, list.TotalCount);
            foreach (var n in list.Data)
            {
                n.ModifyDate = n.ModifyDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone));
                n.PostDate   = n.PostDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone));
            }

            return(CurrentUser.IsAdmin ? View("Index_Admin", list.Data) : View(list.Data));
        }
示例#2
0
        public async Task <ActionResult> Index([Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")] int page = 1, [Range(1, 50, ErrorMessage = "页大小必须在0到50之间")] int size = 15)
        {
            var list = await NoticeService.GetPagesFromCacheAsync <DateTime, NoticeDto>(page, size, n => n.NoticeStatus == NoticeStatus.Normal, n => n.ModifyDate, false);

            ViewData["page"] = new Pagination(page, size, list.TotalCount);
            foreach (var n in list.Data)
            {
                n.ModifyDate = n.ModifyDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone));
                n.PostDate   = n.PostDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone));
                n.Content    = ReplaceVariables(n.Content);
            }

            ViewBag.Ads = AdsService.GetByWeightedPrice(AdvertiseType.ListItem, Request.Location());
            return(CurrentUser.IsAdmin ? View("Index_Admin", list.Data) : View(list.Data));
        }
示例#3
0
        /// <summary>
        /// 获取页面视图模型
        /// </summary>
        /// <returns></returns>
        private async Task <HomePageViewModel> GetIndexPageViewModel()
        {
            var postsQuery = PostService.GetQuery <PostDto>(p => (p.Status == Status.Published || CurrentUser.IsAdmin));                                                                 //准备文章的查询
            var notices    = await NoticeService.GetPagesFromCacheAsync <DateTime, NoticeDto>(1, 5, n => (n.Status == Status.Display || CurrentUser.IsAdmin), n => n.ModifyDate, false); //加载前5条公告

            var cats = await CategoryService.GetQueryFromCacheAsync <string, CategoryDto>(c => c.Status == Status.Available, c => c.Name);                                               //加载分类目录

            var hotSearches = RedisHelper.Get <List <KeywordsRank> >("SearchRank:Week").Take(10).ToList();                                                                               //热词统计
            var hot6Post    = await postsQuery.OrderBy((new Random().Next() % 3) switch
            {
                1 => nameof(OrderBy.VoteUpCount),
                2 => nameof(OrderBy.AverageViewCount),
                _ => nameof(OrderBy.TotalViewCount)
            } +" desc").Skip(0).Take(5).FromCacheAsync();                                                                                                                                                                        //热门文章

            var newdic = new Dictionary <string, int>();                                                                                                                                                                         //标签云最终结果
            var tagdic = postsQuery.Where(p => !string.IsNullOrEmpty(p.Label)).Select(p => p.Label).Distinct().FromCache().ToList().SelectMany(s => s.Split(',', ',')).GroupBy(s => s).ToDictionary(g => g.Key, g => g.Count()); //统计标签

            if (tagdic.Any())
            {
                var min = tagdic.Values.Min();
                foreach (var(key, value) in tagdic)
                {
                    var fontsize = (int)Math.Floor(value * 1.0 / (min * 1.0) + 12.0);
                    newdic.Add(key, fontsize >= 36 ? 36 : fontsize);
                }
            }

            return(new HomePageViewModel()
            {
                Categories = cats.ToList(),
                HotSearch = hotSearches,
                Notices = notices.Data,
                Tags = newdic,
                Top6Post = hot6Post.ToList(),
                PostsQueryable = postsQuery,
                SidebarAds = AdsService.GetsByWeightedPrice(2, AdvertiseType.SideBar),
                ListAdvertisement = AdsService.GetByWeightedPrice(AdvertiseType.PostList)
            });
        }