TermResource.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. // 列名是 channal;channel_id 只存在于本资源的输出里,模型上取不到,
  51. // 于是所有 channel 内的术语都拿不到 channel 信息、还被当成社区术语渲染。
  52. if (! empty($this->channal) && Str::isUuid($this->channal)) {
  53. $channelId = $this->channal;
  54. $data['channel'] = ChannelApi::getById($this->channal);
  55. } else {
  56. $channelId = ChannelApi::getSysChannel('_community_translation_'.$this->language.'_');
  57. if (empty($channelId)) {
  58. $channelId = ChannelApi::getSysChannel('_community_translation_zh-hans_');
  59. }
  60. }
  61. if (! empty($channelId)) {
  62. $channels = [$channelId];
  63. } else {
  64. $channels = [];
  65. }
  66. }
  67. if (! empty($this->note)) {
  68. $mdRender = new MdRender(
  69. [
  70. 'mode' => $request->input('mode', 'read'),
  71. 'format' => $request->input('format', 'react'),
  72. 'studioId' => $this->owner,
  73. ]
  74. );
  75. $data['html'] = $mdRender->convert($this->note, $channels, null);
  76. $paragraphs = MarkdownHelper::splitByParagraphs($this->note);
  77. if (is_array($paragraphs) && count($paragraphs) > 0) {
  78. $summaryContent = $paragraphs[0];
  79. }
  80. } elseif ($request->has('community_summary')) {
  81. $lang = strtolower($this->language);
  82. if ($lang === 'zh') {
  83. $lang = 'zh-hans';
  84. }
  85. $community_channel = ChannelApi::getSysChannel("_community_term_{$lang}_");
  86. if (empty($community_channel)) {
  87. $community_channel = ChannelApi::getSysChannel('_community_term_zh-hans_');
  88. }
  89. if (Str::isUuid($community_channel)) {
  90. // 查找社区解释
  91. $community_note = DhammaTerm::where('word', $this->word)
  92. ->where('channal', $community_channel)
  93. ->value('note');
  94. if (! empty($community_note)) {
  95. $summaryContent = $community_note;
  96. $data['summary_is_community'] = true;
  97. }
  98. }
  99. }
  100. if (isset($summaryContent)) {
  101. $mdRender = new MdRender(
  102. [
  103. 'mode' => $request->input('mode', 'read'),
  104. 'format' => 'text',
  105. 'studioId' => $this->owner,
  106. ]
  107. );
  108. $data['summary'] = $mdRender->convert($summaryContent, $channels, null);
  109. }
  110. $user = AuthService::current($request);
  111. if (! $user) {
  112. $data['role'] = 'reader';
  113. } else {
  114. if ($this->owner === $user['user_uid']) {
  115. $data['role'] = 'owner';
  116. } elseif (! empty($this->channal)) {
  117. $power = ShareApi::getResPower($user['user_uid'], $this->channal);
  118. if ($power === 20) {
  119. $data['role'] = 'editor';
  120. } elseif ($power === 10) {
  121. $data['role'] = 'reader';
  122. } else {
  123. $data['role'] = 'unknown';
  124. }
  125. }
  126. }
  127. if ($request->has('exp')) {
  128. // 毫秒计算的经验值
  129. $exp = UserOperationDaily::where('user_id', $this->editor_id)
  130. ->where('date_int', '<=', date_timestamp_get(date_create($this->updated_at)) * 1000)
  131. ->sum('duration');
  132. $data['exp'] = (int) ($exp / 1000);
  133. }
  134. return $data;
  135. }
  136. }