WikiController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Helpers\WikiContentParser;
  5. use App\Services\TermService;
  6. class WikiController extends Controller
  7. {
  8. public function __construct(
  9. private TermService $termService
  10. ) {}
  11. // ── Mock 数据 ────────────────────────────────────────────────
  12. private function mockEntries(): array
  13. {
  14. return [
  15. [
  16. 'word' => 'Anicca',
  17. 'lang' => 'zh-Hans',
  18. 'slug' => 'anicca',
  19. 'meaning' => '无常',
  20. 'quality' => 'featured', // featured | stub | review | null
  21. 'category' => '法义术语',
  22. 'tags' => ['三相', '法义术语', '相应部', '内观', '五蕴'],
  23. 'langs' => [
  24. ['lang' => 'zh-Hant', 'label' => '繁体中文', 'word' => '无常'],
  25. ['lang' => 'en', 'label' => 'English', 'word' => 'Impermanence'],
  26. ],
  27. 'related' => [
  28. ['word' => 'Dukkha', 'zh' => '苦', 'lang' => 'pi'],
  29. ['word' => 'Anattā', 'zh' => '无我', 'lang' => 'pi'],
  30. ['word' => 'Vipassanā', 'zh' => '内观', 'lang' => 'pi'],
  31. ['word' => 'Ti-lakkhaṇa', 'zh' => '三相', 'lang' => 'pi'],
  32. ],
  33. 'content' => <<<HTML
  34. <h2>词源与释义</h2>
  35. <blockquote>
  36. 「诸比丘,色是无常的。无常的即是苦的。苦的即是无我的。无我的即应如实以正慧观察……」
  37. <cite>SN 22.15 · Khandha-saṃyutta · 相应部·蕴相应</cite>
  38. </blockquote>
  39. HTML,
  40. ],
  41. ];
  42. }
  43. private function featured(array $all): array
  44. {
  45. return array_map(function ($item) {
  46. return ['word' => $item['word'], 'zh' => $item['meaning'], 'lang' => $item['language'], 'category' => '法义术语'];
  47. }, $all);
  48. }
  49. private function mockStats(): array
  50. {
  51. return [
  52. 'total' => 2847,
  53. 'this_month' => 43,
  54. 'contributors' => 128,
  55. ];
  56. }
  57. private function mockRecentUpdates(): array
  58. {
  59. return [
  60. ['word' => 'Nibbāna', 'lang' => 'pi'],
  61. ['word' => '四圣谛', 'lang' => 'zh'],
  62. ['word' => '阿含经', 'lang' => 'zh'],
  63. ['word' => 'Rājagaha', 'lang' => 'pi'],
  64. ];
  65. }
  66. // ── Actions ──────────────────────────────────────────────────
  67. public function index(string $lang)
  68. {
  69. $result = $this->termService->communityTerms($lang);
  70. $fakeRequest = Request::create('', 'GET', []);
  71. $termResource = $result['data'];
  72. $terms = $termResource->toArray($fakeRequest);
  73. $first = $terms[0];
  74. $today = [
  75. 'word' => $first['word'],
  76. 'lang' => $first['language'],
  77. 'slug' => $first['word'],
  78. 'meaning' => $first['meaning'],
  79. 'quality' => 'featured', // featured | stub | review | null
  80. 'category' => '法义术语',
  81. 'content' => $first['summary']
  82. ];
  83. return view('wiki.index', [
  84. 'today' => $today,
  85. 'featured' => $this->featured($terms),
  86. 'stats' => $this->mockStats(),
  87. 'recentUpdates' => $this->mockRecentUpdates(),
  88. 'categories' => $this->categories(),
  89. 'lang' => $lang
  90. ]);
  91. }
  92. public function show(string $lang, string $word)
  93. {
  94. $term = $this->termService->communityTerm($word, $lang);
  95. $urlParam = ['format' => 'html'];
  96. $fakeRequest = Request::create('', 'GET', $urlParam);
  97. $termArray = $term->toArray($fakeRequest);
  98. $entry = [
  99. 'word' => $termArray['word'],
  100. 'lang' => $termArray['language'],
  101. 'slug' => $termArray['word'],
  102. 'meaning' => $termArray['meaning'],
  103. 'quality' => 'featured', // featured | stub | review | null
  104. 'category' => '法义术语',
  105. 'tags' => [],
  106. 'langs' => [
  107. ['lang' => 'zh-Hant', 'label' => '繁体中文', 'word' => '无常'],
  108. ['lang' => 'en', 'label' => 'English', 'word' => 'Impermanence'],
  109. ],
  110. 'related' => [
  111. ['word' => 'Dukkha', 'zh' => '苦', 'lang' => 'pi'],
  112. ['word' => 'Anattā', 'zh' => '无我', 'lang' => 'pi'],
  113. ['word' => 'Vipassanā', 'zh' => '内观', 'lang' => 'pi'],
  114. ['word' => 'Ti-lakkhaṇa', 'zh' => '三相', 'lang' => 'pi'],
  115. ],
  116. 'content' => $termArray['html']
  117. ];
  118. $parsed = WikiContentParser::parse($entry['content']);
  119. return view('wiki.show', [
  120. 'entry' => array_merge($entry, [
  121. 'content' => $parsed['content'],
  122. 'toc' => $parsed['toc'],
  123. ]),
  124. 'categories' => $this->categories(),
  125. 'lang' => $lang
  126. ]);
  127. }
  128. // ── Helpers ──────────────────────────────────────────────────
  129. private function categories(): array
  130. {
  131. return [
  132. ['slug' => 'all', 'label' => '全部'],
  133. ['slug' => 'term', 'label' => '法义术语'],
  134. ['slug' => 'person', 'label' => '人物传记'],
  135. ['slug' => 'text', 'label' => '经典文献'],
  136. ['slug' => 'school', 'label' => '宗派历史'],
  137. ['slug' => 'practice', 'label' => '修行方法'],
  138. ['slug' => 'place', 'label' => '佛教地理'],
  139. ];
  140. }
  141. // 在 WikiController.php 中添加此方法
  142. // 在 WikiController.php 中添加
  143. public function home(string $lang = 'zh-Hans')
  144. {
  145. // Mock 语言列表
  146. $languages = [
  147. ['code' => 'zh-Hans', 'name' => '简体中文'],
  148. ['code' => 'zh-Hant', 'name' => '繁體中文'],
  149. ['code' => 'en', 'name' => 'English'],
  150. ['code' => 'ja', 'name' => '日本語'],
  151. ['code' => 'ko', 'name' => '한국어'],
  152. ['code' => 'vi', 'name' => 'Tiếng Việt'],
  153. ['code' => 'my', 'name' => 'မြန်မာစာ'],
  154. ];
  155. // Mock 统计数据
  156. $stats = [
  157. 'total_articles' => 2847,
  158. 'total_terms' => 1256,
  159. 'languages_count' => 10,
  160. 'contributors' => 328,
  161. 'today_updates' => 12,
  162. ];
  163. // Mock 热门搜索标签
  164. $hotTags = ['无常', 'Anicca', '四圣谛', '涅槃', '内观', '业力', '慈悲', '般若'];
  165. // Mock 每日一词(可选展示)
  166. $dailyTerm = [
  167. 'word' => 'Dhammapada',
  168. 'zh' => '法句经',
  169. 'lang' => 'pi',
  170. 'meaning' => '佛陀的偈颂集,佛教最重要的经典之一',
  171. ];
  172. return view('wiki.home', [
  173. 'languages' => $languages,
  174. 'currentLang' => $lang,
  175. 'stats' => $stats,
  176. 'hotTags' => $hotTags,
  177. 'dailyTerm' => $dailyTerm,
  178. ]);
  179. }
  180. }