ArticleResource.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Http\Resources;
  3. use Illuminate\Http\Resources\Json\JsonResource;
  4. use App\Http\Api\MdRender;
  5. use App\Models\CourseMember;
  6. use App\Models\Course;
  7. use App\Models\Collection;
  8. use App\Models\ArticleCollection;
  9. use App\Models\Channel;
  10. use Illuminate\Support\Facades\Log;
  11. use App\Http\Api\UserApi;
  12. use App\Http\Api\StudioApi;
  13. use App\Http\Api\AuthApi;
  14. use App\Http\Controllers\ArticleController;
  15. use App\Http\Api\ChannelApi;
  16. class ArticleResource extends JsonResource
  17. {
  18. /**
  19. * Transform the resource into an array.
  20. *
  21. * @param \Illuminate\Http\Request $request
  22. * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
  23. */
  24. public function toArray($request)
  25. {
  26. $data = [
  27. "uid" => $this->uid,
  28. "title" => $this->title,
  29. "subtitle" => $this->subtitle,
  30. "summary" => $this->summary,
  31. "studio"=> StudioApi::getById($this->owner),
  32. "editor"=> UserApi::getById($this->editor_id),
  33. "status" => $this->status,
  34. "lang" => $this->lang,
  35. "created_at" => $this->created_at,
  36. "updated_at" => $this->updated_at,
  37. ];
  38. $user = AuthApi::current($request);
  39. if($user){
  40. $canEdit = ArticleController::userCanEdit($user['user_uid'],$this);
  41. if($canEdit){
  42. $data['role'] = 'editor';
  43. }
  44. }
  45. //查询文集
  46. $collectionCount = ArticleCollection::where('article_id',$this->uid)->count();
  47. if($collectionCount>0){
  48. $data['anthology_count'] = $collectionCount;
  49. $collection = ArticleCollection::where('article_id',$this->uid)->first();
  50. $data['anthology_first'] = Collection::find($collection->collect_id);
  51. }
  52. if(isset($this->content) && !empty($this->content)){
  53. if($request->has('channel')){
  54. $channel = $request->get('channel');
  55. }else{
  56. //查找用户默认channel
  57. $studioChannel = Channel::where('owner_uid',$this->owner)
  58. ->where('type','translation')
  59. ->get();
  60. if($studioChannel){
  61. $channel = $studioChannel[0]->uid;
  62. }else{
  63. $channel = ChannelApi::getSysChannel('_community_translation_'.strtolower($this->lang).'_',
  64. '_community_translation_en_');
  65. }
  66. }
  67. $data["content"] = $this->content;
  68. $data["content_type"] = $this->content_type;
  69. $query_id = null;
  70. if($request->has('course')){
  71. if($request->has('exercise')){
  72. $query_id = $request->get('exercise');
  73. if($request->has('user')){
  74. /**
  75. * 显示指定用户作业
  76. * 查询用户在课程中的channel
  77. */
  78. $userId = UserApi::getIdByName($request->get('user'));
  79. $userInCourse = CourseMember::where('course_id',$request->get('course'))
  80. ->where('user_id',$userId)
  81. ->first();
  82. if($userInCourse){
  83. $channel = $userInCourse->channel_id;
  84. }
  85. }else if($request->get('view')==="answer"){
  86. /**
  87. * 显示答案
  88. * 算法:查询course 答案 channel
  89. */
  90. $channel = Course::where('id',$request->get('course'))->value('channel_id');
  91. }else{
  92. //显示答案
  93. $channel = Course::where('id',$request->get('course'))->value('channel_id');
  94. }
  95. }else{
  96. $channel = Course::where('id',$request->get('course'))->value('channel_id');
  97. }
  98. }
  99. if($request->has('mode')){
  100. $mode = $request->get('mode');
  101. }else{
  102. $mode = 'read';
  103. }
  104. $data["html"] = MdRender::render($this->content,[$channel],$query_id,$mode);
  105. }
  106. return $data;
  107. }
  108. }