NotificationResource.php 5.6 KB

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