Wang Chen 4 years ago
parent
commit
e3620f26a5

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

@@ -113,7 +113,8 @@ class PushTest extends Command
                     // 初始化oppo推送
                     $client = new VPush($appId, $appKey, $appSecret);
 
-                    // 设置相关推送数据,保存消息内容
+                    // 设置相关推送数据,保存消息内容(批量推送至少需要两个用户,故写死一个reg_id)
+                    $regIdList[] = '16004127020450881896432';
                     $client->setPushData($title, $content, $url, $regIdList);
                     $client->saveListPayload();
 

+ 51 - 13
app/Libs/Push/VPush/VPush.php

@@ -83,11 +83,11 @@ class VPush
             'skipType'        => $this->_skipType,
             'skipContent'     => $this->_skipContent,
             'networkType'     => $this->_networkType,
-            'clientCustomMap' => [],
+            'clientCustomMap' => (object)[],
         ];
 
         // 请求服务接口
-        $result        = $this->getData(config('push.vivo.saveListPayload'), $data);
+        $result        = $this->getData(config('push.server.vivo.saveListPayload'), $data);
         $this->_taskId = getProp($result, 'taskId');
         return $result;
     }
@@ -100,7 +100,7 @@ class VPush
     public function sendMessage()
     {
         // 参数判断
-        $this->checkParam();
+        $this->checkParam(true);
         if (!$this->_taskId) {
             throw new InvalidArgumentException('VIVO推送必须要设置taskId');
         }
@@ -111,7 +111,7 @@ class VPush
             'requestId' => Utils::randCode(),
         ];
 
-        return $this->getData(config('push.vivo.pushToList'), $data);
+        return $this->getData(config('push.server.vivo.pushToList'), $data);
     }
 
     /**
@@ -122,7 +122,7 @@ class VPush
     public function sendAll()
     {
         // 校验参数
-        $this->checkParam();
+        $this->checkSendAllParam();
         $data = [
             'requestId'       => Utils::randCode(),
             'title'           => $this->_title,
@@ -132,10 +132,10 @@ class VPush
             'skipType'        => $this->_skipType,
             'skipContent'     => $this->_skipContent,
             'networkType'     => $this->_networkType,
-            'clientCustomMap' => [],
+            'clientCustomMap' => (object)[],
         ];
 
-        return $this->getData(config('push.vivo.sendAll'), $data);
+        return $this->getData(config('push.server.vivo.sendAll'), $data);
     }
 
     public function addTagForUsers($tag, $regIds)
@@ -164,7 +164,7 @@ class VPush
         $this->getAccessToken();
 
         // 拼接链接
-        $url = config('push.vivo.getStatistics');
+        $url = config('push.server.vivo.getStatistics');
         $url .= '?taskIds=' . implode(',', $taskIds);
 
         return $this->getData($url, [], 'GET');
@@ -200,9 +200,46 @@ class VPush
         if (!$this->_content) {
             throw new InvalidArgumentException('VIVO推送必须要设置content');
         }
+        if (!$this->_timeToLive) {
+            throw new InvalidArgumentException('VIVO推送必须要设置timeToLive');
+        }
         if (!$this->_regId) {
             throw new InvalidArgumentException('VIVO推送必须要设置regId');
         }
+        return $this;
+    }
+
+    /**
+     * 校验参数
+     * @param false $sendAll
+     * @return $this
+     * @throws GuzzleException
+     */
+    private function checkSendAllParam()
+    {
+        // 授权token校验
+        if (!$this->_accessToken) {
+            $this->getAccessToken();
+        }
+
+        if (!$this->_appId) {
+            throw new InvalidArgumentException('VIVO推送必须要设置appId');
+        }
+        if (!$this->_appKey) {
+            throw new InvalidArgumentException('VIVO推送必须要设置appKey');
+        }
+        if (!$this->_appSecret) {
+            throw new InvalidArgumentException('VIVO推送必须要设置appSecret');
+        }
+        if (!$this->_accessToken) {
+            throw new InvalidArgumentException('VIVO推送必须要设置AccessToken');
+        }
+        if (!$this->_title) {
+            throw new InvalidArgumentException('VIVO推送必须要设置title');
+        }
+        if (!$this->_content) {
+            throw new InvalidArgumentException('VIVO推送必须要设置content');
+        }
         if (!$this->_timeToLive) {
             throw new InvalidArgumentException('VIVO推送必须要设置timeToLive');
         }
@@ -223,7 +260,7 @@ class VPush
         ];
         $sign               = md5($sendData['appId'] . $sendData['appKey'] . $sendData['timestamp'] . $this->_appSecret);
         $sendData['sign']   = $sign;
-        $url                = config('push.vivo.auth');
+        $url                = config('push.server.vivo.auth');
         $data               = $this->getData($url, $sendData);
         $this->_accessToken = getProp($data, 'authToken');
 
@@ -241,20 +278,21 @@ class VPush
     private function getData($url, $sendData, $method = 'POST')
     {
         // 组装请求数据
-        $option = ['json' => $sendData];
+        $authToken = $this->_accessToken;
+        $option    = ['json' => $sendData];
 
         // 请求
-        $client = new Client(['timeout' => 10.0, 'headers' => ['auth_token' => $this->_accessToken]]);
+        $client = new Client(['timeout' => 5.0, 'headers' => ['authToken' => $authToken]]);
         if ($method === 'POST') {
             $response = $client->request('POST', $url, $option);
         } else {
             $response = $client->request('GET', $url);
         }
 
-        $body   = $response->getBody();
+        $body   = $response->getBody()->__toString();
         $result = json_decode($body, true);
 
-        myLog('getDataByInfo', ['url' => $url, 'option' => $option, 'res' => $result]);
+        myLog('push')->info('[VIVO] getDataByInfo', compact('url', 'option', 'authToken', 'result'));
         return $result;
     }
 }

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

@@ -5,6 +5,7 @@ namespace App\Modules\Push\Services;
 
 
 use App\Cache\Lock\LockCache;
+use App\Libs\Push\VPush\VPush;
 use Exception;
 use App\Cache\Push\PushCache;
 use App\Consts\PushConst;
@@ -230,6 +231,14 @@ class PushService
                         $messageId = $client->getMessageId($title, $content, $url);
                         $result    = $client->broadCastAll($messageId);
                         break;
+                    // VIVO
+                    case PushConst::PROVIDER_VIVO:
+                        $client = new VPush($appId, $appKey, $appSecret);
+                        $client->setPushData($title, $content, $url, []);
+                        $client->saveListPayload();
+
+                        $result = $client->sendAll();
+                        break;
                 }
             } catch (Exception $e) {
                 // 最终结果
@@ -292,7 +301,7 @@ class PushService
             QappPushTaskLogs::updateSubTaskStatus($subTaskId, PushConst::STATUS_DOING);
 
             // 循环批量
-            $result = [];
+            $result   = [];
             $regIdArr = array_chunk($regIds, 1000);
             try {
                 switch ($provider) {
@@ -350,6 +359,25 @@ class PushService
                             }
                         }
                         break;
+                    // VIVO
+                    case PushConst::PROVIDER_VIVO:
+                        // 初始化oppo推送
+                        $client = new VPush($appId, $appKey, $appSecret);
+
+                        // 循环推送
+                        $i = 0;
+                        foreach ($regIdArr as $regIdList) {
+                            // 设置相关推送数据,保存消息内容
+                            $client->setPushData($title, $content, $url, $regIdList);
+                            $client->saveListPayload();
+                            $result = $client->sendMessage();
+
+                            // 一般的应用默认消息流控是针对单个应用3000QPS(每秒不能超过3000个token数)
+                            $i++;
+                            if ($i % 3 === 0) {
+                                sleep(1);
+                            }
+                        }
                 }
             } catch (Exception $e) {
                 // 最终结果