SearchResource.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Http\Resources;
  3. use Illuminate\Http\Resources\Json\JsonResource;
  4. use App\Models\PaliText;
  5. use App\Models\Sentence;
  6. use App\Http\Api\ChannelApi;
  7. use Illuminate\Support\Facades\Cache;
  8. use App\Tools\RedisClusters;
  9. class SearchResource extends JsonResource
  10. {
  11. /**
  12. * Transform the resource into an array.
  13. *
  14. * @param \Illuminate\Http\Request $request
  15. * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
  16. */
  17. public function toArray($request)
  18. {
  19. $data = [
  20. "book"=>$this->book,
  21. "paragraph"=> $this->paragraph,
  22. ];
  23. if(isset($this->rank)){
  24. $data["rank"] = $this->rank;
  25. }
  26. $paliText = PaliText::where('book',$this->book)
  27. ->where('paragraph',$this->paragraph)
  28. ->first();
  29. if(isset($this->highlight)){
  30. $data["highlight"] = $this->highlight;
  31. }else if(isset($this->content)){
  32. $data["content"] = $this->content;
  33. }else{
  34. $channelId = RedisClusters::remember('_System_Pali_VRI_',config('mint.cache.expire'),function(){
  35. return ChannelApi::getSysChannel('_System_Pali_VRI_');
  36. });
  37. $paraContent = Sentence::where('book_id',$this->book)
  38. ->where('paragraph',$this->paragraph)
  39. ->where('channel_uid',$channelId)
  40. ->orderBy('word_start')
  41. ->select('content')
  42. ->get();
  43. $html = '';
  44. foreach ($paraContent as $key => $value) {
  45. $html .= $value->content;
  46. }
  47. $data["content"] = $html;
  48. }
  49. if($paliText){
  50. $data['path'] = json_decode($paliText->path);
  51. if($paliText->level<100){
  52. $data["paliTitle"] = $paliText->toc;
  53. }else{
  54. $data["paliTitle"] = PaliText::where('book',$this->book)
  55. ->where('paragraph',$paliText->parent)
  56. ->value('toc');
  57. }
  58. }
  59. return $data;
  60. }
  61. }