TermResource.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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(!empty($this->channal)){
  38. $channelId = $this->channal;
  39. $data["channel"] = ChannelApi::getById($this->channal);
  40. }else{
  41. $channelId = ChannelApi::getSysChannel('_community_translation_'.$this->language.'_');
  42. if(empty($channelId)){
  43. $channelId = ChannelApi::getSysChannel('_community_translation_zh-hans_');
  44. }
  45. }
  46. if(!empty($this->note) && !empty($channelId)){
  47. $data["html"] = MdRender::render($this->note,[$channelId],null,'read');
  48. }
  49. $user = AuthApi::current($request);
  50. if(!$user){
  51. $data["role"] = 'reader';
  52. }else{
  53. if($this->owner === $user['user_uid']){
  54. $data["role"] = 'owner';
  55. }else if(!empty($this->channal)){
  56. $power = ShareApi::getResPower($user['user_uid'],$this->channal);
  57. if($power===20){
  58. $data["role"] = 'editor';
  59. }else if($power===10){
  60. $data["role"] = 'reader';
  61. }else{
  62. $data["role"] = 'unknown';
  63. }
  64. }
  65. }
  66. if($request->has('exp')){
  67. //毫秒计算的经验值
  68. $exp = UserOperationDaily::where('user_id',$this->editor_id)
  69. ->where('date_int','<=',date_timestamp_get(date_create($this->updated_at))*1000)
  70. ->sum('duration');
  71. $data['exp'] = (int)($exp/1000);
  72. }
  73. return $data;
  74. }
  75. }