瀏覽代碼

RSS插广告

懒得勤快 4 年之前
父節點
當前提交
320a4e7ce6

+ 35 - 29
src/Masuit.MyBlogs.Core/Controllers/SubscribeController.cs

@@ -10,7 +10,6 @@ using Microsoft.Extensions.Caching.Memory;
 using System;
 using System.Collections.Generic;
 using System.Linq;
-using System.Net;
 using System.Text;
 using System.Threading.Tasks;
 using WilderMinds.RssSyndication;
@@ -29,39 +28,19 @@ namespace Masuit.MyBlogs.Core.Controllers
         public IPostService PostService { get; set; }
         public ICategoryService CategoryService { get; set; }
         public ICommentService CommentService { get; set; }
-
-        /// <summary>
-        /// 响应数据
-        /// </summary>
-        /// <param name="data">数据</param>
-        /// <param name="success">响应状态</param>
-        /// <param name="message">响应消息</param>
-        /// <param name="isLogin">登录状态</param>
-        /// <param name="code">http响应码</param>
-        /// <returns></returns>
-        public ActionResult ResultData(object data, bool success = true, string message = "", bool isLogin = true, HttpStatusCode code = HttpStatusCode.OK)
-        {
-            return Ok(new
-            {
-                IsLogin = isLogin,
-                Success = success,
-                Message = message,
-                Data = data,
-                code
-            });
-        }
+        public IAdvertisementService AdvertisementService { get; set; }
 
         /// <summary>
         /// RSS订阅
         /// </summary>
         /// <returns></returns>
         [Route("/rss"), ResponseCache(Duration = 3600)]
-        public async Task<IActionResult> Rss()
+        public IActionResult Rss()
         {
             var time = DateTime.Today.AddDays(-1);
             string scheme = Request.Scheme;
             var host = Request.Host;
-            var posts = await PostService.GetQueryNoTracking(p => p.Status == Status.Published && p.ModifyDate >= time, p => p.ModifyDate, false).Select(p => new Item()
+            var posts = PostService.GetQueryNoTracking(p => p.Status == Status.Published && p.ModifyDate >= time, p => p.ModifyDate, false).Select(p => new Item()
             {
                 Author = new Author
                 {
@@ -79,10 +58,11 @@ namespace Masuit.MyBlogs.Core.Controllers
                 Permalink = scheme + "://" + host + "/" + p.Id,
                 Guid = p.Id.ToString(),
                 FullHtmlContent = p.Content.GetSummary(300, 50)
-            }).FromCacheAsync(new MemoryCacheEntryOptions()
+            }).FromCache(new MemoryCacheEntryOptions()
             {
                 AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1)
-            });
+            }).ToList();
+            InsertAdvertisement(posts);
             var feed = new Feed()
             {
                 Title = CommonHelper.SystemSettings["Title"],
@@ -99,6 +79,31 @@ namespace Masuit.MyBlogs.Core.Controllers
             return Content(rss, "text/xml");
         }
 
+        private void InsertAdvertisement(List<Item> posts, int? cid = null)
+        {
+            if (posts.Count > 2)
+            {
+                var ad = AdvertisementService.GetByWeightedPrice((AdvertiseType)(DateTime.Now.Second % 4 + 1), cid);
+                if (ad is not null)
+                {
+                    posts.Insert(new Random().Next(1, posts.Count), new Item()
+                    {
+                        Author = new Author()
+                        {
+                            Name = ad.IndexId
+                        },
+                        Body = ad.Description,
+                        Title = ad.Title,
+                        FullHtmlContent = ad.Description,
+                        Guid = ad.IndexId,
+                        PublishDate = DateTime.UtcNow,
+                        Link = new Uri(Url.ActionLink("Redirect", "Advertisement", new { id = ad.Id })),
+                        Permalink = Url.ActionLink("Redirect", "Advertisement", new { id = ad.Id })
+                    });
+                }
+            }
+        }
+
         /// <summary>
         /// RSS分类订阅
         /// </summary>
@@ -110,7 +115,7 @@ namespace Masuit.MyBlogs.Core.Controllers
             string scheme = Request.Scheme;
             var host = Request.Host;
             var category = await CategoryService.GetByIdAsync(id) ?? throw new NotFoundException("分类未找到");
-            var posts = await PostService.GetQueryNoTracking(p => p.CategoryId == id && p.Status == Status.Published && p.ModifyDate >= time, p => p.ModifyDate, false).Select(p => new Item()
+            var posts = PostService.GetQueryNoTracking(p => p.CategoryId == id && p.Status == Status.Published && p.ModifyDate >= time, p => p.ModifyDate, false).Select(p => new Item()
             {
                 Author = new Author
                 {
@@ -128,10 +133,11 @@ namespace Masuit.MyBlogs.Core.Controllers
                 Permalink = scheme + "://" + host + "/" + p.Id,
                 Guid = p.Id.ToString(),
                 FullHtmlContent = p.Content.GetSummary(300, 50)
-            }).FromCacheAsync(new MemoryCacheEntryOptions()
+            }).FromCache(new MemoryCacheEntryOptions()
             {
                 AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1)
-            });
+            }).ToList();
+            InsertAdvertisement(posts, id);
             var feed = new Feed()
             {
                 Title = Request.Host + $":分类{category.Name}文章订阅",

+ 6 - 13
src/Masuit.MyBlogs.Core/Views/Shared/_ArticleListAdvertisement.cshtml

@@ -4,22 +4,15 @@
     <div class="ibox-content" id="@Stopwatch.GetTimestamp()">
         <a asp-controller="Advertisement" asp-action="Redirect" asp-route-id="@Model.Id" target="_blank" id="@Stopwatch.GetTimestamp()">
             <h4>
-                <i class="text-red size18">[荐]</i>
-                @Model.Title
+                <i class="text-red size18">[荐]</i>@Model.Title
             </h4>
         </a>
         <a asp-controller="Advertisement" asp-action="Redirect" asp-route-id="@Model.Id" target="_blank" id="@Stopwatch.GetTimestamp()">
             <div class="row padding-bot10">
-                @{
-                    string imgSrc = Model.ThumbImgUrl;
-                    if (!string.IsNullOrEmpty(imgSrc))
-                    {
-                        <div class="col-md-2 paddingright-clear">
-                            <img class="img-thumbnail img-responsive thumb" src="@imgSrc" alt="@Model.Title" title="@Model.Title">
-                        </div>
-                    }
-                }
-                <div class="col-md-@(string.IsNullOrEmpty(imgSrc) ? 12 : 10)">
+                <div class="col-md-2 paddingright-clear">
+                    <img class="img-thumbnail img-responsive thumb" src="@Model.ThumbImgUrl" alt="@Model.Title" title="@Model.Title">
+                </div>
+                <div class="col-md-10">
                     <p>
                         @Model.Description
                     </p>
@@ -27,4 +20,4 @@
             </div>
         </a>
     </div>
-</div>
+</div>