Wang Chen преди 4 години
родител
ревизия
971928bd74
променени са 4 файла, в които са добавени 93 реда и са изтрити 44 реда
  1. 2 1
      app/Console/Commands/Push/PushTask.php
  2. 41 5
      app/Consts/PushConst.php
  3. 19 1
      app/Modules/Push/Models/QappPushTask.php
  4. 31 37
      app/Modules/Push/Services/PushService.php

+ 2 - 1
app/Console/Commands/Push/PushTask.php

@@ -38,7 +38,8 @@ class PushTask extends Command
             return false;
         }
 
-        PushService::sendMessage($taskId);
+        // 执行推送
+        PushService::pushMessageByTaskId($taskId);
     }
 
 }

+ 41 - 5
app/Consts/PushConst.php

@@ -6,27 +6,63 @@ namespace App\Consts;
 
 class PushConst
 {
+    /**
+     * 推送APP 不可用
+     */
     const APP_NOT_WORK = 0;
 
+    /**
+     * 推送APP 可用
+     */
     const APP_WORKING = 1;
 
+    /**
+     * 用户标签 - 全部用户(针对华为设备)
+     */
     const TOPIC_ALL = 'all';
 
+    /**
+     * 华为服务商
+     */
     const PROVIDER_HW = 'huawei';
 
+    /**
+     * 小米服务商
+     */
     const PROVIDER_MI = 'xiaomi';
 
+    /**
+     * OPPO服务商
+     */
     const PROVIDER_OPPO = 'oppo';
 
-    const PUSH_STATUS_TODO = 1;
+    /**
+     * 准备推送
+     */
+    const STATUS_TODO = 1;
 
-    const PUSH_STATUS_DOING = 2;
+    /**
+     * 推送中
+     */
+    const STATUS_DOING = 2;
 
-    const PUSH_STATUS_SUCCESS = 3;
+    /**
+     * 推送成功
+     */
+    const STATUS_SUCCESS = 3;
 
-    const PUSH_STATUS_FAIL = 4;
+    /**
+     * 推送失败
+     */
+    const STATUS_FAIL = 4;
 
-    const PUSH_STATUS_STOP = 5;
+    /**
+     * 推送终止
+     */
+    const STATUS_STOP = 5;
 
+    /**
+     * 已经针对任务筛选完了用户
+     */
     const SELECT_USER_OK = 2;
 }

+ 19 - 1
app/Modules/Push/Models/QappPushTask.php

@@ -41,12 +41,30 @@ class QappPushTask extends Model
     }
 
     /**
+     * 更新主任务状态
+     * @param        $id
+     * @param        $status
+     * @param string $pushResult
+     * @return bool
+     */
+    public static function updatePushTaskStatus($id, $status, string $pushResult = ''): bool
+    {
+        if (empty($id)) {
+            return false;
+        }
+
+        $data = ['status' => $status, 'push_result' => $pushResult, 'updated_at' => date('Y-m-d H:i:s')];
+        return self::where('id', $id)->update($data);
+    }
+
+    /**
      * @return mixed
      */
     public static function getValidTask()
     {
-        return self::where('status', PushConst::PUSH_STATUS_TODO)
+        return self::where('status', PushConst::STATUS_TODO)
             ->where('select_user_status', PushConst::SELECT_USER_OK)
+            ->where('push_time', '<=', date('Y-m-d H:i:s'))
             ->orderBy('push_time', 'ASC')
             ->first();
     }

+ 31 - 37
app/Modules/Push/Services/PushService.php

@@ -4,21 +4,18 @@
 namespace App\Modules\Push\Services;
 
 
+use Exception;
 use App\Cache\PushCache;
 use App\Consts\PushConst;
 use App\Libs\Push\OPPOPush\OPPOPushCommon;
 use App\Libs\Push\XMPush\MiPushCommon;
-use App\Libs\Utils;
-use App\Consts\ErrorConst;
 use App\Libs\Push\HuaWei\HwPushCommon;
 use App\Modules\Push\Models\QappPushApp;
-use App\Exceptions\ApiException;
 use App\Modules\Push\Models\QappPushTask;
 use App\Modules\Push\Models\QappPushTaskLogs;
 use App\Modules\Push\Models\QappPushTaskUsers;
 use App\Modules\Push\Models\QappPushUser;
 use App\Modules\User\Models\QappPackage;
-use Exception;
 use GuzzleHttp\Exception\GuzzleException;
 
 class PushService
@@ -87,9 +84,10 @@ class PushService
             }
         }
 
-        // 更新用户reg id缓存
+        // 更新用户数据库中reg_id
         $result = QappPushUser::setUserRegId($uid, $regId);
         if ($result) {
+            // 更新缓存
             PushCache::setUserPushRegId($uid, $regId);
         }
 
@@ -100,45 +98,41 @@ class PushService
      * 推送消息
      * @param $taskId
      * @return bool
-     * @throws ApiException
      * @throws GuzzleException
      */
-    public static function sendMessage($taskId): bool
+    public static function pushMessageByTaskId($taskId): bool
     {
         if (empty($taskId)) {
             return false;
         }
 
-        // 获取任务数据
+        // 获取任务数据,判断任务状态及发送时间
         $pushTask = QappPushTask::getPushTaskById($taskId);
-        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);
-        }
-
-        // 判断任务时间
-        if (getProp($pushTask, 'push_time') > date('Y-m-d H:i:s')) {
-            Utils::throwError(ErrorConst::PUSH_TASK_NOT_START);
+        if ((int)getProp($pushTask, 'status') !== PushConst::STATUS_TODO ||
+            (int)getProp($pushTask, 'select_user_status') !== PushConst::SELECT_USER_OK ||
+            getProp($pushTask, 'push_time') > date('Y-m-d H:i:s')) {
+            return false;
         }
 
-        // 获取全部子任务
+        // 获取全部子任务,判断子任务是否有效
         $subTasks = QappPushTaskLogs::getPushTaskLogsByTaskId($taskId);
         if (!$subTasks) {
-            Utils::throwError(ErrorConst::PUSH_TASK_LOGS_NOT_FOUND);
+            // 更新主任务失败状态及原因
+            QappPushTask::updatePushTaskStatus($taskId, PushConst::STATUS_FAIL, '无有效子任务');
+            return false;
         }
 
         // 获取推送应用信息
         $pushAppIds = array_column($subTasks, 'app_id');
         $pushApps   = QappPushApp::getPushAppByAppIds($pushAppIds);
         if (!$pushApps) {
-            Utils::throwError(ErrorConst::PUSH_APP_NOT_SET);
+            // 更新主任务失败状态及原因
+            QappPushTask::updatePushTaskStatus($taskId, PushConst::STATUS_FAIL, '无有效推送APP');
+            return false;
         }
 
-        // 更新任务开始状态
-        QappPushTask::updatePushTask(getProp($pushTask, 'id'), [
-            'status'     => PushConst::PUSH_STATUS_DOING,
-            'updated_at' => date('Y-m-d H:i:s')
-        ]);
+        // 更新主任务状态为开始状态
+        QappPushTask::updatePushTaskStatus($taskId, PushConst::STATUS_DOING, '无有效推送APP');
 
         // 判断是全量发送还是批量发送
         if (getProp($pushTask, 'push_filter') === 'all') {
@@ -147,11 +141,9 @@ class PushService
             $result = self::pushMessageToUsers($pushTask, $subTasks, $pushApps);
         }
 
-        // 更新任务执行状态
-        QappPushTask::updatePushTask(getProp($pushTask, 'id'), [
-            'status'     => $result ? PushConst::PUSH_STATUS_SUCCESS : PushConst::PUSH_STATUS_FAIL,
-            'updated_at' => date('Y-m-d H:i:s')
-        ]);
+        // 更新主任务最终状态(成功/失败)
+        $status = $result ? PushConst::STATUS_SUCCESS : PushConst::STATUS_FAIL;
+        QappPushTask::updatePushTaskStatus($taskId, $status);
 
         return $result;
     }
@@ -195,7 +187,7 @@ class PushService
 
             // 更新开始状态
             QappPushTaskLogs::updateData(['id' => $subTaskId], [
-                'status'     => PushConst::PUSH_STATUS_DOING,
+                'status'     => PushConst::STATUS_DOING,
                 'updated_at' => date('Y-m-d H:i:s')
             ]);
 
@@ -230,7 +222,7 @@ class PushService
 
                 // 更新子任务失败状态
                 QappPushTaskLogs::updateData(['id' => $subTaskId], [
-                    'status'      => PushConst::PUSH_STATUS_FAIL,
+                    'status'      => PushConst::STATUS_FAIL,
                     'push_result' => json_encode($result, JSON_UNESCAPED_UNICODE),
                     'updated_at'  => date('Y-m-d H:i:s')
                 ]);
@@ -239,7 +231,7 @@ class PushService
 
             // 更新成功状态
             QappPushTaskLogs::updateData(['id' => $subTaskId], [
-                'status'      => PushConst::PUSH_STATUS_SUCCESS,
+                'status'      => PushConst::STATUS_SUCCESS,
                 'push_result' => json_encode($result, JSON_UNESCAPED_UNICODE),
                 'updated_at'  => date('Y-m-d H:i:s')
             ]);
@@ -253,7 +245,6 @@ class PushService
      * @param $subTasks
      * @param $pushApps
      * @return bool
-     * @throws ApiException
      * @throws GuzzleException
      */
     private static function pushMessageToUsers($pushTask, $subTasks, $pushApps)
@@ -262,9 +253,12 @@ class PushService
         $taskId    = getProp($pushTask, 'id');
         $taskUsers = QappPushTaskUsers::getTaskUsers($taskId);
         if (!$taskUsers) {
-            Utils::throwError(ErrorConst::PUSH_TASK_NO_USERS);
+            // 更新主任务失败状态及原因
+            QappPushTask::updatePushTaskStatus($taskId, PushConst::STATUS_FAIL, '未设置推送用户');
+            return false;
         }
 
+        // 推送结果
         $pushResult = true;
 
         // 推送
@@ -289,7 +283,7 @@ class PushService
 
             // 更新开始状态
             QappPushTaskLogs::updateData(['id' => $subTaskId], [
-                'status'     => PushConst::PUSH_STATUS_DOING,
+                'status'     => PushConst::STATUS_DOING,
                 'updated_at' => date('Y-m-d H:i:s')
             ]);
 
@@ -337,7 +331,7 @@ class PushService
 
                 // 更新子任务失败状态
                 QappPushTaskLogs::updateData(['id' => $subTaskId], [
-                    'status'      => PushConst::PUSH_STATUS_FAIL,
+                    'status'      => PushConst::STATUS_FAIL,
                     'push_result' => json_encode($result, JSON_UNESCAPED_UNICODE),
                     'updated_at'  => date('Y-m-d H:i:s')
                 ]);
@@ -346,7 +340,7 @@ class PushService
 
             // 更新成功状态
             QappPushTaskLogs::updateData(['id' => $subTaskId], [
-                'status'      => PushConst::PUSH_STATUS_SUCCESS,
+                'status'      => PushConst::STATUS_SUCCESS,
                 'push_result' => json_encode($result, JSON_UNESCAPED_UNICODE),
                 'updated_at'  => date('Y-m-d H:i:s')
             ]);