ShareResource.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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\Http\Api\StudioApi;
  7. class ShareResource extends JsonResource
  8. {
  9. /**
  10. * Transform the resource into an array.
  11. *
  12. * @param \Illuminate\Http\Request $request
  13. * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
  14. */
  15. public function toArray($request)
  16. {
  17. //获取资源信息
  18. $res_name="";
  19. $owner="";
  20. switch ($this->res_type) {
  21. case 1:
  22. # PCS 文档
  23. break;
  24. case 2:
  25. # Channel 版本
  26. $res = Channel::where('uid',$this->res_id)->first();
  27. if($res){
  28. $res_name = $channel->name;
  29. $owner = StudioApi::getById($res->owner_uid);
  30. }
  31. break;
  32. case 3:
  33. # Article 文章
  34. $res = Article::where('uid',$this->res_id)->first();
  35. if($res){
  36. $res_name = $res->title;
  37. $owner = StudioApi::getById($res->owner);
  38. }
  39. break;
  40. case 4:
  41. # Collection 文集
  42. $res = Collection::where('uid',$this->res_id)->first();
  43. if($res){
  44. $res_name = $res->title;
  45. $owner = StudioApi::getById($res->owner);
  46. }
  47. break;
  48. case 5:
  49. # 版本片段 不支持
  50. break;
  51. default:
  52. # code...
  53. break;
  54. }
  55. return [
  56. "id"=>$this->id,
  57. "res_id"=> $this->res_id,
  58. "res_type"=> $this->res_type,
  59. "power"=> $this->power,
  60. 'res_name'=>$res_name,
  61. 'owner'=>$owner,
  62. "created_at"=> $this->created_at,
  63. "updated_at"=> $this->updated_at,
  64. ];
  65. }
  66. }