李昊 4 年之前
父节点
当前提交
0f884074e3

+ 6 - 0
gx_api/GxPress/Model/GxPress.Result/Media/BookMediaResult.cs

@@ -160,5 +160,11 @@ namespace GxPress.Result.Media
         /// </summary>
         /// <value></value>
         public int ParentId { get; set; }
+
+        /// <summary>
+        /// 是否可阅读
+        /// </summary>
+        /// <value></value>
+        public bool IsRead { get; set; }
     }
 }

+ 22 - 2
gx_api/GxPress/Service/GxPress.Service.Implement/Epub/EpubService.cs

@@ -3,12 +3,20 @@ using GxPress.Request.Media;
 using GxPress.Result.Category;
 using GxPress.Result.Media;
 using GxPress.Service.Interface.Epub;
+using GxPress.Repository.Interface.Media;
 using VersOne.Epub;
+using System;
+using System.Threading.Tasks;
 
 namespace GxPress.Service.Implement.Epub
 {
     public class EpubService : IEpubService
     {
+        private readonly IMediaRepository mediaRepository;
+        public EpubService(IMediaRepository mediaRepository)
+        {
+            this.mediaRepository = mediaRepository;
+        }
         /// <summary>
         /// 获取书籍目录
         /// </summary>
@@ -36,7 +44,6 @@ namespace GxPress.Service.Implement.Epub
                     bookCatalog.CatalogId = item.HtmlContentFile.FileName;
                     bookCatalog.ParentId = i;
                     result.Add(bookCatalog);
-
                 }
                 i++;
             }
@@ -46,20 +53,33 @@ namespace GxPress.Service.Implement.Epub
         /// 获取章节内容
         /// </summary>
         /// <returns></returns>
-        public string GetBookCatalogContent(BookCatalogRequest request)
+        public async Task<string> GetBookCatalogContent(BookCatalogRequest request)
         {
             if (string.IsNullOrEmpty(request.Path))
                 return string.Empty;
+            //获取media
+            var media = await mediaRepository.GetAsync(request.MediaId);
+            var sectionValue = 0;
+            if (media.FreeProportion > 0)
+            {
+                var freeProportion = media.FreeProportion / 100;
+                var catalogs = GetCatalog(request.Path);
+                sectionValue = Convert.ToInt32(catalogs.Count * freeProportion);
+            }
             EpubBook epubBook = EpubReader.ReadBook(request.Path);
             string htmlContent = string.Empty;
+            int i = 0;
             foreach (EpubTextContentFile textContentFile in epubBook.ReadingOrder)
             {
+                if (i >= sectionValue && sectionValue > 0)
+                    throw new Common.Exceptions.BusinessException("");
                 if (textContentFile.FileName.Equals(request.CatalogId) || textContentFile.FileName.Equals(request.CatalogNameg))
                 {
                     //当前文本内容文件的HTML
                     htmlContent = textContentFile.Content;
                     break;
                 }
+                i++;
             }
             return htmlContent;
         }

+ 20 - 1
gx_api/GxPress/Service/GxPress.Service.Implement/Media/MediaService.cs

@@ -8,6 +8,7 @@ using System.Linq;
 using GxPress.Result.Category;
 using GxPress.Service.Interface.Epub;
 using GxPress.Request.Media;
+using System;
 
 namespace GxPress.Service.Implement.Media
 {
@@ -76,7 +77,25 @@ namespace GxPress.Service.Implement.Media
             if (!mediaLibrary.FileUrl.Contains("epub"))
                 throw new Common.Exceptions.BusinessException("书籍不存在epub文件");
             //书籍
+            //获取media
+            var media = await mediaRepository.GetAsync(id);
+            var sectionValue = 0;
             result.BookCatalogResults = epubService.GetCatalog("wwwroot" + mediaLibrary.FileUrl);
+            if (media.FreeProportion > 0)
+            {
+                var freeProportion = media.FreeProportion / 100;
+                sectionValue = Convert.ToInt32(result.BookCatalogResults.Count * freeProportion);
+                foreach (var item in result.BookCatalogResults)
+                {
+                    if (item.Id <= sectionValue)
+                        item.IsRead = true;
+                }
+            }
+            else
+            {
+                foreach (var item in result.BookCatalogResults)
+                    item.IsRead = true;
+            }
             //计算免费占比
             return result;
         }
@@ -115,7 +134,7 @@ namespace GxPress.Service.Implement.Media
             if (!mediaLibrary.FileUrl.Contains("epub"))
                 throw new Common.Exceptions.BusinessException("书籍不存在epub文件");
             request.Path = "wwwroot" + mediaLibrary.FileUrl;
-            return epubService.GetBookCatalogContent(request);
+            return await epubService.GetBookCatalogContent(request);
         }
 
 

+ 2 - 1
gx_api/GxPress/Service/GxPress.Service.Interface/Epub/IEpubService.cs

@@ -1,4 +1,5 @@
 using System.Collections.Generic;
+using System.Threading.Tasks;
 using GxPress.Request.Media;
 using GxPress.Result.Media;
 
@@ -15,6 +16,6 @@ namespace GxPress.Service.Interface.Epub
         /// 获取章节内容
         /// </summary>
         /// <returns></returns>
-        string GetBookCatalogContent(BookCatalogRequest request);
+         Task<string> GetBookCatalogContent(BookCatalogRequest request);
     }
 }