getCacheRepo()->get($cacheKey, []); if (empty($data) || $isForce === true) { $arr = $this->isHashExistsWithoutCache($fileHash); if (isset($arr['data']['is_exists']) && $arr['data']['is_exists'] === true) { $data = $arr['data']; $this->getCacheRepo()->set($cacheKey, $data, $cacheTime); } } return $data; } /** * 是否存在相同hash, 存在返回数据.. * @param string $fileHash * @return array * @throws \GuzzleHttp\Exception\GuzzleException */ public function isHashExistsWithoutCache(string $fileHash) { $result = []; $config = $this->getConfig(); $uri = '/api/ppvod/is/hash/exists'; $postData = [ 'file_hash' => $fileHash, ]; $options = [ 'query' => [ 'token' => $config['token'], ], 'json' => $postData, ]; $ret = $this->getHttpClient()->post($uri, $options); $httpStatus = $ret->getStatusCode(); $bodyRaw = $ret->getBody()->getContents(); if ($httpStatus == 200) { $arrResp = json_decode($bodyRaw, true); $result['code'] = $arrResp['code']; $result['message'] = $arrResp['message']; $result['data'] = $arrResp['data']; } else { $result['code'] = 500; $result['message'] = 'http_status:'. strval($httpStatus); $result['data'] = []; logger('MediaCenterRepository-isHashExistsWithoutCache', ['http_status' => $httpStatus, 'body_raw' => $bodyRaw]); } return $result; } /** * 同步视频到媒资库. * @param $postData * @return array * @throws \GuzzleHttp\Exception\GuzzleException * @throws \Psr\SimpleCache\InvalidArgumentException */ public function syncAppVideoToMediaCenter($postData) { $result = []; $config = $this->getConfig(); $uri = '/api/app/sync/video'; $options = [ 'query' => [ 'token' => $config['token'], ], 'json' => $postData, ]; $ret = $this->getHttpClient()->post($uri, $options); $httpStatus = $ret->getStatusCode(); $bodyRaw = $ret->getBody()->getContents(); if ($httpStatus == 200) { $arrResp = json_decode($bodyRaw, true); $result['code'] = $arrResp['code']; $result['message'] = $arrResp['message']; $result['data'] = $arrResp['data']; } else { $result['code'] = 500; $result['message'] = 'http_status:'. strval($httpStatus); $result['data'] = []; logger('MediaCenterRepository-syncVideo', ['http_status' => $httpStatus, 'body_raw' => $bodyRaw]); } return $result; } /** * 获取配置. * @return array * @throws \Psr\SimpleCache\InvalidArgumentException */ public function getConfig() { if (empty($this->config)) { $baseUrl = $this->getConfigRepo()->get('media_center_base_url', 'sys', ''); $token = $this->getConfigRepo()->get('media_center_token', 'sys', ''); $baseUrl = rtrim($baseUrl, '/'); // 干掉最后一个斜杆 $this->config = [ 'base_url' => $baseUrl, 'token' => $token, ]; } return $this->config; } /** * @return \GuzzleHttp\Client|ClientFactory|null */ private function getHttpClient() { if (!$this->httpClient instanceof ClientFactory) { $config = $this->getConfig(); $baseUrl = rtrim($config['base_url'], '/'); $token = $config['token']; $options = array( 'base_uri' => $baseUrl, 'timeout' => 3, // 超时,单位秒. 'verify' => false, // 是否校验SSL证书. 'http_errors' => false, // 如果跟随http异常头部是否中断. 'debug' => false, // 是否启用调试模式. 'allow_redirects' => false, // 不跟随301/302跳转. 'headers' => [ 'X-TOKEN' => $token, ], ); $this->httpClient = make(ClientFactory::class)->create($options); } return $this->httpClient; } /** * @return ConfigRepoistory|mixed */ private function getConfigRepo() { if (!$this->configRepo instanceof ConfigRepoistory) { $this->configRepo = make(ConfigRepoistory::class); } return $this->configRepo; } /** * @return mixed|CacheInterface|null */ private function getCacheRepo() { if (!$this->cacheRepo instanceof CacheInterface) { $this->cacheRepo = make(CacheInterface::class); } return $this->cacheRepo; } }