TermResource.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace App\Http\Resources;
  3. use Illuminate\Http\Resources\Json\JsonResource;
  4. use App\Http\Api\ChannelApi;
  5. use App\Http\Api\StudioApi;
  6. use App\Http\Api\UserApi;
  7. use App\Http\Api\MdRender;
  8. use App\Http\Api\ShareApi;
  9. use App\Http\Api\AuthApi;
  10. class TermResource 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. $data = [
  21. "id"=>$this->id,
  22. "guid"=>$this->guid,
  23. "word"=> $this->word,
  24. "word_en"=> $this->word_en,
  25. "meaning"=> $this->meaning,
  26. "other_meaning"=> $this->other_meaning,
  27. "tag"=> $this->tag,
  28. "note"=> $this->note,
  29. "language"=> $this->language,
  30. "channal"=> $this->channal,
  31. "studio" => StudioApi::getById($this->owner),
  32. "editor"=> UserApi::getById($this->editor_id),
  33. "created_at"=> $this->created_at,
  34. "updated_at"=> $this->updated_at,
  35. ];
  36. if(!empty($this->channal)){
  37. $channelId = $this->channal;
  38. $data["channel"] = ChannelApi::getById($this->channal);
  39. }else{
  40. $channelId = ChannelApi::getSysChannel('_community_translation_'.$this->language.'_');
  41. if(empty($channelId)){
  42. $channelId = ChannelApi::getSysChannel('_community_translation_zh-hans_');
  43. }
  44. }
  45. if(!empty($this->note) && !empty($channelId)){
  46. $data["html"] = MdRender::render($this->note,[$channelId],null,'read');
  47. }
  48. $user = AuthApi::current($request);
  49. if(!$user){
  50. $data["role"] = 'reader';
  51. }else{
  52. if($this->owner === $user['user_uid']){
  53. $data["role"] = 'owner';
  54. }else if(!empty($this->channal)){
  55. $power = ShareApi::getResPower($user['user_uid'],$this->channal);
  56. if($power===20){
  57. $data["role"] = 'editor';
  58. }else if($power===10){
  59. $data["role"] = 'reader';
  60. }else{
  61. $data["role"] = 'unknown';
  62. }
  63. }
  64. }
  65. return $data;
  66. }
  67. }