Wang Chen 4 years ago
parent
commit
fd2cf736d0

+ 44 - 0
app/Console/Commands/Push/PushTask.php

@@ -0,0 +1,44 @@
+<?php
+
+
+namespace App\Console\Commands\Push;
+
+
+use App\Modules\Push\Models\QappPushTask;
+use App\Modules\Push\Services\PushService;
+use Illuminate\Console\Command;
+
+class PushTask extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'push:task';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '每分钟执行推送任务';
+
+    /**
+     * @return bool
+     * @throws \App\Exceptions\ApiException
+     * @throws \GuzzleHttp\Exception\GuzzleException
+     */
+    public function handle()
+    {
+        // 获取待推送的任务
+        $pushTask = QappPushTask::getValidTask();
+        $taskId   = getProp($pushTask, 'id');
+        if (!$taskId) {
+            return false;
+        }
+
+        PushService::sendMessage($taskId);
+    }
+
+}

+ 6 - 1
app/Console/Kernel.php

@@ -5,6 +5,7 @@ namespace App\Console;
 use App\Console\Commands\Push\HwPushTest;
 use App\Console\Commands\Push\MiPushTest;
 use App\Console\Commands\Push\OppoPushTest;
+use App\Console\Commands\Push\PushTask;
 use App\Console\Commands\Push\PushTest;
 use Illuminate\Console\Scheduling\Schedule;
 use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
@@ -21,6 +22,7 @@ class Kernel extends ConsoleKernel
         HwPushTest::class,
         OppoPushTest::class,
         PushTest::class,
+        PushTask::class,
         Commands\BookAdjust::class,
         Commands\BookAdjustOne::class,
         Commands\BookSpider::class,
@@ -39,7 +41,7 @@ class Kernel extends ConsoleKernel
     /**
      * Define the application's command schedule.
      *
-     * @param  \Illuminate\Console\Scheduling\Schedule $schedule
+     * @param \Illuminate\Console\Scheduling\Schedule $schedule
      * @return void
      */
     protected function schedule(Schedule $schedule)
@@ -51,5 +53,8 @@ class Kernel extends ConsoleKernel
             }
             return false;
         });
+
+        // 推送任务每分钟执行
+        $schedule->command('push:task')->everyMinute()->sendOutputTo(storage_path('push-' . date('Y-m-d')));
     }
 }

+ 6 - 0
app/Consts/PushConst.php

@@ -6,6 +6,10 @@ namespace App\Consts;
 
 class PushConst
 {
+    const APP_NOT_WORK = 0;
+
+    const APP_WORKING = 1;
+
     const TOPIC_ALL = 'all';
 
     const PROVIDER_HW = 'huawei';
@@ -23,4 +27,6 @@ class PushConst
     const PUSH_STATUS_FAIL = 4;
 
     const PUSH_STATUS_STOP = 5;
+
+    const SELECT_USER_OK = 2;
 }

+ 3 - 2
app/Modules/Push/Models/QappPushApp.php

@@ -4,6 +4,7 @@
 namespace App\Modules\Push\Models;
 
 
+use App\Consts\PushConst;
 use Illuminate\Database\Eloquent\Model;
 
 class QappPushApp extends Model
@@ -18,7 +19,7 @@ class QappPushApp extends Model
             return [];
         }
 
-        return self::where('app_id', $appId)->where('status', 1)->first();
+        return self::where('app_id', $appId)->where('status', PushConst::APP_WORKING)->first();
     }
 
     /**
@@ -73,7 +74,7 @@ class QappPushApp extends Model
             return [];
         }
 
-        $result = self::whereIn('app_id', $appIds)->where('status', 1)->get();
+        $result = self::whereIn('app_id', $appIds)->where('status', PushConst::APP_WORKING)->get();
         return $result ? $result->toArray() : [];
     }
 }

+ 15 - 4
app/Modules/Push/Models/QappPushTask.php

@@ -4,26 +4,26 @@
 namespace App\Modules\Push\Models;
 
 
+use App\Consts\PushConst;
 use Illuminate\Database\Eloquent\Model;
 
 class QappPushTask extends Model
 {
     protected $table = 'qapp_push_task';
     protected $fillable = ['uid', 'package_id', 'type', 'title', 'content', 'url', 'num',
-        'status', 'providers', 'push_time', 'push_filter', 'push_result'];
+        'status', 'providers', 'push_time', 'push_filter', 'push_result', 'select_user_status'];
 
     /**
      * @param $id
      * @return array
      */
-    public static function getPushTaskById($id): array
+    public static function getPushTaskById($id)
     {
         if (empty($id)) {
             return [];
         }
 
-        $result = self::where('id', $id)->first();
-        return $result ? $result->toArray() : [];
+        return self::where('id', $id)->first();
     }
 
     /**
@@ -39,4 +39,15 @@ class QappPushTask extends Model
 
         return self::where('id', $id)->update($data);
     }
+
+    /**
+     * @return mixed
+     */
+    public static function getValidTask()
+    {
+        return self::where('status', PushConst::PUSH_STATUS_TODO)
+            ->where('select_user_status', PushConst::SELECT_USER_OK)
+            ->orderBy('push_time', 'ASC')
+            ->first();
+    }
 }

+ 2 - 1
app/Modules/Push/Services/PushService.php

@@ -111,7 +111,8 @@ class PushService
 
         // 获取任务数据
         $pushTask = QappPushTask::getPushTaskById($taskId);
-        if ((int)getProp($pushTask, 'status') !== 1) {
+        if ((int)getProp($pushTask, 'status') !== PushConst::PUSH_STATUS_TODO ||
+            (int)getProp($pushTask, 'select_user_status') !== PushConst::SELECT_USER_OK) {
             Utils::throwError(ErrorConst::PUSH_TASK_INVALID);
         }