NotificationResource.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace App\Http\Resources;
  3. use Illuminate\Http\Resources\Json\JsonResource;
  4. use Illuminate\Support\Facades\Log;
  5. use App\Http\Api\UserApi;
  6. use App\Models\SentPr;
  7. use App\Models\Sentence;
  8. use App\Http\Api\PaliTextApi;
  9. use App\Http\Api\ChannelApi;
  10. class NotificationResource extends JsonResource
  11. {
  12. /**
  13. * Transform the resource into an array.
  14. *
  15. * @param \Illuminate\Http\Request $request
  16. * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
  17. */
  18. public function toArray($request)
  19. {
  20. $data = [
  21. "id"=>$this->id,
  22. "from"=> UserApi::getByUuid($this->from),
  23. "to"=> UserApi::getByUuid($this->to),
  24. "url"=> $this->url,
  25. "content"=> $this->content,
  26. "content_type"=> $this->content_type,
  27. "res_type"=> $this->res_type,
  28. "res_id"=> $this->res_id,
  29. "status"=> $this->status,
  30. "created_at"=> $this->created_at,
  31. "updated_at"=> $this->updated_at,
  32. ];
  33. switch ($this->res_type) {
  34. case 'suggestion':
  35. $data['channel'] = ChannelApi::getById($this->channel);
  36. $prData = SentPr::where('uid',$this->res_id)->first();
  37. if($prData){
  38. $link = config('mint.server.dashboard_base_path')."/article/para/{$prData->book_id}-{$prData->paragraph}";
  39. $link .= "?book={$prData->book_id}&par={$prData->paragraph}&channel={$prData->channel_uid}";
  40. $link .= "&mode=edit&pr=".$this->res_id;
  41. $data['url'] = $link;
  42. //获取标题
  43. if($prData->book_id < 1000){
  44. $path = json_decode(PaliTextApi::getChapterPath($prData->book_id,$prData->paragraph));
  45. if(count($path)>0){
  46. $data['title'] = end($path)->title;
  47. $data['book_title'] = $path[0]->title;
  48. }else{
  49. Log::error('no path data',['pr data'=>$prData]);
  50. }
  51. //内容
  52. $orgContent = Sentence::where('book_id',$prData->book_id)
  53. ->where('paragraph',$prData->paragraph)
  54. ->where('word_start',$prData->word_start)
  55. ->where('word_end',$prData->word_end)
  56. ->where('channel_uid',$prData->channel_uid)
  57. ->value('content');
  58. $content = '>'. mb_substr($orgContent,0,70,"UTF-8")."\n\n";
  59. $content .= mb_substr($prData->content,0,140,"UTF-8");
  60. $data['content'] = $content;
  61. }
  62. }
  63. break;
  64. default:
  65. # code...
  66. break;
  67. }
  68. return $data;
  69. }
  70. }