ParagraphContentController.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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()
  15. {
  16. //
  17. }
  18. /**
  19. * Store a newly created resource in storage.
  20. */
  21. public function store(Request $request)
  22. {
  23. //
  24. }
  25. /**
  26. * Display the specified resource.
  27. * * @param \Illuminate\Http\Request $request
  28. * @param string $id
  29. * @return \Illuminate\Http\Response
  30. */
  31. public function show(Request $request, string $id)
  32. {
  33. $paliService = app(PaliContentService::class);
  34. //
  35. $sentId = \explode('-', $id);
  36. $channels = [];
  37. if ($request->has('channels')) {
  38. if (strpos($request->get('channels'), ',') === FALSE) {
  39. $_channels = explode('_', $request->get('channels'));
  40. } else {
  41. $_channels = explode(',', $request->get('channels'));
  42. }
  43. foreach ($_channels as $key => $channel) {
  44. if (Str::isUuid($channel)) {
  45. $channels[] = $channel;
  46. }
  47. }
  48. }
  49. $mode = $request->get('mode', 'read');
  50. if ($mode === 'read') {
  51. //阅读模式加载html格式原文
  52. $channelId = ChannelApi::getSysChannel('_System_Pali_VRI_');
  53. } else {
  54. //翻译模式加载json格式原文
  55. $channelId = ChannelApi::getSysChannel('_System_Wbw_VRI_');
  56. }
  57. if ($channelId !== false) {
  58. $channels[] = $channelId;
  59. }
  60. $chapter = PaliText::where('book', $sentId[0])->where('paragraph', $sentId[1])->first();
  61. if (!$chapter) {
  62. return $this->error("no data");
  63. }
  64. #获取channel索引表
  65. $indexChannel = [];
  66. $indexChannel = $paliService->getChannelIndex($channels);
  67. $from = $sentId[1];
  68. $to = $sentId[2] ?? $sentId[1];
  69. $record = Sentence::where('book_id', $sentId[0])
  70. ->whereBetween('paragraph', [$from, $to])
  71. ->whereIn('channel_uid', $channels)
  72. ->orderBy('paragraph')
  73. ->orderBy('word_start')
  74. ->get();
  75. if (count($record) === 0) {
  76. return $this->error("no data");
  77. }
  78. $result = [];
  79. $result['content'] = json_encode($paliService->makeContentObj($record, $mode, $indexChannel), JSON_UNESCAPED_UNICODE);
  80. $result['content_type'] = 'json';
  81. return $this->ok($result);
  82. }
  83. /**
  84. * Update the specified resource in storage.
  85. */
  86. public function update(Request $request, string $id)
  87. {
  88. //
  89. }
  90. /**
  91. * Remove the specified resource from storage.
  92. */
  93. public function destroy(string $id)
  94. {
  95. //
  96. }
  97. }