2
0

ParagraphContentController.php 3.1 KB

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