TermResource.php 4.9 KB

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