|
@@ -12,7 +12,6 @@ using Masuit.MyBlogs.Core.Models.ViewModel;
|
|
|
using Masuit.Tools;
|
|
|
using Masuit.Tools.Core.Net;
|
|
|
using Masuit.Tools.Linq;
|
|
|
-using Masuit.Tools.Models;
|
|
|
using Masuit.Tools.Systems;
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
@@ -55,19 +54,19 @@ namespace Masuit.MyBlogs.Core.Controllers
|
|
|
[HttpGet, ResponseCache(Duration = 600, VaryByHeader = "Cookie", Location = ResponseCacheLocation.Any)]
|
|
|
public async Task<ActionResult> Index([FromServices] IFastShareService fastShareService)
|
|
|
{
|
|
|
- var banners = AdsService.GetsByWeightedPrice(8, AdvertiseType.Banner).OrderBy(a => Guid.NewGuid()).ToList();
|
|
|
+ var banners = AdsService.GetsByWeightedPrice(8, AdvertiseType.Banner, Request.Location()).OrderBy(a => Guid.NewGuid()).ToList();
|
|
|
var fastShares = await fastShareService.GetAllFromCacheAsync(s => s.Sort);
|
|
|
var postsQuery = PostService.GetQuery(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).ToList());
|
|
|
- CheckPermission(posts);
|
|
|
+ CheckPermission(posts.Data);
|
|
|
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);
|
|
|
- viewModel.ListAdvertisement = AdsService.GetByWeightedPrice(AdvertiseType.PostList);
|
|
|
+ viewModel.SidebarAds = AdsService.GetsByWeightedPrice(2, AdvertiseType.SideBar, Request.Location());
|
|
|
+ viewModel.ListAdvertisement = AdsService.GetByWeightedPrice(AdvertiseType.PostList, Request.Location());
|
|
|
return View(viewModel);
|
|
|
}
|
|
|
|
|
@@ -89,11 +88,11 @@ namespace Masuit.MyBlogs.Core.Controllers
|
|
|
posts.Data.InsertRange(0, postsQuery.Where(p => p.IsFixedTop).OrderByDescending(p => p.ModifyDate).ProjectTo<PostDto>(MapperConfig).ToList());
|
|
|
}
|
|
|
|
|
|
- CheckPermission(posts);
|
|
|
+ CheckPermission(posts.Data);
|
|
|
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);
|
|
|
+ viewModel.SidebarAds = AdsService.GetsByWeightedPrice(2, AdvertiseType.SideBar, Request.Location());
|
|
|
+ viewModel.ListAdvertisement = AdsService.GetByWeightedPrice(AdvertiseType.PostList, Request.Location());
|
|
|
return View(viewModel);
|
|
|
}
|
|
|
|
|
@@ -109,13 +108,13 @@ namespace Masuit.MyBlogs.Core.Controllers
|
|
|
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(p => p.Label.Contains(id) && p.Status == Status.Published).OrderBy($"{nameof(PostDto.IsFixedTop)} desc,{(orderBy ?? OrderBy.ModifyDate).GetDisplay()} desc").ToCachedPagedListAsync<Post, PostDto>(page, size, MapperConfig);
|
|
|
- CheckPermission(posts);
|
|
|
+ CheckPermission(posts.Data);
|
|
|
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);
|
|
|
+ viewModel.SidebarAds = AdsService.GetsByWeightedPrice(2, AdvertiseType.SideBar, Request.Location());
|
|
|
+ viewModel.ListAdvertisement = AdsService.GetByWeightedPrice(AdvertiseType.PostList, Request.Location());
|
|
|
return View(viewModel);
|
|
|
}
|
|
|
|
|
@@ -133,14 +132,14 @@ namespace Masuit.MyBlogs.Core.Controllers
|
|
|
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 = await PostService.GetQuery(where).OrderBy($"{nameof(PostDto.IsFixedTop)} desc,{(orderBy ?? OrderBy.ModifyDate).GetDisplay()} desc").ToCachedPagedListAsync<Post, PostDto>(page, size, MapperConfig);
|
|
|
- CheckPermission(posts);
|
|
|
+ CheckPermission(posts.Data);
|
|
|
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);
|
|
|
+ viewModel.SidebarAds = AdsService.GetsByWeightedPrice(2, AdvertiseType.SideBar, Request.Location());
|
|
|
+ viewModel.ListAdvertisement = AdsService.GetByWeightedPrice(AdvertiseType.PostList, Request.Location());
|
|
|
return View(viewModel);
|
|
|
}
|
|
|
|
|
@@ -157,13 +156,13 @@ namespace Masuit.MyBlogs.Core.Controllers
|
|
|
{
|
|
|
var cat = await CategoryService.GetByIdAsync(id) ?? throw new NotFoundException("文章分类未找到");
|
|
|
var posts = await PostService.GetQuery(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);
|
|
|
- CheckPermission(posts);
|
|
|
+ CheckPermission(posts.Data);
|
|
|
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);
|
|
|
+ viewModel.SidebarAds = AdsService.GetsByWeightedPrice(2, AdvertiseType.SideBar, Request.Location(), id);
|
|
|
+ viewModel.ListAdvertisement = AdsService.GetByWeightedPrice(AdvertiseType.PostList, Request.Location(), id);
|
|
|
return View(viewModel);
|
|
|
}
|
|
|
|
|
@@ -178,36 +177,36 @@ namespace Masuit.MyBlogs.Core.Controllers
|
|
|
return Redirect(string.IsNullOrEmpty(referer) ? "/" : referer);
|
|
|
}
|
|
|
|
|
|
- private void CheckPermission(PagedList<PostDto> posts)
|
|
|
+ private void CheckPermission(List<PostDto> posts)
|
|
|
{
|
|
|
var location = Request.Location() + "|" + Request.Headers[HeaderNames.UserAgent];
|
|
|
- posts.Data.RemoveAll(p =>
|
|
|
+ posts.RemoveAll(p =>
|
|
|
{
|
|
|
switch (p.LimitMode)
|
|
|
{
|
|
|
- case PostLimitMode.AllowRegion:
|
|
|
+ case RegionLimitMode.AllowRegion:
|
|
|
return !location.Contains(p.Regions.Split(',', StringSplitOptions.RemoveEmptyEntries)) && !CurrentUser.IsAdmin && !VisitorTokenValid && !Request.IsRobot();
|
|
|
- case PostLimitMode.ForbidRegion:
|
|
|
+ case RegionLimitMode.ForbidRegion:
|
|
|
return location.Contains(p.Regions.Split(',', StringSplitOptions.RemoveEmptyEntries)) && !CurrentUser.IsAdmin && !VisitorTokenValid && !Request.IsRobot();
|
|
|
- case PostLimitMode.AllowRegionExceptForbidRegion:
|
|
|
+ case RegionLimitMode.AllowRegionExceptForbidRegion:
|
|
|
if (location.Contains(p.ExceptRegions.Split(',', StringSplitOptions.RemoveEmptyEntries)) && !CurrentUser.IsAdmin && !VisitorTokenValid)
|
|
|
{
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
- goto case PostLimitMode.AllowRegion;
|
|
|
- case PostLimitMode.ForbidRegionExceptAllowRegion:
|
|
|
+ goto case RegionLimitMode.AllowRegion;
|
|
|
+ case RegionLimitMode.ForbidRegionExceptAllowRegion:
|
|
|
if (location.Contains(p.ExceptRegions.Split(',', StringSplitOptions.RemoveEmptyEntries)) && !CurrentUser.IsAdmin && !VisitorTokenValid)
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- goto case PostLimitMode.ForbidRegion;
|
|
|
+ goto case RegionLimitMode.ForbidRegion;
|
|
|
default:
|
|
|
return false;
|
|
|
}
|
|
|
});
|
|
|
- foreach (var item in posts.Data)
|
|
|
+ foreach (var item in posts)
|
|
|
{
|
|
|
item.PostDate = item.PostDate.ToTimeZone(HttpContext.Session.Get<string>(SessionKey.TimeZone));
|
|
|
item.ModifyDate = item.ModifyDate.ToTimeZone(HttpContext.Session.Get<string>(SessionKey.TimeZone));
|
|
@@ -229,7 +228,8 @@ namespace Masuit.MyBlogs.Core.Controllers
|
|
|
1 => nameof(OrderBy.VoteUpCount),
|
|
|
2 => nameof(OrderBy.AverageViewCount),
|
|
|
_ => nameof(OrderBy.TotalViewCount)
|
|
|
- } + " desc").Skip(0).Take(5).Cacheable(); //热门文章
|
|
|
+ } + " desc").Skip(0).Take(5).Cacheable().ToList(); //热门文章
|
|
|
+ CheckPermission(hot6Post);
|
|
|
var newdic = new Dictionary<string, int>(); //标签云最终结果
|
|
|
var tagdic = postsQuery.Where(p => !string.IsNullOrEmpty(p.Label)).Select(p => p.Label).Distinct().Cacheable().ToList().SelectMany(s => s.Split(',', ',')).GroupBy(s => s).ToDictionary(g => g.Key, g => g.Count()); //统计标签
|
|
|
|
|
@@ -249,7 +249,7 @@ namespace Masuit.MyBlogs.Core.Controllers
|
|
|
HotSearch = hotSearches,
|
|
|
Notices = notices.Data,
|
|
|
Tags = newdic,
|
|
|
- Top6Post = hot6Post.ToList(),
|
|
|
+ Top6Post = hot6Post,
|
|
|
PostsQueryable = postsQuery
|
|
|
};
|
|
|
}
|