xielin лет назад: 5
Родитель
Сommit
00842b6bbe
3 измененных файлов с 243 добавлено и 1 удалено
  1. 58 0
      app/Console/Commands/PostCreate.php
  2. 6 1
      app/Console/Kernel.php
  3. 179 0
      app/Repositories/PostRepositories.php

+ 58 - 0
app/Console/Commands/PostCreate.php

@@ -0,0 +1,58 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Models\Post;
+use App\Models\PostData;
+use App\Repositories\PostRepositories;
+use Carbon\Carbon;
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\Redis;
+
+class PostCreate extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'post:create';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '根据数据创建帖子';
+
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct(PostRepositories $postRepositories)
+    {
+        parent::__construct();
+        $this->postRepositories = $postRepositories;
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        $this->line(date('Y-m-d H:i:s').'开始创建帖子');
+        $key = "community_post_create";
+        $posts = Redis::smembers($key);
+        Log::debug('帖子数据:' . json_encode($posts));
+        foreach ($posts as $post) {
+            $row = $this->postRepositories->createPost(json_decode($post));
+            Log::debug(date("Y-m-d H:i:s") . "创建帖子:" . json_encode($row));
+        }
+        $this->line(date('Y-m-d H:i:s').' 创建帖子结束');
+    }
+}

+ 6 - 1
app/Console/Kernel.php

@@ -10,6 +10,7 @@ use App\Console\Commands\ContentFeedCreate;
 use App\Console\Commands\ContentFeedDelete;
 use App\Console\Commands\DelPostNewReply;
 use App\Console\Commands\ExcellentResidents;
+use App\Console\Commands\PostCreate;
 use App\Console\Commands\RankingList;
 use App\Console\Commands\Tem\DelMemberRepeatTopic;
 use App\Console\Commands\Tem\DelPostNewComment;
@@ -34,7 +35,8 @@ class Kernel extends ConsoleKernel
         DelPostNewComment::class,
         DelMemberRepeatTopic::class,
         RankingList::class,
-        CalcCircleMessageWeight::class
+        CalcCircleMessageWeight::class,
+        PostCreate::class
     ];
 
     /**
@@ -56,5 +58,8 @@ class Kernel extends ConsoleKernel
             ->withoutOverlapping()->appendOutputTo($path);
         $schedule->command('circle:calc_weight')
             ->everyFiveMinutes()->withoutOverlapping()->appendOutputTo($path);
+
+        $schedule->command('post:create')
+            ->everyMinute()->withoutOverlapping()->appendOutputTo($path);
     }
 }

+ 179 - 0
app/Repositories/PostRepositories.php

@@ -923,4 +923,183 @@ class PostRepositories
         return $this->detectionService->checkImg($img);
     }
 
+    /**
+     * 发布内容--程序对接使用
+     */
+    public function createPost($request)
+    {
+        //验证小号
+        $isValid = 1;
+
+        $detectionText = strip_tags($request['title']) . ',' . strip_tags($request['content']);
+        $detectionTextResult = $this->detectionService->checkText($detectionText);
+        if ($detectionTextResult['code'] < 0) {
+            return jsonError('内容违规,请修正哦');
+        }
+
+        $topicIds = json_decode($request['topic_ids'], true);
+        $topicCount = count($topicIds);
+        if ($topicCount == 0 || $topicCount > 2) {
+            return jsonError('所选话题必须1-2个');
+        }
+        //验证话题
+        $hasTopicCount = $this->topic->whereIn('id', $topicIds)->count();
+        if ($topicCount != $hasTopicCount) {
+            Log::error('所选话题非法' . $request['topic_ids']);
+            return jsonError('所选话题非法');
+        }
+        $imgs = [];
+        if ($request['type'] == 'image') {
+            $imgs = json_decode($request['imgs'], true);
+            $imgCount = count($imgs);
+            if ($imgCount == 0 || $imgCount > 9) {
+                return jsonError('所传图集必须1-9个');
+            }
+        }
+        $allImg = array_merge($imgs, [$request['img']]);
+        foreach ($allImg as &$img) {
+            $img = $img . '&x-oss-process=image/resize,p_50/quality,Q_50';
+        }
+        $detectionImageResult = $this->detectionService->checkImg($allImg);
+        if ($detectionImageResult['code'] < 0) {
+            Log::debug('图片违规,请修正哦' . json_encode($detectionImageResult));
+            return jsonError('图片违规,请修正哦');
+        }
+        //如果为html时候,则imgs只是去把富文本的图片拿去鉴黄,但不做其他处理
+        if ($request['type'] == 'html') {
+            $imgs = [];
+        }
+        $videoUrl = "";
+        $videoId = "";
+        if (isset($request['video']) && $request['video']) {
+            $videoId = $request['video'];
+            for ($i = 0; $i < 3; $i++) {
+                $videoUrl = $this->aliYunVodService->getPlayUrlByVideoId($request['video']);
+                Log::debug('video-url:' . $videoUrl);
+                if ($videoUrl) {
+                    break;
+                }
+            }
+            if (empty($videoUrl)) {
+                return jsonError('发布失败,请重试');
+            }
+        }
+
+        $fresh = (Carbon::now()->timestamp) - (Carbon::parse("2019-05-01 00:00:00")->timestamp);
+        $score = $fresh / 43200;
+        $data = [
+            'uid' => $request['uid'],
+            'username' => $request['username'],
+            'mobile' => $request['mobile'],
+            'avatar' => $request['avatar'] ?? '',
+            'type' => $request['type'],
+            'img' => $request['img'],
+            'video' => $videoUrl,
+            'video_id' => $videoId,
+            'topic_ids' => implode(',', $topicIds),
+            'title' => isset($request['title']) ? strip_tags($request['title']) : '',
+            'content' => $request['content'],
+            'lat' => isset($request['lat']) ? number_format($request['lat'],8) : 0,
+            'lng' => isset($request['lng']) ? number_format($request['lng'],8) : 0,
+            'location' => isset($request['location']) ? strip_tags($request['location']) : '',
+            'is_suggest' => 0,
+            'is_hide' => 0,
+            'weight' => $score
+        ];
+
+        $date = date('Y-m-d H:i:s');
+
+        DB::beginTransaction();
+        try {
+            $post = $this->post->create($data);
+
+            $postData = $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,
+                'collect_count' => 0,
+                'collect_real_count' => 0,
+                'available_bean' => $this->availableBean(),
+                'will_collect_bean' => rand(100, 200),
+                'collect_bean' => 0
+            ]);
+
+            if ($imgs) {
+                $imgData = [];
+                foreach ($imgs as $img) {
+                    $imgData[] = [
+                        'post_id' => $post->id,
+                        'img' => $img,
+                        'created_at' => $date,
+                        'updated_at' => $date
+                    ];
+                }
+                $this->postImgs->insert($imgData);
+            }
+
+            DB::commit();
+            Redis::zadd('post_trigger_type', $isValid, $post->id);
+            foreach ($topicIds as $id) {
+                Redis::zincrby('topic.user_uid' . $request['uid'], 1, $id);
+                Redis::zincrby('topic.just_use_count', 1, $id);
+            }
+            Redis::HSET('post_info_' . $post->id,
+                'id', $post->id,
+                'uid', $post->uid,
+                'type', $post->type,
+                'img', $post->img,
+                'imgs', json_encode($imgs),
+                'video', $post->video,
+                'topic_ids', $post->topic_ids,
+                'is_fine', $post->is_fine,
+                'title', $post->title,
+                'content', $post->content,
+                'location', $post->location,
+                'pv', $postData->pv,
+                'dislike_count', $postData->dislike_count,
+                'praise_count', $postData->praise_count,
+                'share_count', $postData->share_count,
+                'comment_count', $postData->comment_count,
+                'collect_count', $postData->collect_count,
+                'available_bean', $postData->available_bean,
+                'will_collect_bean', $postData->will_collect_bean,
+                'create_bean', $postData->create_bean,
+                'collect_bean', $postData->collect_bean,
+                'created_at', $post->created_at);
+            Log::info('post_create:' . $post->id . ',post_author:' . $post->uid . ',author_ip:' . getClientIp());
+            $behavior = Behavior::where('behavior_identification', 'publish')->first();
+            if ($behavior) {
+                $data = [
+                    "behavior_id" => $behavior['virus_behavior_id'],
+                    "behavior_flag" => "publish",
+                    "post_id" => $post->id,
+                    "post_type" => $post->type,
+                    "post_desc" => $post->title?$post->title:subtext($post->conetnt,100),
+                    "post_cover" => $post->img,
+                    "target_id" => $post->uid,
+                    "action_id" => $post->id,  //传帖子ID
+                ];
+                Log::debug('h5邀请用户注册' . json_encode($data));
+                //扔进virus队列
+                $rabbit = new RabbitMqUtil();
+                $rabbit->push('virus_add', $data);
+            }
+            return jsonSuccess([
+                'post_id' => $post->id,
+                'h5url' => config('customer.share_post_h5url') . "?post_id={$post->id}&invite_code={$userInfo['invite_code']}",
+                'bean' => $postData->available_bean,
+            ]);
+
+        } catch (QueryException $exception) {
+            DB::rollBack();
+            Log::debug('发布内容失败:' . $exception->getMessage());
+            return jsonError('发布内容失败,请重试');
+        }
+    }
 }