PostRepository.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/5
  6. * Time: 16:03
  7. */
  8. namespace App\Repositories\Post;
  9. use App\Models\Post;
  10. use App\Models\PostComment;
  11. use App\Models\PostData;
  12. use App\Models\PostImgs;
  13. use App\Models\PostLog;
  14. use App\Models\Topic;
  15. use Illuminate\Database\QueryException;
  16. use Dingo\Api\Http\Response;
  17. use Illuminate\Support\Facades\DB;
  18. use Illuminate\Support\Facades\Redis;
  19. use Symfony\Component\HttpKernel\Exception\HttpException;
  20. use Tymon\JWTAuth\Facades\JWTAuth;
  21. class PostRepository
  22. {
  23. public function __construct(Post $post,
  24. PostData $postData,
  25. PostComment $postComment,
  26. PostImgs $postImgs,
  27. PostLog $postLog,
  28. Topic $topic)
  29. {
  30. $this->post = $post;
  31. $this->postData = $postData;
  32. $this->postComment = $postComment;
  33. $this->postImgs = $postImgs;
  34. $this->postLog = $postLog;
  35. $this->topic = $topic;
  36. }
  37. /**
  38. * 发布内容
  39. */
  40. public function create($request)
  41. {
  42. //验证小号
  43. //验证话题
  44. $topicIds = $this->topic->whereIn('id', explode(',', $request['topic_ids']))->pluck('id')->toArray();
  45. $topicCount = count($topicIds);
  46. if($topicCount == 0 || $topicCount >= 5){
  47. throw new HttpException(500, '所选话题必须1-5个');
  48. }
  49. $topicIds = implode(',', $topicIds);
  50. $data = [
  51. 'uid' => $request['uid'],
  52. 'username' => '暂无',
  53. 'mobile' => '暂无',
  54. 'avatar' => '暂无',
  55. 'type' => $request['type'],
  56. 'img' => $request['img'],
  57. 'video' => $request['video']??'',
  58. 'topic_ids' => $topicIds,
  59. 'title' => $request['title']??'',
  60. 'content' => $request['content'],
  61. 'location' => $request['location']??'',
  62. 'is_suggest' => 0,
  63. 'is_hide' => 0
  64. ];
  65. $date = date('Y-m-d H:i:s');
  66. DB::beginTransaction();
  67. try{
  68. $post = $this->post->create($data);
  69. $this->postData->create([
  70. 'post_id' => $post->id,
  71. 'pv' => 0,
  72. 'pv_real' => 0,
  73. 'dislike_count' => 0,
  74. 'praise_count' => 0,
  75. 'praise_real_count' => 0,
  76. 'share_count' => 0,
  77. 'share_real_count' => 0,
  78. 'comment_count' => 0,
  79. 'comment_real_count' => 0,
  80. 'collect_count' => 0,
  81. 'collect_real_count' => 0,
  82. 'available_bean' => 0,
  83. 'will_collect_bean' => 0,
  84. 'collect_bean' => 0,
  85. 'weight' => 0
  86. ]);
  87. if($request['imgs'] && $request['type'] == 'image'){
  88. $imgData = [];
  89. foreach($request['imgs'] as $img){
  90. $imgData[] = [
  91. 'post_id' => $post->id,
  92. 'img' => $img,
  93. 'created_at' => $date,
  94. 'updated_at' => $date
  95. ];
  96. }
  97. $this->postImgs->insert($imgData);
  98. }
  99. DB::commit();
  100. return Response::create();
  101. }catch (QueryException $exception){
  102. DB::rollBack();
  103. return Response::create([
  104. 'message' => '发布失败,请重试',
  105. 'error' => $exception->getMessage(),
  106. 'status_code' => 500
  107. ]);
  108. }
  109. }
  110. }