_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; } }