TermResource.php 4.6 KB

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