SentSimResource.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Http\Resources;
  3. use Illuminate\Http\Resources\Json\JsonResource;
  4. use App\Models\PaliSentence;
  5. use App\Models\PaliText;
  6. use App\Http\Api\UserApi;
  7. use App\Http\Api\ChannelApi;
  8. use App\Http\Controllers\CorpusController;
  9. use App\Http\Api\AuthApi;
  10. class SentSimResource extends JsonResource
  11. {
  12. /**
  13. * Transform the resource into an array.
  14. *
  15. * @param \Illuminate\Http\Request $request
  16. * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
  17. */
  18. public function toArray($request)
  19. {
  20. //获取实际句子信息
  21. $sent = PaliSentence::find($this->sent2);
  22. $channelId = ChannelApi::getSysChannel('_System_Pali_VRI_');
  23. $sentOrg = [
  24. "id"=>$sent->id,
  25. "book"=> $sent->book,
  26. "para"=> $sent->paragraph,
  27. "wordStart"=> $sent->word_begin,
  28. "wordEnd"=> $sent->word_end,
  29. "editor"=> UserApi::getByUuid(config("app.admin.root_uuid")),
  30. "channel"=> ChannelApi::getById($channelId),
  31. "content"=>$sent->text,
  32. "html"=> "<span>{$sent->text}</span>",
  33. "role"=>"member",
  34. "created_at"=> $sent->created_at,
  35. "updated_at"=> $sent->updated_at,
  36. ];
  37. $user = AuthApi::current($request);
  38. if($user){
  39. $userUid = $user['user_uid'];
  40. }else{
  41. $userUid = null;
  42. }
  43. $resCount = CorpusController::sentCanReadCount($sent->book,$sent->paragraph,$sent->word_begin,$sent->word_end,$userUid);
  44. $result = [
  45. "id" => "{$sent->book}-{$sent->paragraph}-{$sent->word_begin}-{$sent->word_end}",
  46. "book"=> $sent->book,
  47. "para"=> $sent->paragraph,
  48. "wordStart"=> $sent->word_begin,
  49. "wordEnd"=> $sent->word_end,
  50. "origin" => [$sentOrg],
  51. "translation" => [],
  52. "layout" => "column",
  53. "tranNum" => $resCount['tranNum'],
  54. "nissayaNum" => $resCount['nissayaNum'],
  55. "commNum" => $resCount['commNum'],
  56. "originNum" => $resCount['originNum'],
  57. "simNum" => $resCount['simNum'],
  58. ];
  59. $result['path'] = json_decode(PaliText::where('book',$sent->book)
  60. ->where('paragraph',$sent->paragraph)
  61. ->value('path'));
  62. return $result;
  63. }
  64. }