NotificationResource.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace App\Http\Resources;
  3. use App\Http\Api\ChannelApi;
  4. use App\Http\Api\PaliTextApi;
  5. use App\Http\Api\UserApi;
  6. use App\Models\Discussion;
  7. use App\Models\Sentence;
  8. use App\Models\SentPr;
  9. use App\Models\Wbw;
  10. use Illuminate\Contracts\Support\Arrayable;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Http\Resources\Json\JsonResource;
  13. use Illuminate\Support\Facades\Log;
  14. class NotificationResource extends JsonResource
  15. {
  16. /**
  17. * Transform the resource into an array.
  18. *
  19. * @param Request $request
  20. * @return array|Arrayable|\JsonSerializable
  21. */
  22. public function toArray($request)
  23. {
  24. $data = [
  25. 'id' => $this->id,
  26. 'from' => UserApi::getByUuid($this->from),
  27. 'to' => UserApi::getByUuid($this->to),
  28. 'url' => $this->url,
  29. 'content' => $this->content,
  30. 'content_type' => $this->content_type,
  31. 'res_type' => $this->res_type,
  32. 'res_id' => $this->res_id,
  33. 'status' => $this->status,
  34. 'created_at' => $this->created_at,
  35. 'updated_at' => $this->updated_at,
  36. ];
  37. $data['channel'] = ChannelApi::getById($this->channel);
  38. switch ($this->res_type) {
  39. case 'suggestion':
  40. $prData = SentPr::where('uid', $this->res_id)->first();
  41. if ($prData) {
  42. $link = config('mint.server.dashboard_base_path')."/article/para/{$prData->book_id}-{$prData->paragraph}";
  43. $link .= "?book={$prData->book_id}&par={$prData->paragraph}&channel={$prData->channel_uid}";
  44. $link .= '&mode=edit&pr='.$this->res_id;
  45. $data['url'] = $link;
  46. // 获取标题
  47. if ($prData->book_id < 1000) {
  48. $path = json_decode(PaliTextApi::getChapterPath($prData->book_id, $prData->paragraph));
  49. if (count($path) > 0) {
  50. $data['title'] = end($path)->title;
  51. $data['book_title'] = $path[0]->title;
  52. } else {
  53. Log::error('no path data', ['pr data' => $prData]);
  54. }
  55. // 内容
  56. $orgContent = Sentence::where('book_id', $prData->book_id)
  57. ->where('paragraph', $prData->paragraph)
  58. ->where('word_start', $prData->word_start)
  59. ->where('word_end', $prData->word_end)
  60. ->where('channel_uid', $prData->channel_uid)
  61. ->value('content');
  62. $content = '>'.mb_substr($orgContent, 0, 70, 'UTF-8')."\n\n";
  63. $content .= mb_substr($prData->content, 0, 140, 'UTF-8');
  64. $data['content'] = $content;
  65. }
  66. }
  67. break;
  68. case 'discussion':
  69. $discussion = Discussion::where('id', $this->res_id)->first();
  70. if ($discussion->parent) {
  71. $topic = Discussion::where('id', $discussion->parent)->first();
  72. }
  73. if ($discussion) {
  74. $link = config('mint.server.dashboard_base_path').'/discussion/topic/';
  75. if (isset($topic)) {
  76. $link .= "{$topic->id}#{$discussion->id}";
  77. } else {
  78. $link .= "{$discussion->id}";
  79. }
  80. $data['url'] = $link;
  81. // 标题
  82. switch ($discussion->res_type) {
  83. case 'sentence':
  84. break;
  85. case 'wbw':
  86. $wbw = Wbw::where('uid', $discussion->res_id)->first();
  87. if ($wbw) {
  88. $data['title'] = $wbw->word;
  89. }
  90. break;
  91. default:
  92. break;
  93. }
  94. /*
  95. {
  96. $path = json_decode(PaliTextApi::getChapterPath($prData->book_id,$prData->paragraph));
  97. if(count($path)>0){
  98. $data['title'] = end($path)->title;
  99. $data['book_title'] = $path[0]->title;
  100. }else{
  101. Log::error('no path data',['pr data'=>$prData]);
  102. }
  103. //内容
  104. $orgContent = Sentence::where('book_id',$prData->book_id)
  105. ->where('paragraph',$prData->paragraph)
  106. ->where('word_start',$prData->word_start)
  107. ->where('word_end',$prData->word_end)
  108. ->where('channel_uid',$prData->channel_uid)
  109. ->value('content');
  110. $content = '>'. mb_substr($orgContent,0,70,"UTF-8")."\n\n";
  111. $content .= mb_substr($prData->content,0,140,"UTF-8");
  112. $data['content'] = $content;
  113. }
  114. */
  115. }
  116. break;
  117. case 'task':
  118. $link = config('mint.server.dashboard_base_path')."/article/task/{$this->res_id}";
  119. $data['url'] = $link;
  120. break;
  121. default:
  122. // code...
  123. break;
  124. }
  125. return $data;
  126. }
  127. }