Exemplo n.º 1
0
        public async Task <ActionResult> HistoryVersion(int id, int hid)
        {
            var post = await PostHistoryVersionService.GetAsync(v => v.Id == hid && (v.Post.Status == Status.Published || CurrentUser.IsAdmin)) ?? throw new NotFoundException("文章未找到");

            CheckPermission(post.Post);
            post.Content        = ReplaceVariables(post.Content);
            post.ProtectContent = ReplaceVariables(post.ProtectContent);
            var next = await PostHistoryVersionService.GetAsync(p => p.PostId == id && p.ModifyDate > post.ModifyDate, p => p.ModifyDate);

            var prev = await PostHistoryVersionService.GetAsync(p => p.PostId == id && p.ModifyDate < post.ModifyDate, p => p.ModifyDate, false);

            ViewBag.Next = next;
            ViewBag.Prev = prev;
            ViewBag.Ads  = AdsService.GetByWeightedPrice(AdvertiseType.InPage, post.CategoryId);
            return(CurrentUser.IsAdmin ? View("HistoryVersion_Admin", post) : View(post));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> History(int id, [Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")] int page = 1, [Range(1, 50, ErrorMessage = "页大小必须在0到50之间")] int size = 20)
        {
            var post = await PostService.GetAsync(p => p.Id == id && (p.Status == Status.Published || CurrentUser.IsAdmin)) ?? throw new NotFoundException("文章未找到");

            CheckPermission(post);
            ViewBag.Primary = post;
            var list = PostHistoryVersionService.GetPages(page, size, v => v.PostId == id, v => v.ModifyDate, false);

            foreach (var item in list.Data)
            {
                item.ModifyDate = item.ModifyDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone));
            }

            ViewBag.Ads = AdsService.GetByWeightedPrice(AdvertiseType.InPage, post.CategoryId);
            return(View(list));
        }
Exemplo n.º 3
0
        public async Task <ActionResult> Category(int id, [Optional] OrderBy?orderBy, [Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")] int page = 1, [Range(1, 50, ErrorMessage = "页大小必须在0到50之间")] int size = 15)
        {
            var cat = await CategoryService.GetByIdAsync(id) ?? throw new NotFoundException("文章分类未找到");

            var posts = PostService.GetQuery <PostDto>(p => p.CategoryId == cat.Id && p.Status == Status.Published).OrderBy($"{nameof(PostDto.IsFixedTop)} desc,{(orderBy ?? OrderBy.ModifyDate).GetDisplay()} desc").ToCachedPagedList(page, size);

            CheckPermission(posts);
            var viewModel = await GetIndexPageViewModel();

            viewModel.Posts             = posts;
            ViewBag.Category            = cat;
            viewModel.PageParams        = new Pagination(page, size, posts.TotalCount, orderBy);
            viewModel.SidebarAds        = AdsService.GetsByWeightedPrice(2, AdvertiseType.SideBar, id);
            viewModel.ListAdvertisement = AdsService.GetByWeightedPrice(AdvertiseType.PostList, id);
            return(View(viewModel));
        }
        public ActionResult Search(string wd = "", [Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")] int page = 1, [Range(1, 50, ErrorMessage = "页大小必须在0到50之间")] int size = 15)
        {
            var nul = new SearchResult <PostDto>();

            ViewBag.PageSize = size;
            ViewBag.Keyword  = wd;
            string ip  = ClientIP;
            string key = "Search:" + ip;

            if (CacheManager.Exists(key) && CacheManager.Get(key) != wd)
            {
                var hotSearches = RedisHelper.Get <List <KeywordsRank> >("SearchRank:Week").Take(10).ToList();
                ViewBag.hotSearches = hotSearches;
                ViewBag.ErrorMsg    = "10秒内只能搜索1次!";
                return(View(nul));
            }

            wd = wd?.Trim().Replace("+", " ");
            if (!string.IsNullOrWhiteSpace(wd) && !wd.Contains("锟斤拷"))
            {
                if (!HttpContext.Session.TryGetValue("search:" + wd, out _) && !HttpContext.Request.IsRobot())
                {
                    SearchDetailsService.AddEntity(new SearchDetails
                    {
                        Keywords   = wd,
                        SearchTime = DateTime.Now,
                        IP         = ClientIP
                    });
                    SearchDetailsService.SaveChanges();
                    HttpContext.Session.Set("search:" + wd, wd.ToByteArray());
                }

                var posts = PostService.SearchPage(page, size, wd);
                if (posts.Total > 1)
                {
                    CacheManager.AddOrUpdate(key, wd, s => wd);
                    CacheManager.Expire(key, TimeSpan.FromSeconds(10));
                }

                ViewBag.Ads         = AdsService.GetByWeightedPrice(AdvertiseType.PostList);
                ViewBag.hotSearches = new List <KeywordsRank>();
                return(View(posts));
            }

            ViewBag.hotSearches = RedisHelper.Get <List <KeywordsRank> >("SearchRank:Week").Take(10).ToList();
            return(View(nul));
        }
Exemplo n.º 5
0
        public async Task <ActionResult> Author(string author, [Optional] OrderBy?orderBy, [Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")] int page = 1, [Range(1, 50, ErrorMessage = "页大小必须在0到50之间")] int size = 15)
        {
            Expression <Func <Post, bool> > where = p => p.Author.Equals(author) || p.Modifier.Equals(author) || p.Email.Equals(author) || p.PostHistoryVersion.Any(v => v.Modifier.Equals(author) || v.ModifierEmail.Equals(author));
            where = where.And(p => p.Status == Status.Published);
            var posts = PostService.GetQuery <PostDto>(where).OrderBy($"{nameof(PostDto.IsFixedTop)} desc,{(orderBy ?? OrderBy.ModifyDate).GetDisplay()} desc").ToCachedPagedList(page, size);

            CheckPermission(posts);
            var viewModel = await GetIndexPageViewModel();

            ViewBag.Author              = author;
            ViewBag.Total               = posts.TotalCount;
            viewModel.Posts             = posts;
            viewModel.PageParams        = new Pagination(page, size, posts.TotalCount, orderBy);
            viewModel.SidebarAds        = AdsService.GetsByWeightedPrice(2, AdvertiseType.SideBar);
            viewModel.ListAdvertisement = AdsService.GetByWeightedPrice(AdvertiseType.PostList);
            return(View(viewModel));
        }
Exemplo n.º 6
0
        public async Task <ActionResult> Details(int id)
        {
            var notice = await NoticeService.GetByIdAsync(id) ?? throw new NotFoundException("页面未找到");

            if (!HttpContext.Session.TryGetValue("notice" + id, out _))
            {
                notice.ViewCount += 1;
                await NoticeService.SaveChangesAsync();

                HttpContext.Session.Set("notice" + id, notice.Title);
            }

            notice.ModifyDate = notice.ModifyDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone));
            notice.PostDate   = notice.PostDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone));
            ViewBag.Ads       = AdsService.GetByWeightedPrice(AdvertiseType.InPage);
            return(View(notice));
        }
Exemplo n.º 7
0
        public async Task <ActionResult> Search([FromServices] IPostService postService, string wd = "", [Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")] int page = 1, [Range(1, 50, ErrorMessage = "页大小必须在0到50之间")] int size = 15)
        {
            wd = ChineseConverter.Convert(wd?.Trim() ?? "", ChineseConversionDirection.TraditionalToSimplified);
            ViewBag.PageSize = size;
            ViewBag.Keyword  = wd;
            string key = "Search:" + ClientIP;

            if (CacheManager.Exists(key) && CacheManager.Get(key) != wd)
            {
                var hotSearches = RedisHelper.Get <List <KeywordsRank> >("SearchRank:Week").Take(10).ToList();
                ViewBag.hotSearches = hotSearches;
                ViewBag.ErrorMsg    = "10秒内只能搜索1次!";
                return(View(new SearchResult <PostDto>()));
            }

            if (!string.IsNullOrWhiteSpace(wd) && !wd.Contains("锟斤拷"))
            {
                new JiebaSegmenter().AddWord(wd);
                if (!HttpContext.Session.TryGetValue("search:" + wd, out _) && !HttpContext.Request.IsRobot())
                {
                    SearchDetailsService.AddEntity(new SearchDetails
                    {
                        Keywords   = wd,
                        SearchTime = DateTime.Now,
                        IP         = ClientIP
                    });
                    await SearchDetailsService.SaveChangesAsync();

                    HttpContext.Session.Set("search:" + wd, wd.ToByteArray());
                }

                var posts = postService.SearchPage(page, size, wd);
                if (posts.Total > 1)
                {
                    CacheManager.AddOrUpdate(key, wd, s => wd);
                    CacheManager.Expire(key, TimeSpan.FromSeconds(10));
                    ViewBag.Ads = AdsService.GetByWeightedPrice(AdvertiseType.PostList);
                }

                ViewBag.hotSearches = new List <KeywordsRank>();
                return(View(posts));
            }

            ViewBag.hotSearches = RedisHelper.Get <List <KeywordsRank> >("SearchRank:Week").Take(10).ToList();
            return(View(new SearchResult <PostDto>()));
        }
Exemplo n.º 8
0
        public async Task <ActionResult> Details(int id, string kw)
        {
            var post = await PostService.GetAsync(p => p.Id == id && (p.Status == Status.Published || CurrentUser.IsAdmin)) ?? throw new NotFoundException("文章未找到");

            CheckPermission(post);
            ViewBag.Keyword = post.Keyword + "," + post.Label;
            ViewBag.Desc    = await post.Content.GetSummary(200);

            var modifyDate = post.ModifyDate;

            ViewBag.Next = await PostService.GetFromCacheAsync <DateTime, PostModelBase>(p => p.ModifyDate > modifyDate && (p.Status == Status.Published || CurrentUser.IsAdmin), p => p.ModifyDate);

            ViewBag.Prev = await PostService.GetFromCacheAsync <DateTime, PostModelBase>(p => p.ModifyDate < modifyDate && (p.Status == Status.Published || CurrentUser.IsAdmin), p => p.ModifyDate, false);

            if (!string.IsNullOrEmpty(kw))
            {
                ViewData["keywords"] = post.Content.Contains(kw) ? $"['{kw}']" : SearchEngine.LuceneIndexSearcher.CutKeywords(kw).ToJsonString();
            }

            ViewBag.Ads = AdsService.GetByWeightedPrice(AdvertiseType.InPage, post.CategoryId);
            var related = PostService.ScoreSearch(1, 11, string.IsNullOrWhiteSpace(post.Keyword + post.Label) ? post.Title : post.Keyword + post.Label);

            related.RemoveAll(p => p.Id == id);
            if (related.Count <= 1)
            {
                related = (await PostService.GetPagesFromCacheAsync(1, 10, p => p.Id != id && p.CategoryId == post.CategoryId, p => p.TotalViewCount, false)).Data;
            }

            ViewBag.Related     = related;
            post.ModifyDate     = post.ModifyDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone));
            post.PostDate       = post.PostDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone));
            post.Content        = ReplaceVariables(post.Content);
            post.ProtectContent = ReplaceVariables(post.ProtectContent);
            if (CurrentUser.IsAdmin)
            {
                return(View("Details_Admin", post));
            }

            if (!HttpContext.Request.IsRobot() && string.IsNullOrEmpty(HttpContext.Session.Get <string>("post" + id)))
            {
                HangfireHelper.CreateJob(typeof(IHangfireBackJob), nameof(HangfireBackJob.RecordPostVisit), args: id);
                HttpContext.Session.Set("post" + id, id.ToString());
            }

            return(View(post));
        }
Exemplo n.º 9
0
        public async Task <ActionResult> Post([Optional] OrderBy?orderBy, [Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")] int page = 1, [Range(1, 50, ErrorMessage = "页大小必须在0到50之间")] int size = 15)
        {
            var viewModel = await GetIndexPageViewModel();

            var postsQuery = PostService.GetQuery <PostDto>(p => p.Status == Status.Published); //准备文章的查询
            var posts      = await postsQuery.Where(p => !p.IsFixedTop).OrderBy((orderBy ?? OrderBy.ModifyDate).GetDisplay() + " desc").ToCachedPagedListAsync(page, size);

            if (page == 1)
            {
                posts.Data.InsertRange(0, postsQuery.Where(p => p.IsFixedTop).OrderByDescending(p => p.ModifyDate).ToList());
            }

            CheckPermission(posts);
            viewModel.Posts             = posts;
            viewModel.PageParams        = new Pagination(page, size, posts.TotalCount, orderBy);
            viewModel.SidebarAds        = AdsService.GetsByWeightedPrice(2, AdvertiseType.SideBar);
            viewModel.ListAdvertisement = AdsService.GetByWeightedPrice(AdvertiseType.PostList);
            return(View(viewModel));
        }
Exemplo n.º 10
0
        public async Task <ActionResult> Index()
        {
            var banners    = AdsService.GetsByWeightedPrice(8, AdvertiseType.Banner).OrderBy(a => Guid.NewGuid()).ToList();
            var fastShares = await FastShareService.GetAllFromCacheAsync(s => s.Sort);

            var postsQuery = PostService.GetQuery <PostDto>(p => p.Status == Status.Published); //准备文章的查询
            var posts      = await postsQuery.Where(p => !p.IsFixedTop).OrderBy(OrderBy.ModifyDate.GetDisplay() + " desc").ToCachedPagedListAsync(1, 15);

            posts.Data.InsertRange(0, postsQuery.Where(p => p.IsFixedTop).OrderByDescending(p => p.ModifyDate).ToList());
            CheckPermission(posts);
            var viewModel = await GetIndexPageViewModel();

            viewModel.Banner            = banners;
            viewModel.Posts             = posts;
            ViewBag.FastShare           = fastShares.ToList();
            viewModel.PageParams        = new Pagination(1, 15, posts.TotalCount, OrderBy.ModifyDate);
            viewModel.SidebarAds        = AdsService.GetsByWeightedPrice(2, AdvertiseType.SideBar);
            viewModel.ListAdvertisement = AdsService.GetByWeightedPrice(AdvertiseType.PostList);
            return(View(viewModel));
        }
Exemplo n.º 11
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)
            });
        }
        public async Task <ActionResult> RandomGo()
        {
            var ad = AdsService.GetByWeightedPrice((AdvertiseType) new Random().Next(1, 4), Request.Location());

            if (!Request.IsRobot() && string.IsNullOrEmpty(HttpContext.Session.Get <string>("ads" + ad.Id)))
            {
                HttpContext.Session.Set("ads" + ad.Id, ad.Id.ToString());
                ad.ClickRecords.Add(new AdvertisementClickRecord()
                {
                    IP       = ClientIP,
                    Location = ClientIP.GetIPLocation(),
                    Referer  = Request.Headers[HeaderNames.Referer].ToString(),
                    Time     = DateTime.Now
                });
                await AdsService.SaveChangesAsync();

                var start = DateTime.Today.AddMonths(-1);
                await ClickRecordService.GetQuery(a => a.Time < start).DeleteFromQueryAsync();
            }

            return(Redirect(ad.Url));
        }
Exemplo n.º 13
0
        public async Task <ActionResult> Index([FromServices] IFastShareService fastShareService)
        {
            var banners    = AdsService.GetsByWeightedPrice(8, AdvertiseType.Banner, Request.Location()).OrderByRandom().ToList();
            var fastShares = await fastShareService.GetAllFromCacheAsync(s => s.Sort);

            var postsQuery = PostService.GetQuery(PostBaseWhere().And(p => p.Status == Status.Published)); //准备文章的查询
            var posts      = await postsQuery.Where(p => !p.IsFixedTop).OrderBy(OrderBy.ModifyDate.GetDisplay() + " desc").ToCachedPagedListAsync <Post, PostDto>(1, 15, MapperConfig);

            posts.Data.InsertRange(0, postsQuery.Where(p => p.IsFixedTop).OrderByDescending(p => p.ModifyDate).ProjectTo <PostDto>(MapperConfig).Cacheable().ToList());
            var viewModel = await GetIndexPageViewModel();

            viewModel.Banner            = banners;
            viewModel.Posts             = posts;
            ViewBag.FastShare           = fastShares;
            viewModel.PageParams        = new Pagination(1, 15, posts.TotalCount, OrderBy.ModifyDate);
            viewModel.SidebarAds        = AdsService.GetsByWeightedPrice(2, AdvertiseType.SideBar, Request.Location());
            viewModel.ListAdvertisement = AdsService.GetByWeightedPrice(AdvertiseType.ListItem, Request.Location());
            foreach (var item in posts.Data)
            {
                item.ModifyDate = item.ModifyDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone));
            }

            return(View(viewModel));
        }
Exemplo n.º 14
0
        public async Task <ActionResult> Category(int id, [Optional] OrderBy?orderBy, [Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")] int page = 1, [Range(1, 50, ErrorMessage = "页大小必须在0到50之间")] int size = 15)
        {
            var cat = await CategoryService.GetByIdAsync(id) ?? throw new NotFoundException("文章分类未找到");

            var h24   = DateTime.Today.AddDays(-1);
            var posts = orderBy switch
            {
                OrderBy.Trending => await PostService.GetQuery(PostBaseWhere().And(p => p.CategoryId == cat.Id && p.Status == Status.Published)).OrderByDescending(p => p.PostVisitRecordStats.Where(e => e.Date >= h24).Sum(e => e.Count)).ToCachedPagedListAsync <Post, PostDto>(page, size, MapperConfig),
                _ => await PostService.GetQuery(PostBaseWhere().And(p => p.CategoryId == cat.Id && p.Status == Status.Published)).OrderBy($"{nameof(PostDto.IsFixedTop)} desc,{(orderBy ?? OrderBy.ModifyDate).GetDisplay()} desc").ToCachedPagedListAsync <Post, PostDto>(page, size, MapperConfig)
            };
            var viewModel = await GetIndexPageViewModel();

            viewModel.Posts             = posts;
            ViewBag.Category            = cat;
            viewModel.PageParams        = new Pagination(page, size, posts.TotalCount, orderBy);
            viewModel.SidebarAds        = AdsService.GetsByWeightedPrice(2, AdvertiseType.SideBar, Request.Location(), id);
            viewModel.ListAdvertisement = AdsService.GetByWeightedPrice(AdvertiseType.ListItem, Request.Location(), id);
            foreach (var item in posts.Data)
            {
                item.ModifyDate = item.ModifyDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone));
            }

            return(View(viewModel));
        }
Exemplo n.º 15
0
        public async Task <ActionResult> Author(string author, [Optional] OrderBy?orderBy, [Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")] int page = 1, [Range(1, 50, ErrorMessage = "页大小必须在0到50之间")] int size = 15)
        {
            Expression <Func <Post, bool> > where = p => p.Author.Equals(author) || p.Modifier.Equals(author) || p.Email.Equals(author) || p.PostHistoryVersion.Any(v => v.Modifier.Equals(author) || v.ModifierEmail.Equals(author));
            where = where.And(p => p.Status == Status.Published).And(PostBaseWhere());
            var h24   = DateTime.Today.AddDays(-1);
            var posts = orderBy switch
            {
                OrderBy.Trending => await PostService.GetQuery(where).OrderByDescending(p => p.PostVisitRecordStats.Where(e => e.Date >= h24).Sum(e => e.Count)).ToCachedPagedListAsync <Post, PostDto>(page, size, MapperConfig),
                _ => await PostService.GetQuery(where).OrderBy($"{nameof(PostDto.IsFixedTop)} desc,{(orderBy ?? OrderBy.ModifyDate).GetDisplay()} desc").ToCachedPagedListAsync <Post, PostDto>(page, size, MapperConfig)
            };
            var viewModel = await GetIndexPageViewModel();

            ViewBag.Author              = author;
            viewModel.Posts             = posts;
            viewModel.PageParams        = new Pagination(page, size, posts.TotalCount, orderBy);
            viewModel.SidebarAds        = AdsService.GetsByWeightedPrice(2, AdvertiseType.SideBar, Request.Location());
            viewModel.ListAdvertisement = AdsService.GetByWeightedPrice(AdvertiseType.ListItem, Request.Location());
            foreach (var item in posts.Data)
            {
                item.ModifyDate = item.ModifyDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone));
            }

            return(View(viewModel));
        }
Exemplo n.º 16
0
        /// <summary>
        /// 获取页面视图模型
        /// </summary>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <param name="orderBy"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        private IndexPageViewModel GetIndexPageViewModel(int page, int size, OrderBy?orderBy, UserInfoOutputDto user)
        {
            var postsQuery  = PostService.GetQuery <PostOutputDto>(p => (p.Status == Status.Pended || user.IsAdmin));                                                                             //准备文章的查询
            var notices     = NoticeService.GetPagesFromCache <DateTime, NoticeOutputDto>(1, 5, out int _, n => (n.Status == Status.Display || user.IsAdmin), n => n.ModifyDate, false).ToList(); //加载前5条公告
            var cats        = CategoryService.GetQueryFromCache <string, CategoryOutputDto>(c => c.Status == Status.Available, c => c.Name).ToList();                                             //加载分类目录
            var hotSearches = RedisHelper.Get <List <KeywordsRankOutputDto> >("SearchRank:Week").Take(10).ToList();                                                                               //热词统计
            Expression <Func <PostOutputDto, double> > order = p => p.TotalViewCount;

            switch (new Random().Next() % 3)
            {
            case 1:
                order = p => p.VoteUpCount;
                break;

            case 2:
                order = p => p.AverageViewCount;
                break;
            }
            var hot6Post = postsQuery.OrderByDescending(order).Skip(0).Take(5).Cacheable().ToList();                                                                                                                          //热门文章
            var newdic   = new Dictionary <string, int>();                                                                                                                                                                    //标签云最终结果
            var tagdic   = postsQuery.Where(p => !string.IsNullOrEmpty(p.Label)).Select(p => p.Label).Cacheable().AsEnumerable().SelectMany(s => s.Split(',', ',')).GroupBy(s => s).ToDictionary(g => g.Key, g => g.Count()); //统计标签

            if (tagdic.Any())
            {
                int min = tagdic.Values.Min();
                tagdic.ForEach(kv =>
                {
                    var fontsize = (int)Math.Floor(kv.Value * 1.0 / (min * 1.0) + 12.0);
                    newdic.Add(kv.Key, fontsize >= 36 ? 36 : fontsize);
                });
            }
            IList <PostOutputDto> posts;

            switch (orderBy) //文章排序
            {
            case OrderBy.CommentCount:
                posts = postsQuery.Where(p => !p.IsFixedTop).OrderByDescending(p => p.CommentCount).Skip(size * (page - 1)).Take(size).Cacheable().ToList();
                break;

            case OrderBy.PostDate:
                posts = postsQuery.Where(p => !p.IsFixedTop).OrderByDescending(p => p.PostDate).Skip(size * (page - 1)).Take(size).Cacheable().ToList();
                break;

            case OrderBy.ViewCount:
                posts = postsQuery.Where(p => !p.IsFixedTop).OrderByDescending(p => p.TotalViewCount).Skip(size * (page - 1)).Take(size).Cacheable().ToList();
                break;

            case OrderBy.VoteCount:
                posts = postsQuery.Where(p => !p.IsFixedTop).OrderByDescending(p => p.VoteUpCount).Skip(size * (page - 1)).Take(size).Cacheable().ToList();
                break;

            case OrderBy.AverageViewCount:
                posts = postsQuery.Where(p => !p.IsFixedTop).OrderByDescending(p => p.AverageViewCount).Skip(size * (page - 1)).Take(size).Cacheable().ToList();
                break;

            default:
                posts = postsQuery.Where(p => !p.IsFixedTop).OrderByDescending(p => p.ModifyDate).Skip(size * (page - 1)).Take(size).Cacheable().ToList();
                break;
            }
            if (page == 1)
            {
                posts = postsQuery.Where(p => p.IsFixedTop).OrderByDescending(p => p.ModifyDate).AsEnumerable().Union(posts).ToList();
            }
            return(new IndexPageViewModel()
            {
                Categories = cats,
                HotSearch = hotSearches,
                Notices = notices,
                Posts = posts,
                Tags = newdic,
                Top6Post = hot6Post,
                PostsQueryable = postsQuery,
                SidebarAds = AdsService.GetsByWeightedPrice(2, AdvertiseType.SideBar),
                ListAdvertisement = AdsService.GetByWeightedPrice(AdvertiseType.PostList)
            });
        }