ShareResource.php 2.8 KB

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