TermResource.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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_uid 装得下 AI 模型的 uuid,getByUuid 查不到人类用户时
  40. // 会回落到 AI 模型;老数据没有这一列,仍按 editor_id 解析。
  41. 'editor' => $this->editor_uid
  42. ? UserApi::getByUuid($this->editor_uid)
  43. : UserApi::getById($this->editor_id),
  44. 'created_at' => $this->created_at,
  45. 'updated_at' => $this->updated_at,
  46. ];
  47. if ($request->has('channel') && ! empty($request->input('channel'))) {
  48. $channels = explode('_', $request->input('channel'));
  49. } else {
  50. if (! empty($this->channel_id) && Str::isUuid($this->channel_id)) {
  51. $channelId = $this->channel_id;
  52. $data['channel'] = ChannelApi::getById($this->channel_id);
  53. } else {
  54. $channelId = ChannelApi::getSysChannel('_community_translation_'.$this->language.'_');
  55. if (empty($channelId)) {
  56. $channelId = ChannelApi::getSysChannel('_community_translation_zh-hans_');
  57. }
  58. }
  59. if (! empty($channelId)) {
  60. $channels = [$channelId];
  61. } else {
  62. $channels = [];
  63. }
  64. }
  65. if (! empty($this->note)) {
  66. $mdRender = new MdRender(
  67. [
  68. 'mode' => $request->input('mode', 'read'),
  69. 'format' => $request->input('format', 'react'),
  70. 'studioId' => $this->owner,
  71. ]
  72. );
  73. $data['html'] = $mdRender->convert($this->note, $channels, null);
  74. $paragraphs = MarkdownHelper::splitByParagraphs($this->note);
  75. if (is_array($paragraphs) && count($paragraphs) > 0) {
  76. $summaryContent = $paragraphs[0];
  77. }
  78. } elseif ($request->has('community_summary')) {
  79. $lang = strtolower($this->language);
  80. if ($lang === 'zh') {
  81. $lang = 'zh-hans';
  82. }
  83. $community_channel = ChannelApi::getSysChannel("_community_term_{$lang}_");
  84. if (empty($community_channel)) {
  85. $community_channel = ChannelApi::getSysChannel('_community_term_zh-hans_');
  86. }
  87. if (Str::isUuid($community_channel)) {
  88. // 查找社区解释
  89. $community_note = DhammaTerm::where('word', $this->word)
  90. ->where('channal', $community_channel)
  91. ->value('note');
  92. if (! empty($community_note)) {
  93. $summaryContent = $community_note;
  94. $data['summary_is_community'] = true;
  95. }
  96. }
  97. }
  98. if (isset($summaryContent)) {
  99. $mdRender = new MdRender(
  100. [
  101. 'mode' => $request->input('mode', 'read'),
  102. 'format' => 'text',
  103. 'studioId' => $this->owner,
  104. ]
  105. );
  106. $data['summary'] = $mdRender->convert($summaryContent, $channels, null);
  107. }
  108. $user = AuthService::current($request);
  109. if (! $user) {
  110. $data['role'] = 'reader';
  111. } else {
  112. if ($this->owner === $user['user_uid']) {
  113. $data['role'] = 'owner';
  114. } elseif (! empty($this->channal)) {
  115. $power = ShareApi::getResPower($user['user_uid'], $this->channal);
  116. if ($power === 20) {
  117. $data['role'] = 'editor';
  118. } elseif ($power === 10) {
  119. $data['role'] = 'reader';
  120. } else {
  121. $data['role'] = 'unknown';
  122. }
  123. }
  124. }
  125. if ($request->has('exp')) {
  126. // 毫秒计算的经验值
  127. $exp = UserOperationDaily::where('user_id', $this->editor_id)
  128. ->where('date_int', '<=', date_timestamp_get(date_create($this->updated_at)) * 1000)
  129. ->sum('duration');
  130. $data['exp'] = (int) ($exp / 1000);
  131. }
  132. return $data;
  133. }
  134. }