123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/6/5
- * Time: 16:03
- */
- namespace App\Repositories\Post;
- use App\Models\Post;
- use App\Models\PostComment;
- use App\Models\PostData;
- use App\Models\PostImgs;
- use App\Models\PostLog;
- use App\Models\Topic;
- use Illuminate\Database\QueryException;
- use Dingo\Api\Http\Response;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Redis;
- use Symfony\Component\HttpKernel\Exception\HttpException;
- use Tymon\JWTAuth\Facades\JWTAuth;
- class PostRepository
- {
- public function __construct(Post $post,
- PostData $postData,
- PostComment $postComment,
- PostImgs $postImgs,
- PostLog $postLog,
- Topic $topic)
- {
- $this->post = $post;
- $this->postData = $postData;
- $this->postComment = $postComment;
- $this->postImgs = $postImgs;
- $this->postLog = $postLog;
- $this->topic = $topic;
- }
- /**
- * 发布内容
- */
- public function create($request)
- {
- //验证小号
- //验证话题
- $topicIds = $this->topic->whereIn('id', explode(',', $request['topic_ids']))->pluck('id')->toArray();
- $topicCount = count($topicIds);
- if($topicCount == 0 || $topicCount >= 5){
- throw new HttpException(500, '所选话题必须1-5个');
- }
- $topicIds = implode(',', $topicIds);
- $data = [
- 'uid' => $request['uid'],
- 'username' => '暂无',
- 'mobile' => '暂无',
- 'avatar' => '暂无',
- 'type' => $request['type'],
- 'img' => $request['img'],
- 'video' => $request['video']??'',
- 'topic_ids' => $topicIds,
- 'title' => $request['title']??'',
- 'content' => $request['content'],
- 'location' => $request['location']??'',
- 'is_suggest' => 0,
- 'is_hide' => 0
- ];
- $date = date('Y-m-d H:i:s');
- DB::beginTransaction();
- try{
- $post = $this->post->create($data);
- $this->postData->create([
- 'post_id' => $post->id,
- 'pv' => 0,
- 'pv_real' => 0,
- 'dislike_count' => 0,
- 'praise_count' => 0,
- 'praise_real_count' => 0,
- 'share_count' => 0,
- 'share_real_count' => 0,
- 'comment_count' => 0,
- 'comment_real_count' => 0,
- 'collect_count' => 0,
- 'collect_real_count' => 0,
- 'available_bean' => 0,
- 'will_collect_bean' => 0,
- 'collect_bean' => 0,
- 'weight' => 0
- ]);
- if($request['imgs'] && $request['type'] == 'image'){
- $imgData = [];
- foreach($request['imgs'] as $img){
- $imgData[] = [
- 'post_id' => $post->id,
- 'img' => $img,
- 'created_at' => $date,
- 'updated_at' => $date
- ];
- }
- $this->postImgs->insert($imgData);
- }
- DB::commit();
- return Response::create();
- }catch (QueryException $exception){
- DB::rollBack();
- Log::debug('发布内容:'.$exception->getMessage());
- return Response::create([
- 'message' => '发布失败,请重试',
- 'error' => $exception->getMessage(),
- 'status_code' => 500
- ]);
- }
- }
- }
|