ShareResource.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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\Http\Api\StudioApi;
  8. use App\Http\Api\GroupApi;
  9. use App\Http\Api\UserApi;
  10. class ShareResource 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. $res_name="";
  22. $owner="";
  23. switch ($this->res_type) {
  24. case 1:
  25. # PCS 文档
  26. break;
  27. case 2:
  28. # Channel 版本
  29. $res = Channel::where('uid',$this->res_id)->first();
  30. if($res){
  31. $res_name = $res->name;
  32. $owner = StudioApi::getById($res->owner_uid);
  33. }
  34. break;
  35. case 3:
  36. # Article 文章
  37. $res = Article::where('uid',$this->res_id)->first();
  38. if($res){
  39. $res_name = $res->title;
  40. $owner = StudioApi::getById($res->owner);
  41. }
  42. break;
  43. case 4:
  44. # Collection 文集
  45. $res = Collection::where('uid',$this->res_id)->first();
  46. if($res){
  47. $res_name = $res->title;
  48. $owner = StudioApi::getById($res->owner);
  49. }
  50. break;
  51. case 5:
  52. # 版本片段 不支持
  53. break;
  54. default:
  55. # code...
  56. break;
  57. }
  58. $data = [
  59. "id"=>$this->id,
  60. "res_id"=> $this->res_id,
  61. "res_type"=> $this->res_type,
  62. "collaborator_type"=> $this->cooperator_type,
  63. "power"=> $this->power,
  64. 'res_name'=>$res_name,
  65. 'owner'=>$owner,
  66. "created_at"=> $this->created_at,
  67. "updated_at"=> $this->updated_at,
  68. ];
  69. switch ($this->cooperator_type) {
  70. case 0:
  71. # user
  72. $data['user'] = UserApi::getByUuid($this->cooperator_id);
  73. break;
  74. case 1:
  75. # code...
  76. $data['group'] = GroupApi::getById($this->cooperator_id);
  77. break;
  78. }
  79. return $data;
  80. }
  81. }