Wang Chen před 4 roky
rodič
revize
efbc6eb093
3 změnil soubory, kde provedl 284 přidání a 1 odebrání
  1. 236 0
      app/Libs/Push/VPush/VPush.php
  2. 30 0
      app/Libs/Utils.php
  3. 18 1
      config/push.php

+ 236 - 0
app/Libs/Push/VPush/VPush.php

@@ -0,0 +1,236 @@
+<?php
+
+
+namespace App\Libs\Push\VPush;
+
+
+use App\Libs\Utils;
+use Exception;
+use GuzzleHttp\Client;
+use GuzzleHttp\Exception\GuzzleException;
+use http\Exception\InvalidArgumentException;
+
+/**
+ * Vivo推送
+ * 服务端接口文档地址:https://dev.vivo.com.cn/documentCenter/doc/362
+ * Class VPush
+ * @package App\Libs\Push\VPush
+ */
+class VPush
+{
+    // App基本配置
+    private $_appId;
+    private $_appKey;
+    private $_appSecret;
+
+    // 授权token
+    private $_accessToken = '';
+
+    // 推送内容及推送用户
+    private $_title = '';
+    private $_content = '';
+    private $_skipContent = '';
+    private $_regId = [];
+
+    // 消息体的id
+    private $_taskId = '';
+
+    // 推送设置
+    private $_notifyType = 4;
+    private $_timeToLive = 86400;
+    private $_skipType = 4;
+    private $_networkType = -1;
+
+    public function __construct($appId, $appKey, $appSecret)
+    {
+        $this->_appId     = $appId;
+        $this->_appKey    = $appKey;
+        $this->_appSecret = $appSecret;
+    }
+
+    /**
+     * 设置推送消息
+     * @param $title
+     * @param $content
+     * @param $url
+     * @param $regId
+     */
+    public function setPushData($title, $content, $url, $regId)
+    {
+        // 赋值
+        $this->_title       = $title;
+        $this->_content     = $content;
+        $this->_skipContent = $url;
+        $this->_regId       = $regId;
+    }
+
+    /**
+     * 保存群推消息公共体接口
+     * @return mixed
+     * @throws GuzzleException
+     * @throws Exception
+     */
+    public function saveListPayload()
+    {
+        // 校验参数
+        $this->checkParam();
+        $data = [
+            'requestId'       => Utils::randCode(),
+            'title'           => $this->_title,
+            'content'         => $this->_content,
+            'notifyType'      => $this->_notifyType,
+            'timeToLive'      => $this->_timeToLive,
+            'skipType'        => $this->_skipType,
+            'skipContent'     => $this->_skipContent,
+            'networkType'     => $this->_networkType,
+            'clientCustomMap' => [],
+        ];
+
+        // 请求服务接口
+        $result        = $this->getData(config('push.vivo.saveListPayload'), $data);
+        $this->_taskId = getProp($result, 'taskId');
+        return $result;
+    }
+
+    /**
+     * 批量推送用户接口
+     * @return mixed
+     * @throws GuzzleException
+     */
+    public function sendMessage()
+    {
+        // 参数判断
+        $this->checkParam();
+        if (!$this->_taskId) {
+            throw new InvalidArgumentException('VIVO推送必须要设置taskId');
+        }
+
+        $data = [
+            'regIds'    => $this->_regId,
+            'taskId'    => $this->_taskId,
+            'requestId' => Utils::randCode(),
+        ];
+
+        return $this->getData(config('push.vivo.pushToList'), $data);
+    }
+
+    /**
+     * @return mixed
+     * @throws GuzzleException
+     */
+    public function sendAll()
+    {
+        // 校验参数
+        $this->checkParam();
+        $data = [
+            'requestId'       => Utils::randCode(),
+            'title'           => $this->_title,
+            'content'         => $this->_content,
+            'notifyType'      => $this->_notifyType,
+            'timeToLive'      => $this->_timeToLive,
+            'skipType'        => $this->_skipType,
+            'skipContent'     => $this->_skipContent,
+            'networkType'     => $this->_networkType,
+            'clientCustomMap' => [],
+        ];
+
+        return $this->getData(config('push.vivo.sendAll'), $data);
+    }
+
+    public function getStatistics()
+    {
+        
+    }
+
+    /**
+     * 校验参数
+     * @return $this
+     * @throws GuzzleException
+     */
+    private function checkParam()
+    {
+        // 授权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->_regId) {
+            throw new InvalidArgumentException('VIVO推送必须要设置regId');
+        }
+        if (!$this->_timeToLive) {
+            throw new InvalidArgumentException('VIVO推送必须要设置timeToLive');
+        }
+        return $this;
+    }
+
+    /**
+     * 授权
+     * @return $this
+     * @throws GuzzleException
+     */
+    private function getAccessToken()
+    {
+        $sendData           = [
+            'appId'     => $this->_appId,
+            'appKey'    => $this->_appKey,
+            'timestamp' => time() . '000',
+        ];
+        $sign               = md5($sendData['appId'] . $sendData['appKey'] . $sendData['timestamp'] . $this->_appSecret);
+        $sendData['sign']   = $sign;
+        $url                = config('push.vivo.auth');
+        $data               = $this->getData($url, $sendData);
+        $this->_accessToken = getProp($data, 'authToken');
+
+        return $this;
+    }
+
+    /**
+     * 请求数据
+     * @param        $url
+     * @param        $sendData
+     * @param string $method
+     * @return mixed
+     * @throws GuzzleException
+     */
+    private function getData($url, $sendData, $method = 'POST')
+    {
+        // 组装请求数据
+        $option = ['json' => $sendData];
+        if ($this->_accessToken) {
+            $option['headers']['auth_token'] = $this->_accessToken;
+        }
+
+        // 请求
+        $client = new Client(['timeout' => 10.0]);
+        if ($method === 'POST') {
+            $response = $client->request('POST', $url, $option);
+        } else {
+            $response = $client->request('GET', $url);
+        }
+
+        $body   = $response->getBody();
+        $result = json_decode($body, true);
+
+        myLog('getDataByInfo', ['url' => $url, 'option' => $option, 'res' => $result]);
+        return $result;
+    }
+}
+

+ 30 - 0
app/Libs/Utils.php

@@ -282,6 +282,36 @@ class Utils
     }
 
     /**
+     * @param int $length
+     * @param int $type
+     * @return string
+     * @throws \Exception
+     */
+    public static function randCode($length = 32, $type = 0): string
+    {
+        $arr = [
+            1 => '0123456789',
+            2 => 'abcdefghijklmnopqrstuvwxyz',
+            3 => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
+            4 => '~@#$%^&*(){}[]|'
+        ];
+        if ($type === 0) {
+            array_pop($arr);
+            $string = implode('', $arr);
+        } elseif ($type === -1) {
+            $string = implode('', $arr);
+        } else {
+            $string = $arr[$type];
+        }
+        $count = strlen($string) - 1;
+        $code  = '';
+        for ($i = 0; $i < $length; $i++) {
+            $code .= $string[random_int(0, $count)];
+        }
+        return $code;
+    }
+
+    /**
      * 将数据推送到rabbitMq
      * 参考:https://www.vckai.com/xiao-xi-dui-lie-rabbitmq-san-phpde-shi-yong-shi-li
      * @param        $messageBody

+ 18 - 1
config/push.php

@@ -28,6 +28,23 @@ return [
             'getInvalidRegIds' => 'https://feedback.push.oppomobile.com/server/v1/feedback/fetch_invalid_regidList',
             'fetchPushPermit'  => 'https://feedback.push.oppomobile.com/server/v1/feedback/fetch_push_permit',
         ],
-        'vivo'   => []
+        'vivo'   => [
+            'auth'             => 'https://api-push.vivo.com.cn/message/auth',
+            'sendMessage'      => 'https://api-push.vivo.com.cn/message/send',
+            'saveListPayload'  => 'https://api-push.vivo.com.cn/message/saveListPayload',
+            'pushToList'       => 'https://api-push.vivo.com.cn/message/pushToList',
+            'sendAll'          => 'https://api-push.vivo.com.cn/message/all',
+            'getStatistics'    => 'https://api-push.vivo.com.cn/report/getStatistics',
+            'addTag'           => 'https://api-push.vivo.com.cn/tag/add',
+            'updateTag'        => 'https://api-push.vivo.com.cn/tag/update',
+            'addMembers'       => 'https://api-push.vivo.com.cn/tag/addMembers',
+            'removeMembers'    => 'https://api-push.vivo.com.cn/tag/removeMembers',
+            'tagPush'          => 'https://api-push.vivo.com.cn/message/tagPush',
+            'msgStatus'        => 'https://api-push.vivo.com.cn/search/msgStatus',
+            'checkInvalidUser' => 'https://api-push.vivo.com.cn/invalidUser/check',
+            'authV3'           => 'https://api-push.vivo.com.cn/v3/message/auth',
+            'sendMessageV3'    => 'https://api-push.vivo.com.cn/v3/message/send',
+            'getAppConfig'     => 'https://api-push.vivo.com.cn/report/getAppConfig',
+        ]
     ]
 ];