TermResource.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace App\Http\Resources;
  3. use Illuminate\Http\Resources\Json\JsonResource;
  4. use App\Http\Api\ChannelApi;
  5. use App\Http\Api\StudioApi;
  6. use App\Http\Api\UserApi;
  7. use App\Http\Api\MdRender;
  8. use App\Http\Api\ShareApi;
  9. use App\Http\Api\AuthApi;
  10. use App\Models\UserOperationDaily;
  11. class TermResource 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. $data = [
  22. "id"=>$this->id,
  23. "guid"=>$this->guid,
  24. "word"=> $this->word,
  25. "word_en"=> $this->word_en,
  26. "meaning"=> $this->meaning,
  27. "other_meaning"=> $this->other_meaning,
  28. "tag"=> $this->tag,
  29. "note"=> $this->note,
  30. "language"=> $this->language,
  31. "channal"=> $this->channal,
  32. "studio" => StudioApi::getById($this->owner),
  33. "editor"=> UserApi::getById($this->editor_id),
  34. "created_at"=> $this->created_at,
  35. "updated_at"=> $this->updated_at,
  36. ];
  37. if($request->has('channel')){
  38. $channels = explode('_',$request->get('channel')) ;
  39. }else{
  40. if(!empty($this->channal)){
  41. $channelId = $this->channal;
  42. $data["channel"] = ChannelApi::getById($this->channal);
  43. }else{
  44. $channelId = ChannelApi::getSysChannel('_community_translation_'.$this->language.'_');
  45. if(empty($channelId)){
  46. $channelId = ChannelApi::getSysChannel('_community_translation_zh-hans_');
  47. }
  48. }
  49. if(!empty($channelId)){
  50. $channels = [$channelId];
  51. }else{
  52. $channels = [];
  53. }
  54. }
  55. if(!empty($this->note)){
  56. $data["html"] = MdRender::render($this->note,$channels,null,$request->get('mode','read'));
  57. }
  58. $user = AuthApi::current($request);
  59. if(!$user){
  60. $data["role"] = 'reader';
  61. }else{
  62. if($this->owner === $user['user_uid']){
  63. $data["role"] = 'owner';
  64. }else if(!empty($this->channal)){
  65. $power = ShareApi::getResPower($user['user_uid'],$this->channal);
  66. if($power===20){
  67. $data["role"] = 'editor';
  68. }else if($power===10){
  69. $data["role"] = 'reader';
  70. }else{
  71. $data["role"] = 'unknown';
  72. }
  73. }
  74. }
  75. if($request->has('exp')){
  76. //毫秒计算的经验值
  77. $exp = UserOperationDaily::where('user_id',$this->editor_id)
  78. ->where('date_int','<=',date_timestamp_get(date_create($this->updated_at))*1000)
  79. ->sum('duration');
  80. $data['exp'] = (int)($exp/1000);
  81. }
  82. return $data;
  83. }
  84. }