BlogController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. // app/Http/Controllers/BlogController.php
  3. namespace App\Http\Controllers;
  4. use App\Models\Post;
  5. use App\Models\Tag;
  6. use App\Models\ProgressChapter;
  7. use App\Http\Api\UserApi;
  8. use Illuminate\Support\Facades\Log;
  9. use App\Services\ProgressChapterService;
  10. class BlogController extends Controller
  11. {
  12. protected $categories = [
  13. ['id' => 'sutta', 'label' => 'suttapiṭaka'],
  14. ['id' => 'vinaya', 'label' => 'vinayapiṭaka'],
  15. ['id' => 'abhidhamma', 'label' => 'abhidhammapiṭaka'],
  16. ['id' => 'añña', 'label' => 'añña'],
  17. ['id' => 'mūla', 'label' => 'mūla'],
  18. ['id' => 'aṭṭhakathā', 'label' => 'aṭṭhakathā'],
  19. ['id' => 'ṭīkā', 'label' => 'ṭīkā'],
  20. ];
  21. // 首页 - 最新博文列表
  22. public function index($user)
  23. {
  24. $user = UserApi::getByName($user);
  25. $posts = ProgressChapter::with('channel')
  26. ->where('progress', '>', 0.9)
  27. ->whereHas('channel', function ($query) use ($user) {
  28. $query->where('status', 30)->where('owner_uid', $user['id']);
  29. })
  30. ->latest()
  31. ->paginate(10);
  32. $categories = $this->categories;
  33. /*
  34. $posts = Post::published()
  35. ->with(['category', 'tags'])
  36. ->latest()
  37. ->paginate(10);
  38. $categories = Category::withCount('posts')->get();
  39. $popularPosts = Post::published()
  40. ->orderBy('views_count', 'desc')
  41. ->take(5)
  42. ->get();
  43. */
  44. //return view('blog.index', compact('posts', 'categories', 'popularPosts'));
  45. return view('blog.index', compact('user', 'posts', 'categories'));
  46. }
  47. /*
  48. // 博文详情页
  49. public function show(Post $post)
  50. {
  51. if (!$post->is_published) {
  52. abort(404);
  53. }
  54. $post->incrementViews();
  55. $post->load(['category', 'tags']);
  56. // 相关文章
  57. $relatedPosts = Post::published()
  58. ->where('category_id', $post->category_id)
  59. ->where('id', '!=', $post->id)
  60. ->take(3)
  61. ->get();
  62. // 上一篇和下一篇
  63. $prevPost = Post::published()
  64. ->where('published_at', '<', $post->published_at)
  65. ->latest()
  66. ->first();
  67. $nextPost = Post::published()
  68. ->where('published_at', '>', $post->published_at)
  69. ->oldest()
  70. ->first();
  71. return view('blog.show', compact('post', 'relatedPosts', 'prevPost', 'nextPost'));
  72. }
  73. // 分类列表
  74. public function categories()
  75. {
  76. $categories = Category::withCount('posts')
  77. ->having('posts_count', '>', 0)
  78. ->orderBy('posts_count', 'desc')
  79. ->get();
  80. return view('blog.categories', compact('categories'));
  81. }
  82. */
  83. // 分类下的文章
  84. public function category(
  85. $user,
  86. $category1,
  87. $category2 = null,
  88. $category3 = null,
  89. $category4 = null,
  90. $category5 = null
  91. ) {
  92. $user = UserApi::getByName($user);
  93. $categories = $this->categories;
  94. $chapterService = new ProgressChapterService();
  95. $tags = $this->getCategories($category1, $category2, $category3, $category4, $category5);
  96. $posts = $chapterService->setProgress(0.9)->setChannelOwnerId($user['id'])
  97. ->setTags($tags)
  98. ->get();
  99. $count = count($posts);
  100. $current = array_map(function ($item) {
  101. return ['id' => $item, 'label' => $item];
  102. }, $tags);
  103. $tagOptions = $chapterService->setProgress(0.9)->setChannelOwnerId($user['id'])
  104. ->setTags($tags)
  105. ->getTags();
  106. return view('blog.category', compact('user', 'categories', 'posts', 'current', 'tagOptions', 'count'));
  107. }
  108. private function getCategories($category1, $category2, $category3, $category4, $category5)
  109. {
  110. $category = [];
  111. if ($category1) {
  112. $category[] = $category1;
  113. }
  114. if ($category2) {
  115. $category[] = $category2;
  116. }
  117. if ($category3) {
  118. $category[] = $category3;
  119. }
  120. if ($category4) {
  121. $category[] = $category4;
  122. }
  123. if ($category5) {
  124. $category[] = $category5;
  125. }
  126. return $category;
  127. }
  128. /*
  129. // 年度归档
  130. public function archives()
  131. {
  132. $archives = Post::published()
  133. ->selectRaw('YEAR(published_at) as year, COUNT(*) as count')
  134. ->groupBy('year')
  135. ->orderBy('year', 'desc')
  136. ->get();
  137. return view('blog.archives', compact('archives'));
  138. }
  139. // 指定年份的文章
  140. public function archivesByYear($year)
  141. {
  142. $posts = Post::published()
  143. ->whereYear('published_at', $year)
  144. ->with(['category', 'tags'])
  145. ->latest()
  146. ->paginate(15);
  147. // 按月分组
  148. $postsByMonth = $posts->getCollection()->groupBy(function ($post) {
  149. return $post->published_at->format('Y-m');
  150. });
  151. return view('blog.archives-year', compact('posts', 'postsByMonth', 'year'));
  152. }
  153. // 标签页面
  154. public function tag(Tag $tag)
  155. {
  156. $posts = $tag->posts()
  157. ->published()
  158. ->with(['category', 'tags'])
  159. ->latest()
  160. ->paginate(10);
  161. return view('blog.tag', compact('tag', 'posts'));
  162. }
  163. // 搜索
  164. public function search(Request $request)
  165. {
  166. $query = $request->get('q');
  167. if (empty($query)) {
  168. return redirect()->route('blog.index');
  169. }
  170. $posts = Post::published()
  171. ->where(function ($q) use ($query) {
  172. $q->where('title', 'LIKE', "%{$query}%")
  173. ->orWhere('content', 'LIKE', "%{$query}%")
  174. ->orWhere('excerpt', 'LIKE', "%{$query}%");
  175. })
  176. ->with(['category', 'tags'])
  177. ->latest()
  178. ->paginate(10);
  179. return view('blog.search', compact('posts', 'query'));
  180. }
  181. */
  182. }