Esempio n. 1
0
        public async Task <ActionResult> Donate()
        {
            ViewBag.Ads = AdsService.GetsByWeightedPrice(2, AdvertiseType.InPage);
            var text = await System.IO.File.ReadAllTextAsync(Path.Combine(HostEnvironment.WebRootPath, "template", "donate.html"));

            return(CurrentUser.IsAdmin ? View("Donate_Admin", text) : View(model: text));
        }
Esempio n. 2
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(PostBaseWhere().And(p => p.Status == Status.Published)); //准备文章的查询
            var h24        = DateTime.Today.AddDays(-1);
            var posts      = orderBy switch
            {
                OrderBy.Trending => await postsQuery.Where(p => !p.IsFixedTop).OrderByDescending(p => p.PostVisitRecordStats.Where(e => e.Date >= h24).Sum(t => t.Count)).ToCachedPagedListAsync <Post, PostDto>(page, size, MapperConfig),
                _ => await postsQuery.Where(p => !p.IsFixedTop).OrderBy((orderBy ?? OrderBy.ModifyDate).GetDisplay() + " desc").ToCachedPagedListAsync <Post, PostDto>(page, size, MapperConfig)
            };

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

            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));
        }
Esempio n. 3
0
        public async Task <ActionResult> Archieve([Range(2010, 2099)] int yyyy, [Range(1, 12)] int mm, [Range(1, 31)] int dd, [Optional] OrderBy?orderBy, [Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")] int page = 1, [Range(1, 50, ErrorMessage = "页大小必须在0到50之间")] int size = 15, string mode = nameof(Models.Entity.Post.ModifyDate))
        {
            if (!DateTime.TryParse(yyyy + "-" + mm + "-" + dd, out var date))
            {
                date = DateTime.Today;
            }

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

            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));
        }
Esempio n. 4
0
        public async Task <ActionResult> Tag(string tag, [Optional] OrderBy?orderBy, [Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")] int page = 1, [Range(1, 50, ErrorMessage = "页大小必须在0到50之间")] int size = 15)
        {
            if (string.IsNullOrWhiteSpace(tag))
            {
                throw new NotFoundException("");
            }

            var where = PostBaseWhere().And(p => p.Status == Status.Published);
            var queryable = PostService.GetQuery(tag.Split(",", StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).Aggregate(where, (current, s) => current.And(p => Regex.IsMatch(p.Label, s))));
            var h24       = DateTime.Today.AddDays(-1);
            var posts     = orderBy switch
            {
                OrderBy.Trending => await queryable.OrderByDescending(p => p.PostVisitRecordStats.Where(e => e.Date >= h24).Sum(e => e.Count)).ToCachedPagedListAsync <Post, PostDto>(page, size, MapperConfig),
                _ => await queryable.OrderBy($"{nameof(PostDto.IsFixedTop)} desc,{(orderBy ?? OrderBy.ModifyDate).GetDisplay()} desc").ToCachedPagedListAsync <Post, PostDto>(page, size, MapperConfig)
            };
            var viewModel = await GetIndexPageViewModel();

            ViewBag.Tag                 = tag;
            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));
        }
Esempio n. 5
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());
            PostService.SolvePostsCategory(posts.Data);
            foreach (var item in posts.Data)
            {
                item.ModifyDate = item.ModifyDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone));
            }

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

            var cids  = cat.Flatten().Select(c => c.Id).ToArray();
            var h24   = DateTime.Today.AddDays(-1);
            var posts = orderBy switch
            {
                OrderBy.Trending => await PostService.GetQuery(PostBaseWhere().And(p => cids.Contains(p.CategoryId) && 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 => cids.Contains(p.CategoryId) && 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);
            PostService.SolvePostsCategory(posts.Data);
            foreach (var item in posts.Data)
            {
                item.ModifyDate = item.ModifyDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone));
            }

            return(View(viewModel));
        }
Esempio n. 7
0
        public async Task <ActionResult> Author(string author, [Optional] OrderBy?orderBy, int page = 1, [Range(1, 50, ErrorMessage = "页大小必须在0到50之间")] int size = 15)
        {
            page = Math.Max(1, page);
            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());
            PostService.SolvePostsCategory(posts.Data);
            foreach (var item in posts.Data)
            {
                item.ModifyDate = item.ModifyDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone));
            }

            return(View(viewModel));
        }
Esempio n. 8
0
        public ActionResult Index()
        {
            ViewBag.Total = PostService.Count(p => p.Status == Status.Pended || CurrentUser.IsAdmin);
            var banners    = AdsService.GetsByWeightedPrice(8, AdvertiseType.Banner).OrderBy(a => Guid.NewGuid()).ToList();
            var fastShares = FastShareService.GetAllFromCache(s => s.Sort).ToList();

            ViewBag.FastShare = fastShares;
            var viewModel = GetIndexPageViewModel(1, 15, OrderBy.ModifyDate, CurrentUser);

            viewModel.Banner = banners;
            return(View(viewModel));
        }
Esempio n. 9
0
        public async Task <ActionResult> Donate()
        {
            var ads = AdsService.GetsByWeightedPrice(2, AdvertiseType.InPage, Request.Location());

            if (bool.Parse(CommonHelper.SystemSettings.GetOrAdd("EnableDonate", "true")))
            {
                ViewBag.Ads = ads;
                var text = await new FileInfo(Path.Combine(HostEnvironment.WebRootPath, "template", "donate.html")).ShareReadWrite().ReadAllTextAsync(Encoding.UTF8);
                return(CurrentUser.IsAdmin ? View("Donate_Admin", text) : View(model: text));
            }

            return(Redirect(ads.FirstOrDefault()?.Url ?? "/"));
        }
Esempio n. 10
0
        public async Task <ActionResult> Tag(string id, [Optional] OrderBy?orderBy, [Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")] int page = 1, [Range(1, 50, ErrorMessage = "页大小必须在0到50之间")] int size = 15)
        {
            var posts = await PostService.GetQuery <PostDto>(p => p.Label.Contains(id) && p.Status == Status.Published).OrderBy($"{nameof(PostDto.IsFixedTop)} desc,{(orderBy ?? OrderBy.ModifyDate).GetDisplay()} desc").ToCachedPagedListAsync(page, size);

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

            ViewBag.Tag                 = id;
            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));
        }
Esempio n. 11
0
        public ActionResult Index()
        {
            ViewBag.Total = PostService.Count(p => p.Status == Status.Pended || CurrentUser.IsAdmin);
            var banners    = AdsService.GetsByWeightedPrice(8, AdvertiseType.Banner).OrderBy(a => Guid.NewGuid()).ToList();
            var fastShares = FastShareService.GetAllFromCache(s => s.Sort).ToList();
            var postsQuery = PostService.GetQuery <PostOutputDto>(p => (p.Status == Status.Pended || CurrentUser.IsAdmin)); //准备文章的查询
            var posts      = postsQuery.Where(p => !p.IsFixedTop).OrderBy(OrderBy.ModifyDate.GetDisplay() + " desc").Take(15).Cacheable().ToList();
            var viewModel  = GetIndexPageViewModel();

            viewModel.Banner  = banners;
            viewModel.Posts   = Enumerable.AsEnumerable(postsQuery.Where(p => p.IsFixedTop).OrderByDescending(p => p.ModifyDate)).Union(posts).ToList();
            ViewBag.FastShare = fastShares;
            return(View(viewModel));
        }
Esempio n. 12
0
        public ActionResult CompareVersion(int id, int v1, int v2)
        {
            var main  = PostService.Get(p => p.Id == id && (p.Status == Status.Pended || CurrentUser.IsAdmin)).Mapper <PostHistoryVersion>() ?? throw new NotFoundException("文章未找到");
            var left  = v1 <= 0 ? main : PostHistoryVersionService.Get(v => v.Id == v1) ?? throw new NotFoundException("文章未找到");
            var right = v2 <= 0 ? main : PostHistoryVersionService.Get(v => v.Id == v2) ?? throw new NotFoundException("文章未找到");

            main.Id = id;
            var diff       = new HtmlDiff.HtmlDiff(right.Content, left.Content);
            var diffOutput = diff.Build();

            right.Content = Regex.Replace(Regex.Replace(diffOutput, "<ins.+?</ins>", string.Empty), @"<\w+></\w+>", string.Empty);
            left.Content  = Regex.Replace(Regex.Replace(diffOutput, "<del.+?</del>", string.Empty), @"<\w+></\w+>", string.Empty);
            ViewBag.Ads   = AdsService.GetsByWeightedPrice(2, AdvertiseType.InPage, main.CategoryId);
            return(View(new[] { main, left, right }));
        }
Esempio n. 13
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));
        }
Esempio n. 14
0
        public ActionResult Index()
        {
            var banners    = AdsService.GetsByWeightedPrice(8, AdvertiseType.Banner).OrderBy(a => Guid.NewGuid()).ToList();
            var fastShares = FastShareService.GetAllFromCache(s => s.Sort).ToList();
            var postsQuery = PostService.GetQuery <PostDto>(p => (p.Status == Status.Published || CurrentUser.IsAdmin)); //准备文章的查询
            var posts      = postsQuery.Where(p => !p.IsFixedTop).OrderBy(OrderBy.ModifyDate.GetDisplay() + " desc").ToCachedPagedList(1, 15);

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

            viewModel.Banner     = banners;
            viewModel.Posts      = posts;
            ViewBag.FastShare    = fastShares;
            viewModel.PageParams = new Pagination(1, 15, posts.TotalCount, OrderBy.ModifyDate);
            return(View(viewModel));
        }
Esempio 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);
            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));
        }
Esempio n. 16
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));
        }
Esempio n. 17
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));
        }
Esempio n. 18
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)
            });
        }
Esempio n. 19
0
        public async Task <ActionResult> CompareVersion(int id, int v1, int v2)
        {
            var post = await PostService.GetAsync(p => p.Id == id && (p.Status == Status.Published || CurrentUser.IsAdmin));

            var main = post.Mapper <PostHistoryVersion>() ?? throw new NotFoundException("文章未找到");

            CheckPermission(post);
            var left = v1 <= 0 ? main : await PostHistoryVersionService.GetAsync(v => v.Id == v1) ?? throw new NotFoundException("文章未找到");

            var right = v2 <= 0 ? main : await PostHistoryVersionService.GetAsync(v => v.Id == v2) ?? throw new NotFoundException("文章未找到");

            main.Id = id;
            var diff       = new HtmlDiff.HtmlDiff(right.Content, left.Content);
            var diffOutput = diff.Build();

            right.Content       = ReplaceVariables(Regex.Replace(Regex.Replace(diffOutput, "<ins.+?</ins>", string.Empty), @"<\w+></\w+>", string.Empty));
            right.ModifyDate    = right.ModifyDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone));
            left.Content        = ReplaceVariables(Regex.Replace(Regex.Replace(diffOutput, "<del.+?</del>", string.Empty), @"<\w+></\w+>", string.Empty));
            left.ModifyDate     = left.ModifyDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone));
            ViewBag.Ads         = AdsService.GetsByWeightedPrice(2, AdvertiseType.InPage, Request.Location(), main.CategoryId);
            ViewBag.DisableCopy = post.DisableCopy;
            return(View(new[] { main, left, right }));
        }
Esempio n. 20
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)
            });
        }
Esempio n. 21
0
 public ActionResult Donate()
 {
     ViewBag.Ads = AdsService.GetsByWeightedPrice(2, AdvertiseType.InPage);
     return(CurrentUser.IsAdmin ? View("Donate_Admin") : View());
 }