PostRepository.php 3.7 KB

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