TermResource.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace App\Http\Resources;
  3. use Illuminate\Http\Resources\Json\JsonResource;
  4. use Illuminate\Support\Str;
  5. use App\Http\Api\ChannelApi;
  6. use App\Http\Api\StudioApi;
  7. use App\Http\Api\UserApi;
  8. use App\Http\Api\MdRender;
  9. use App\Http\Api\ShareApi;
  10. use App\Http\Api\AuthApi;
  11. use App\Models\UserOperationDaily;
  12. class TermResource extends JsonResource
  13. {
  14. /**
  15. * Transform the resource into an array.
  16. *
  17. * @param \Illuminate\Http\Request $request
  18. * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
  19. */
  20. public function toArray($request)
  21. {
  22. $data = [
  23. "id"=>$this->id,
  24. "guid"=>$this->guid,
  25. "word"=> $this->word,
  26. "word_en"=> $this->word_en,
  27. "meaning"=> $this->meaning,
  28. "other_meaning"=> $this->other_meaning,
  29. "tag"=> $this->tag,
  30. "note"=> $this->note,
  31. "language"=> $this->language,
  32. "channal"=> $this->channal,
  33. "studio" => StudioApi::getById($this->owner),
  34. "editor"=> UserApi::getById($this->editor_id),
  35. "created_at"=> $this->created_at,
  36. "updated_at"=> $this->updated_at,
  37. ];
  38. if($request->has('channel') && !empty($request->get('channel'))){
  39. $channels = explode('_',$request->get('channel')) ;
  40. }else{
  41. if(!empty($this->channal) && Str::isUuid($this->channal)){
  42. $channelId = $this->channal;
  43. $data["channel"] = ChannelApi::getById($this->channal);
  44. }else{
  45. $channelId = ChannelApi::getSysChannel('_community_translation_'.$this->language.'_');
  46. if(empty($channelId)){
  47. $channelId = ChannelApi::getSysChannel('_community_translation_zh-hans_');
  48. }
  49. }
  50. if(!empty($channelId)){
  51. $channels = [$channelId];
  52. }else{
  53. $channels = [];
  54. }
  55. }
  56. if(!empty($this->note)){
  57. $mdRender = new MdRender(
  58. [
  59. 'mode'=>$request->get('mode','read'),
  60. 'format'=>'react',
  61. 'studioId'=>$this->owner,
  62. ]);
  63. $data["html"] = $mdRender->convert($this->note,$channels,null);
  64. }
  65. $user = AuthApi::current($request);
  66. if(!$user){
  67. $data["role"] = 'reader';
  68. }else{
  69. if($this->owner === $user['user_uid']){
  70. $data["role"] = 'owner';
  71. }else if(!empty($this->channal)){
  72. $power = ShareApi::getResPower($user['user_uid'],$this->channal);
  73. if($power===20){
  74. $data["role"] = 'editor';
  75. }else if($power===10){
  76. $data["role"] = 'reader';
  77. }else{
  78. $data["role"] = 'unknown';
  79. }
  80. }
  81. }
  82. if($request->has('exp')){
  83. //毫秒计算的经验值
  84. $exp = UserOperationDaily::where('user_id',$this->editor_id)
  85. ->where('date_int','<=',date_timestamp_get(date_create($this->updated_at))*1000)
  86. ->sum('duration');
  87. $data['exp'] = (int)($exp/1000);
  88. }
  89. return $data;
  90. }
  91. }