zhangchangchun 5 년 전
부모
커밋
129fcdcf1f

+ 68 - 0
app/Console/Commands/PostYesterday.php

@@ -0,0 +1,68 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/6/12
+ * Time: 16:32
+ */
+
+namespace App\Console\Commands;
+
+
+use App\Models\PostData;
+use Illuminate\Console\Command;
+use Illuminate\Support\Carbon;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\Redis;
+
+class PostYesterday extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'post:yesterday';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '昨日内容统计';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct(PostData $postData)
+    {
+        parent::__construct();
+        $this->postData = $postData;
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        $this->line("开始统计昨日内容");
+
+        $postData = $this->postData
+            ->where('created_at', '>=', Carbon::now()->startOfDay()->toDateTimeString())
+            ->select(DB::raw('count(*) as count'), DB::raw('sum(create_bean) as bean'))
+            ->first();
+        Log::info('统计昨日内容'.json_encode($postData));
+        Log::debug('统计昨日内容'.json_encode($postData));
+
+        Redis::set('yesterday_post_count', $postData->count);
+        Redis::set('yesterday_post_create_bean', $postData->bean);
+
+        $this->line("统计昨日内容结束");
+
+    }
+}

+ 6 - 0
app/Console/Kernel.php

@@ -4,6 +4,7 @@ namespace App\Console;
 
 use App\Console\Commands\Apollo;
 use App\Console\Commands\Downloads;
+use App\Console\Commands\PostYesterday;
 use Illuminate\Console\Scheduling\Schedule;
 use Laravel\Lumen\Console\Kernel as ConsoleKernel;
 
@@ -16,6 +17,7 @@ class Kernel extends ConsoleKernel
      */
     protected $commands = [
         Apollo::class,
+        PostYesterday::class,
         Downloads::class
     ];
 
@@ -32,5 +34,9 @@ class Kernel extends ConsoleKernel
         $schedule->command('download:download')
             ->everyMinute()
             ->withoutOverlapping()->appendOutputTo($path);
+
+        $schedule->command('post:yesterday')
+            ->dailyAt('23:50')
+            ->withoutOverlapping()->appendOutputTo($path);
     }
 }

+ 31 - 22
app/Http/Controllers/Behavior/BehaviorController.php

@@ -3,17 +3,18 @@ namespace App\Http\Controllers\Behavior;
 /**
  * Created by PhpStorm.
  * User: durong
- * Date: 2019/6/10
+ * Date: 2019/6/12
  * Time: 上午11:09
  */
 use App\Http\Controllers\Controller;
-use App\Repositories\BehaviorRepository;
+use App\Repositories\Behavior\BehaviorRepository;
 use App\Transformers\Behavior\BehaviorTransformer;
 use Illuminate\Http\Request;
 use Illuminate\Validation\Rule;
 use League\Fractal\Manager;
 use League\Fractal\Pagination\IlluminatePaginatorAdapter;
 use League\Fractal\Resource\Collection;
+use Illuminate\Support\Facades\Validator;
 
 class BehaviorController extends Controller
 {
@@ -27,28 +28,36 @@ class BehaviorController extends Controller
      */
     public function index(Request $request)
     {
-        $behavior_list = $this->behaviorRepository->index($request->all());
-        if ($behavior_list){
-            return $behavior_list;
-        }else{
-            return '没有找到对应行为列表';
+        $bahavior_list = $this->behaviorRepository->index($request->all());
+
+        if ($bahavior_list){
+
+            return $bahavior_list;
+        }
+    }
+
+
+    //登记行为
+    public function create(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'virus_behavior_id' => 'required',
+            'name' => 'required|string',
+            'behavior_level' => ['required',Rule::in(0, 1)],
+            'behavior_cycle_type' => ['required',Rule::in(0, 1)],
+        ]);
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
         }
-        $fractal = new Manager();
-        $resource = new Collection($behavior_list, new BehaviorTransformer());
-        $resource->setPaginator(new IlluminatePaginatorAdapter($behavior_list));
-        $data = $fractal->createData($resource)->toArray();
-        $data['extra'] = [
-            'filters' => [
-                '_id',
-                'behavior_name',
-            ],
-            'columns' => [
-                '_id',
-                'behavior_name',
-            ]
-        ];
-        return $data;
+        return  $this->behaviorRepository->create($request->all());
+    }
+
+    //编辑行为
+    public function edit(Request $request)
+    {
 
     }
 
+
+
 }

+ 25 - 0
app/Http/Controllers/ConfigController.php

@@ -94,6 +94,31 @@ class ConfigController extends Controller
                 1=>'推荐关注',
                 2=>'热门视频',
                 3=>'推荐话题',
+             ],
+            'behavior_status'=>[
+                '1' => '已登记行为',
+                '0' => '未登记行为'
+            ],
+            //行为级别
+            'behavior_level'=>[
+                '1'=>'多级行为',
+                '0'=>'唯一/基础行为'
+            ],
+            //行为周期类型
+            'behavior_cycle_type'=>[
+                '1'=>'长期',
+                '0'=>'普通/短期'
+            ],
+            //触发类型
+            'trigger_type'=>[
+                '1'=>'有效触发',
+                '0'=>'无效触发'
+            ],
+            //发放状态
+            'release_status'=>[
+                '1'=>'正常',
+                '0'=>'异常'
+
             ]
         ];
     }

+ 0 - 7
app/Http/Controllers/Post/PostController.php

@@ -130,13 +130,6 @@ class PostController extends Controller
      */
     public function commentList(Request $request)
     {
-        $validator = Validator::make($request->all(), [
-            'post_id' => 'required|exists:post,id'
-        ]);
-        if ($validator->fails()) {
-            return $this->response->error($validator->errors()->first(), 500);
-        }
-
         $commentList = $this->postRepository->commentList($request->all());
         $fractal = new Manager();
         $resource = new Collection($commentList, new CommentTransformer());

+ 134 - 8
app/Repositories/Behavior/BehaviorRepository.php

@@ -1,9 +1,12 @@
 <?php
-namespace App\Repositories;
+namespace App\Repositories\Behavior;
 use App\Models\Behavior;
 use GuzzleHttp\Client;
 use GuzzleHttp\Exception\RequestException;
+use Illuminate\Support\Facades\DB;
 use Tymon\JWTAuth\Facades\JWTAuth;
+use Dingo\Api\Http\Response;
+use Symfony\Component\HttpKernel\Exception\HttpException;
 
 /**
  * Created by PhpStorm.
@@ -17,26 +20,149 @@ class BehaviorRepository
     public function __construct(Behavior $behavior)
     {
         $this->behavior = $behavior;
+        //初始化virus域名
+        $this->client = new Client([
+            'base_uri' => config('constants.VIRUS_URL'),
+            'timeout' => 3
+        ]);
     }
 
     public function index($request)
     {
         try {
-            $app = config('constants.VIRUS_APP_ID');
             $signKey = [
                 'app' => config('constants.VIRUS_APP_ID')
             ];
             ksort($signKey);
             $signKey = urldecode(http_build_query($signKey));
             $sign = md5(config('constants.VIRUS_APP_SECRET') . $signKey);
-            $url = config('constants.VIRUS_URL') . '/behaviorList';
-            $array = [
-                'json' => ['app' => $app,'sign' => $sign], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
+
+            $response = $this->client->request('GET', 'behaviorList', [
+                'headers' => [
+                    'Content-Type' => 'application/json',
+                    'app' => config('constants.VIRUS_APP_ID'),
+                    'sign' => $sign
+                ]
+            ]);
+            $res = json_decode($response->getBody()->getContents(), true);
+            $res = $res ? $res['behaviors'] : [];
+        }catch (RequestException $exception){
+            $res = [
+                'returnCode' => '-1',
+                'errMsg' => '网络超时'.$exception->getMessage()
             ];
+        }
+        //已登记行为
+        $registered_bahavior = $this->behavior->get();
+        $registered_bahaviors = $registered_bahavior->toArray();
+
+        foreach ($registered_bahaviors as $key=>$val){
+            $registered_bahaviors[$key]['behavior_status'] = 1;//已登记
+        }
+        $virus_behavior_id = array_column($registered_bahaviors,'virus_behavior_id');
+
+        $new_res = [];
+        //行为列表去重
+        foreach ($res as $k=>$v) {
+            if (isset($v['_id']) && !empty($virus_behavior_id)){
+                    $res[$k]['behavior_status'] = 0;//未登记
+                if (in_array($v['_id'], $virus_behavior_id)) {
+                    unset($res[$k]);
+                }
+                    $new_res = array_merge($res, $registered_bahaviors);
+
+            }elseif (!isset($v['_id']) && $virus_behavior_id) {
+                    $new_res = $registered_bahaviors;
+            }elseif (isset($v['_id']) && empty($virus_behavior_id)){
+                    $res[$k]['behavior_status'] = 0;//未登记
+                    $new_res = $res;
+            }
+        }
+        $new_re = [];
+        //根据行为ID筛选
+        if (isset($request['virus_behavior_id'])){
+            foreach ($new_res as $k=>$v){
+                if (isset($v['virus_behavior_id']) && $v['virus_behavior_id'] != $request['virus_behavior_id']){
+                    unset($new_res[$k]);
+                    $new_re = array_merge($new_res);
+                }elseif (isset($v['_id']) && $v['_id'] != $request['virus_behavior_id']){
+                    unset($new_res[$k]);
+                    $new_re = array_merge($new_res);
+                }
+            }
+            return $new_re;
+        }
+        //根据行为状态(类型)筛选
+        if (isset($request['behavior_status'])){
+            foreach ($new_res as $kk=>$vv){
+                if (isset($vv['behavior_status']) && $vv['behavior_status'] != $request['behavior_status']){
+                    unset($new_res[$kk]);
+                    $new_re = array_merge($new_res);
+                }
+            }
+            return $new_re;
+        }
+        //根据绑定的动作ID筛选
+        if (isset($request['behavior_action_id'])){
+            foreach ($new_res as $key=>$val) {
+                if (isset($val['behavior_action_id']) && ($val['behavior_action_id'] != $request['behavior_action_id'])) {
+                    unset($new_res[$key]);
+                    $new_re = array_merge($new_res);
+                } elseif (!isset($val['behavior_action_id'])) {
+                    unset($new_res[$key]);
+                    $new_re = array_merge($new_res);
+                }
+            }
+            return $new_re;
+        }
+        //根据行为名称筛选
+        if (isset($request['name'])){
+            foreach ($new_res as $key => $val) {
+                if (isset($val['name']) && strpos($val['name'],$request['name']) !== false) {
+                    $new_re[] = $new_res[$key];
+                }
+                if (isset($val['behavior_name']) && strpos($val['behavior_name'],$request['name']) !== false ) {
+                    $new_re[] = $new_res[$key];
+                }
+            }
+            return $new_re;
+        }
+        return $new_res;
+    }
+
+    public function create($request)
+    {
+        $behavior_name = $this->behavior->where(['name'=>$request['name']])->first();
+        if($behavior_name){
+            return Response::create([
+                'message'  => '该行为已存在',
+                'status_code'   => 500
+            ]);
+        }
+        $data = [
+            'virus_behavior_id' => $request['virus_behavior_id'],
+            'name' => $request['name'],
+            'behavior_level' => $request['behavior_level'],
+            'behavior_cycle_type' => $request['behavior_cycle_type'],
+            'is_open' => 0,
+            'behavior_action_id' => isset($request['behavior_action_id']) ? $request['behavior_action_id'] : 0,
+            'behavior_cycle' => isset($request['behavior_cycle']) ? $request['behavior_cycle'] : '',
+            'behavior_binding_users' => isset($request['behavior_binding_users']) ? $request['behavior_binding_users'] : 0,
+            'physical_strength' => isset($request['physical_strength']) ? $request['physical_strength'] : '',
+            'rainbow_beans' => isset($request['rainbow_beans']) ? $request['rainbow_beans'] : '',
+            'remarks' => isset($request['remarks']) ? $request['remarks'] : '',
+            'behavioral_cycle_start_time' => isset($request['behavioral_cycle_start_time']) ? $request['behavioral_cycle_start_time'] : null,
+            'behavioral_cycle_end_time' => isset($request['behavioral_cycle_end_time']) ? $request['behavioral_cycle_end_time'] : null,
+            'allotted_quantity_rule' => isset($request['allotted_quantity_rule']) ? json_encode($request['allotted_quantity_rule']) : '',
+            'behavior_identification' => isset($request['behavior_identification']) ? $request['behavior_identification'] : '',
+            'trigger_times' => isset($request['trigger_times']) ? $request['trigger_times'] : 0,
+            'effective_trigger' => isset($request['effective_trigger']) ? $request['effective_trigger'] : 0,
+            'relative_series' => isset($request['relative_series']) ? $request['relative_series'] : 0,
+            'absolute_progression' => isset($request['absolute_progression']) ? $request['absolute_progression'] : 0,
+        ];
 
-            return http($url, $array, 'get');
-        } catch (\Exception $e) {
-            return [];
+        if (!$this->behavior->create($data)) {
+            throw new HttpException(500, '添加失败,请重试');
         }
     }
 }

+ 10 - 1
app/Repositories/Post/PostRepository.php

@@ -365,8 +365,17 @@ class PostRepository
     {
         $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
 
+        $where = [];
+        if(isset($request['post_id'])){
+            $where[] = ['post_id', $request['post_id']];
+        }
+
+        if(isset($request['uid'])){
+            $where[] = ['uid', $request['uid']];
+        }
+
         return $this->postComment
-            ->where('post_id', $request['post_id'])
+            ->where($where)
             ->orderBy('id','desc')
             ->paginate($perPage);
     }

+ 5 - 4
composer.json

@@ -8,16 +8,17 @@
         "php": ">=7.1.3",
         "cviebrock/laravel-elasticsearch": "^3.5",
         "dingo/api": "^2.2",
+        "doctrine/dbal": "^2.9",
         "guzzlehttp/guzzle": "^6.3",
         "hhxsv5/laravel-s": "~3.4.0",
+        "illuminate/redis": "^5.8",
         "laravel/lumen-framework": "5.8.*",
+        "league/csv": "^9.1",
         "multilinguals/apollo-client": "^0.1.2",
         "php-amqplib/php-amqplib": "^2.9",
-        "tymon/jwt-auth": "1.0.0-rc.4.1",
-        "vlucas/phpdotenv": "^3.3",
-        "illuminate/redis": "^5.8",
         "predis/predis": "^1.1",
-        "league/csv": "^9.1"
+        "tymon/jwt-auth": "1.0.0-rc.4.1",
+        "vlucas/phpdotenv": "^3.3"
     },
     "require-dev": {
         "fzaninotto/faker": "^1.4",

+ 1 - 1
config/constants.php

@@ -7,5 +7,5 @@
 return [
     'VIRUS_APP_ID' => env('VIRUS_APP_ID', '5cfdcf97249e6a00082639d3'),
     'VIRUS_APP_SECRET' => env('VIRUS_APP_SECRET', '4f2afc9c4099ee1f39c9f551123e54bd'),
-    'VIRUS_URL' => env('VIRUS_URL', 'https://api.dev.caihongxingqiu.com/virus'),
+    'VIRUS_URL' => env('VIRUS_URL', 'https://api.dev.caihongxingqiu.com/virus/'),
 ];

+ 32 - 0
database/migrations/2019_06_13_143624_update_behavior_action_id_to_behavior_table.php

@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class UpdateBehaviorActionIdToBehaviorTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('behavior', function (Blueprint $table) {
+            $table->string('behavior_action_id')->nullable()->change();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('behavior', function (Blueprint $table) {
+            //
+        });
+    }
+}

+ 2 - 0
routes/api.php

@@ -89,5 +89,7 @@ $api->version('v1', [
     $api->group(['namespace' => 'Behavior'], function ($api) {
         //行为列表
         $api->get('behavior/list', 'BehaviorController@index');
+        //登记/注册行为
+        $api->post('behavior/create', 'BehaviorController@create');
     });
 });