getNoticenterConfig(); $uri = '/api/tg/send/common/text/message'; $options = [ 'query' => [ 'AUTH-TOKEN' => $ntCofnig['token'], ], 'json' => [ 'content' => $content, 'task_name' => $taskName, ], ]; $ret = $this->getHttpClient()->post($uri, $options); $httpStatus = $ret->getStatusCode(); $bodyRaw = $ret->getBody()->getContents(); $result = []; if ($httpStatus == 200) { $arrResp = json_decode($bodyRaw, true); $result['code'] = $arrResp['code']; $result['message'] = $arrResp['message']; $result['data'] = $arrResp['data']; } else { logger('TelegramRepository-sendCommonText', ['http_status' => $httpStatus, 'body_raw' => $bodyRaw]); } return true; } /** * 获取消息中心配置. * @return array * @throws \Psr\SimpleCache\InvalidArgumentException */ public function getNoticenterConfig() { if (empty($this->noticenterConfig)) { $baseUrl = $this->getConfigRepo()->get('tg_msg_server', 'sys', ''); $token = $this->getConfigRepo()->get('tg_api_token', 'sys', ''); $baseUrl = rtrim($baseUrl, '/'); // 干掉最后一个斜杆 $this->noticenterConfig = [ 'base_url' => $baseUrl, 'token' => $token, ]; } return $this->noticenterConfig; } /** * @return \GuzzleHttp\Client|ClientFactory|null */ private function getHttpClient() { if (!$this->httpClient instanceof ClientFactory) { $ntCofnig = $this->getNoticenterConfig(); $baseUrl = rtrim($ntCofnig['base_url'], '/'); $token = $ntCofnig['token']; $options = array( 'base_uri' => $baseUrl, 'timeout' => 15, // 超时,单位秒. 'verify' => false, // 是否校验SSL证书. 'http_errors' => false, // 如果跟随http异常头部是否中断. 'debug' => false, // 是否启用调试模式. 'allow_redirects' => false, // 不跟随301/302跳转. 'headers' => [ 'AUTH-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; } }