TermResource.php 4.8 KB

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