TermResource.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. use App\Models\DhammaTerm;
  13. class TermResource extends JsonResource
  14. {
  15. /**
  16. * Transform the resource into an array.
  17. *
  18. * @param \Illuminate\Http\Request $request
  19. * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
  20. */
  21. public function toArray($request)
  22. {
  23. $data = [
  24. "id"=>$this->id,
  25. "guid"=>$this->guid,
  26. "word"=> $this->word,
  27. "word_en"=> $this->word_en,
  28. "meaning"=> $this->meaning,
  29. "other_meaning"=> $this->other_meaning,
  30. "tag"=> $this->tag,
  31. "note"=> $this->note,
  32. "language"=> $this->language,
  33. "channal"=> $this->channal,
  34. "studio" => StudioApi::getById($this->owner),
  35. "editor"=> UserApi::getById($this->editor_id),
  36. "created_at"=> $this->created_at,
  37. "updated_at"=> $this->updated_at,
  38. ];
  39. if($request->has('channel') && !empty($request->get('channel'))){
  40. $channels = explode('_',$request->get('channel')) ;
  41. }else{
  42. if(!empty($this->channal) && Str::isUuid($this->channal)){
  43. $channelId = $this->channal;
  44. $data["channel"] = ChannelApi::getById($this->channal);
  45. }else{
  46. $channelId = ChannelApi::getSysChannel('_community_translation_'.$this->language.'_');
  47. if(empty($channelId)){
  48. $channelId = ChannelApi::getSysChannel('_community_translation_zh-hans_');
  49. }
  50. }
  51. if(!empty($channelId)){
  52. $channels = [$channelId];
  53. }else{
  54. $channels = [];
  55. }
  56. }
  57. if(!empty($this->note)){
  58. $mdRender = new MdRender(
  59. [
  60. 'mode'=>$request->get('mode','read'),
  61. 'format'=>'react',
  62. 'studioId'=>$this->owner,
  63. ]);
  64. $data["html"] = $mdRender->convert($this->note,$channels,null);
  65. $summaryContent = $this->note;
  66. }else if($request->has('community_summary')){
  67. $lang = strtolower($this->language);
  68. if($lang==='zh'){
  69. $lang='zh-hans';
  70. }
  71. $community_channel = ChannelApi::getSysChannel("_community_term_{$lang}_");
  72. if(empty($community_channel)){
  73. $community_channel = ChannelApi::getSysChannel('_community_term_zh-hans_');
  74. }
  75. if(Str::isUuid($community_channel)){
  76. //查找社区解释
  77. $community_note = DhammaTerm::where("word",$this->word)
  78. ->where('channal',$community_channel)
  79. ->value('note');
  80. if(!empty($community_note)){
  81. $summaryContent = $community_note;
  82. $data["summary_is_community"] = true;
  83. }
  84. }
  85. }
  86. if(isset($summaryContent)){
  87. $mdRender = new MdRender(
  88. [
  89. 'mode'=>$request->get('mode','read'),
  90. 'format'=>'text',
  91. 'studioId'=>$this->owner,
  92. ]);
  93. $data["summary"] = $mdRender->convert($summaryContent,$channels,null);
  94. }
  95. $user = AuthApi::current($request);
  96. if(!$user){
  97. $data["role"] = 'reader';
  98. }else{
  99. if($this->owner === $user['user_uid']){
  100. $data["role"] = 'owner';
  101. }else if(!empty($this->channal)){
  102. $power = ShareApi::getResPower($user['user_uid'],$this->channal);
  103. if($power===20){
  104. $data["role"] = 'editor';
  105. }else if($power===10){
  106. $data["role"] = 'reader';
  107. }else{
  108. $data["role"] = 'unknown';
  109. }
  110. }
  111. }
  112. if($request->has('exp')){
  113. //毫秒计算的经验值
  114. $exp = UserOperationDaily::where('user_id',$this->editor_id)
  115. ->where('date_int','<=',date_timestamp_get(date_create($this->updated_at))*1000)
  116. ->sum('duration');
  117. $data['exp'] = (int)($exp/1000);
  118. }
  119. return $data;
  120. }
  121. }