ShareResource.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace App\Http\Resources;
  3. use App\Http\Api\GroupApi;
  4. use App\Http\Api\StudioApi;
  5. use App\Http\Api\UserApi;
  6. use App\Models\Article;
  7. use App\Models\Channel;
  8. use App\Models\Collection;
  9. use App\Models\Project;
  10. use Illuminate\Contracts\Support\Arrayable;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Http\Resources\Json\JsonResource;
  13. class ShareResource extends JsonResource
  14. {
  15. /**
  16. * Transform the resource into an array.
  17. *
  18. * @param Request $request
  19. * @return array|Arrayable|\JsonSerializable
  20. */
  21. public function toArray($request)
  22. {
  23. // 获取资源信息
  24. $res_name = '';
  25. $owner = '';
  26. switch ($this->res_type) {
  27. case 1:
  28. // PCS 文档
  29. break;
  30. case 2:
  31. // Channel 版本
  32. $res = Channel::where('uid', $this->res_id)->first();
  33. if ($res) {
  34. $res_name = $res->name;
  35. $owner = StudioApi::getById($res->owner_uid);
  36. }
  37. break;
  38. case 3:
  39. // Article 文章
  40. $res = Article::where('uid', $this->res_id)->first();
  41. if ($res) {
  42. $res_name = $res->title;
  43. $owner = StudioApi::getById($res->owner);
  44. }
  45. break;
  46. case 4:
  47. // Collection 文集
  48. $res = Collection::where('uid', $this->res_id)->first();
  49. if ($res) {
  50. $res_name = $res->title;
  51. $owner = StudioApi::getById($res->owner);
  52. }
  53. break;
  54. case 5:
  55. // 版本片段 不支持
  56. break;
  57. case 6: // workflow
  58. $res = Project::where('uid', $this->res_id)->first();
  59. if ($res) {
  60. $res_name = $res->title;
  61. $owner = StudioApi::getById($res->owner_id);
  62. }
  63. break;
  64. default:
  65. // code...
  66. break;
  67. }
  68. $data = [
  69. 'id' => $this->id,
  70. 'res_id' => $this->res_id,
  71. 'res_type' => $this->res_type,
  72. 'collaborator_type' => $this->cooperator_type,
  73. 'power' => $this->power,
  74. 'res_name' => $res_name,
  75. 'owner' => $owner,
  76. 'created_at' => $this->created_at,
  77. 'updated_at' => $this->updated_at,
  78. ];
  79. switch ($this->cooperator_type) {
  80. case 0:
  81. // user
  82. $data['user'] = UserApi::getByUuid($this->cooperator_id);
  83. break;
  84. case 1:
  85. // code...
  86. $data['group'] = GroupApi::getById($this->cooperator_id);
  87. break;
  88. }
  89. return $data;
  90. }
  91. }