NotificationResource.php 2.9 KB

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