ParagraphContentController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Api\ChannelApi;
  4. use App\Models\PaliText;
  5. use App\Models\Sentence;
  6. use App\Services\PaliContentService;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Http\Response;
  9. use Illuminate\Support\Str;
  10. class ParagraphContentController extends Controller
  11. {
  12. /**
  13. * Display a listing of the resource.
  14. */
  15. public function index(Request $request, PaliContentService $paliService)
  16. {
  17. $data = $request->validate([
  18. 'book' => 'required|integer',
  19. 'para' => 'required|integer',
  20. 'to' => 'integer',
  21. ]);
  22. $channels = [];
  23. if ($request->has('channels')) {
  24. $_channels = explode(',', str_replace('_', ',', $request->input('channels')));
  25. foreach ($_channels as $key => $channel) {
  26. if (Str::isUuid($channel)) {
  27. $channels[] = $channel;
  28. }
  29. }
  30. }
  31. $mode = $request->input('mode', 'read');
  32. if ($mode === 'read') {
  33. // 阅读模式加载html格式原文
  34. $channelId = ChannelApi::getSysChannel('_System_Pali_VRI_');
  35. } else {
  36. // 翻译模式加载json格式原文
  37. $channelId = ChannelApi::getSysChannel('_System_Wbw_VRI_');
  38. }
  39. if ($channelId !== false) {
  40. $channels[] = $channelId;
  41. }
  42. $chapter = PaliText::where('book', $data['book'])
  43. ->where('paragraph', $data['para'])->first();
  44. if (! $chapter) {
  45. return $this->error('no data');
  46. }
  47. // 获取channel索引表
  48. $indexChannel = [];
  49. $indexChannel = $paliService->getChannelIndex($channels);
  50. $from = $data['para'];
  51. if (isset($data['to'])) {
  52. $to = $data['to'];
  53. } else {
  54. $to = $data['para'];
  55. }
  56. $record = Sentence::where('book_id', $data['book'])
  57. ->whereBetween('paragraph', [$from, $to])
  58. ->whereIn('channel_uid', $channels)
  59. ->orderBy('paragraph')
  60. ->orderBy('word_start')
  61. ->get();
  62. if (count($record) === 0) {
  63. return $this->error('no data');
  64. }
  65. $result = $paliService->makeContentObj($record, $mode, $indexChannel);
  66. return $this->ok([
  67. 'items' => $result,
  68. 'pagination' => [
  69. 'page' => 1,
  70. 'pageSize' => $to - $from + 1,
  71. 'total' => $to - $from + 1,
  72. ],
  73. ]);
  74. }
  75. /**
  76. * Store a newly created resource in storage.
  77. */
  78. public function store(Request $request)
  79. {
  80. //
  81. }
  82. /**
  83. * Display the specified resource.
  84. *
  85. * @return Response
  86. */
  87. public function show(Request $request, string $id) {}
  88. /**
  89. * Update the specified resource in storage.
  90. */
  91. public function update(Request $request, string $id)
  92. {
  93. //
  94. }
  95. /**
  96. * Remove the specified resource from storage.
  97. */
  98. public function destroy(string $id)
  99. {
  100. //
  101. }
  102. }