BlogRenderService.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace App\Web\Services;
  3. use App\Web\Models\BlogModel;
  4. use App\Web\Models\BlogPlateModel;
  5. use App\Web\Models\BlogTagModel;
  6. use App\Web\Models\BlogTagRelationModel;
  7. use App\Web\Models\BlogTypeModel;
  8. use App\Web\Models\BlogTypeRelationModel;
  9. class BlogRenderService
  10. {
  11. protected $blogService;
  12. protected $blogModel;
  13. protected $blogTypeModel;
  14. protected $blogTagModel;
  15. protected $blogTypeRelationModel;
  16. protected $blogTagRelationModel;
  17. protected $blogPlateModel;
  18. public function __construct(BlogModel $blogModel,
  19. BlogTypeModel $blogTypeModel,
  20. BlogTypeRelationModel $blogTypeRelationModel,
  21. BlogTagModel $blogTagModel,
  22. BlogTagRelationModel $blogTagRelationModel,
  23. BlogPlateModel $blogPlateModel,
  24. BlogService $blogService)
  25. {
  26. $this->blogPlateModel = $blogPlateModel;
  27. $this->blogTagRelationModel = $blogTagRelationModel;
  28. $this->blogTypeRelationModel = $blogTypeRelationModel;
  29. $this->blogTagModel = $blogTagModel;
  30. $this->blogTypeModel = $blogTypeModel;
  31. $this->blogModel = $blogModel;
  32. $this->blogService = $blogService;
  33. }
  34. /**
  35. * 获取已发布的文章
  36. * @param $plateId
  37. * @param $keyword
  38. * @param $page
  39. * @param $pageSize
  40. * @param array $sortField
  41. * @param int $typeId
  42. * @param int $tagId
  43. * @param $fields
  44. * @return array
  45. */
  46. public function getPublishBlogList($plateId, $keyword, $page, $pageSize, array $sortField = [], $typeId = 0, $tagId = 0, $fields = null,$echoContent=false): array
  47. {
  48. $params = [
  49. 'page' => $page,
  50. 'page_size' => $pageSize,
  51. 'type_id' => $typeId,
  52. 'tag_id' => $tagId,
  53. 'plate_id' => $plateId,
  54. 'keyword' => $keyword
  55. ];
  56. if (!empty($sortField)) {
  57. foreach ($sortField as $sf) {
  58. $params['sort'][$sf] = 1;
  59. }
  60. }
  61. $fields ?? $fields = 'a.id,a.plate_id,a.pub_date,a.create_time,a.title,a.author,a.description,a.image_url,a.image_alt,a.is_top,a.is_recommend,a.download_url,a.total_view,b.urla,c.type_id,d.tag_id,a.expand_content,a.main_image';
  62. if ($echoContent) {
  63. $fields .= ',a.content';
  64. }
  65. $blogList = $this->blogModel->getPublishBlogListByPage($params, $fields);
  66. if(!empty($blogList['data'])) {
  67. $blogIds = array_column($blogList['data'], 'id');
  68. if ($blogIds) {
  69. $newestTypeAndTagData = $this->blogService->getTypeAndTagDataByBlogId($blogIds);
  70. foreach ($blogList['data'] as &$newestItem) {
  71. if (!empty($newestTypeAndTagData[$newestItem['id']]['type_items'])) {
  72. $newestItem['type_items'] = $newestTypeAndTagData[$newestItem['id']]['type_items'];
  73. } else {
  74. $newestItem['type_items'] = [];
  75. }
  76. if (!empty($newestTypeAndTagData[$newestItem['id']]['tag_items'])) {
  77. $newestItem['tag_items'] = $newestTypeAndTagData[$newestItem['id']]['tag_items'];
  78. } else {
  79. $newestItem['tag_items'] = [];
  80. }
  81. $newestItem['expand_content'] = empty($newestItem['expand_content'])?[]:json_decode($newestItem['expand_content'],true);
  82. $newestItem['expand_content'] = mapByKey($newestItem['expand_content'],'key');
  83. }
  84. }
  85. }
  86. return $blogList;
  87. }
  88. /**
  89. * @param $blogId
  90. * @return array
  91. */
  92. public function getPublishedBlogInfo($blogId): array
  93. {
  94. $data = $this->blogService->getBlogRenderData([
  95. 'id' => $blogId,
  96. 'status' => 0
  97. ]);
  98. if(!empty($data['type_items'])) {
  99. $typeIds = array_column($data['type_items'], 'type_id');
  100. $typeCountArr = $this->blogTypeRelationModel->getBlogTypeCount($typeIds);
  101. if(!empty($typeCountArr)) {
  102. foreach ($data['type_items'] as &$dyi) {
  103. if(!empty($typeCountArr[$dyi['type_id']])) {
  104. $dyi['type_count'] = $typeCountArr[$dyi['type_id']];
  105. } else {
  106. $dyi['type_count'] = 0;
  107. }
  108. }
  109. }
  110. }
  111. if(!empty($data['tag_items'])) {
  112. $tagIds = array_column($data['tag_items'], 'tag_id');
  113. $tagCountArr = $this->blogTagRelationModel->getBlogTagCount($tagIds);
  114. if(!empty($tagCountArr)) {
  115. foreach ($data['tag_items'] as &$dgi) {
  116. if(!empty($tagCountArr[$dgi['tag_id']])) {
  117. $dgi['tag_count'] = $tagCountArr[$dgi['tag_id']];
  118. } else {
  119. $dgi['tag_count'] = 0;
  120. }
  121. }
  122. }
  123. }
  124. return $data;
  125. }
  126. /**
  127. * 获取分类
  128. * @param $plateId
  129. * @param int $newsId
  130. * @return mixed
  131. */
  132. public function getPublishedTypeList($plateId, int $newsId = 0, $limit = 4)
  133. {
  134. return $this->blogTypeModel->getLimitBlogType($plateId, $limit, $newsId);
  135. }
  136. /**
  137. * 获取标签
  138. * @param $plateId
  139. * @param int $newsId
  140. * @return mixed
  141. */
  142. public function getPublishedTagList($plateId, int $newsId = 0)
  143. {
  144. return $this->blogTagModel->getLimitBlogTag($plateId, 69, $newsId);
  145. }
  146. /**
  147. * @param $plateId
  148. * @return array
  149. */
  150. public function getPlateInfo($plateId): array
  151. {
  152. return $this->blogPlateModel->getBlogPlateInfo(['id' => $plateId]);
  153. }
  154. public function getPreBlog($plateId, $newsId)
  155. {
  156. return $this->blogModel->getPrevBlog($plateId, $newsId);
  157. }
  158. public function getNextBlog($plateId, $newsId)
  159. {
  160. return $this->blogModel->getNextBlog($plateId, $newsId);
  161. }
  162. public function getBlogTypeInfo($typeId)
  163. {
  164. $blog_type = $this->blogTypeModel->getBlogTypeById($typeId);
  165. if (!empty($blog_type['expand_content'])) {
  166. $blog_type['expand_content'] = json_decode($blog_type['expand_content'],true);
  167. $blog_type['expand_content'] = mapByKey($blog_type['expand_content'],'key');
  168. }
  169. return $blog_type;
  170. }
  171. public function getBlogTagInfo($tagId)
  172. {
  173. return $this->blogTagModel->getBlogTagById($tagId);
  174. }
  175. }