TermResource.php 4.9 KB

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