'https://lead-vma.lynkco.com.cn', 'app_key' => '204033053', 'app_secret' => 'pflQoSYIPSXMnv3hancZtClkP3ftBbqn', 'activityCode' => 'GGTF20240726002', 'leadSourceCode' => 'GGTF0002' ]; private array $requestData = []; /** * RSA public key * @var string */ private string $pubKey = ''; private \Magento\Framework\HTTP\Client\Curl $curl; private string $response; private AvatrInterface $clue; private Transformers $transformers; /** * 经销商编码 * @var string */ private string $distributorCode; /** * 达人 * @var string */ private string $remark; public function __construct( Context $context, \Magento\Framework\HTTP\Client\Curl $curl, Transformers $transformers ) { parent::__construct($context); $this->curl = $curl; $this->transformers = $transformers; } /** * @throws LocalizedException */ public function getHost(): string { return $this->_getData('host'); } public function setConfig(array $config = []): static { $this->config = array_merge($this->config, $config); return $this; } /** * @throws LocalizedException */ function _getData(string $key): string { if (isset($this->config[$key]) && !empty($this->config[$key])) { return $this->config[$key]; } throw new LocalizedException(__($key.' is not set')); } /** * @throws LocalizedException */ function getAppSecret(): string { return $this->_getData('app_secret'); } /** * @throws LocalizedException */ function getAppKey(): string { return $this->_getData('app_key'); } /** * data must contains follow data: * customerPhoneNo: 客户电话 * outwardOriginId: 线索生成系统的原始id * city: 客户城市编码(国标) https://github.com/modood/Administrative-divisions-of-China/tree/master/dist * * @param array $data * @return $this */ public function setRequestData(array $data): static { $this->requestData = array_merge([ 'activityCode' => 'GGTF20240726002', 'leadSourceCode' => 'GGTF0002', ], $data); return $this; } /** * @return array */ public function getRequestData(): array { if (!empty($this->requestData)) { return $this->requestData; } $this->requestData = [ 'activityCode' => 'GGTF20240726002', 'leadSourceCode' => 'GGTF0002', 'mobile' => $this->clue->getTelephone(), 'name' => $this->clue->getName(), 'seriesCode' => $this->clue->getFavoriteVehicle(), 'dealercode' => $this->distributorCode, ]; if ($this->remark) { $this->requestData['remark'] = $this->remark; } return $this->requestData; } /** * @throws LocalizedException * @throws GuzzleException */ public function post(): self { $signer = new RequestSigner(new Key($this->getAppKey(), $this->getAppSecret())); $middleware = new RequestSignerMiddleware($signer); $stack = HandlerStack::create(); $stack->push($middleware); $client = new Client([ 'handler' => $stack, 'base_uri'=> $this->getHost(), 'headers' => [ 'content-Type' => 'application/json' , 'accept' => 'application/json', ] ]); $response = $client->post( '/api/v1/lead/mediaLeadInput', [\GuzzleHttp\RequestOptions::JSON => $this->getRequestData()] ); $this->response = $response->getBody()->getContents(); return $this; } /** * @throws LocalizedException */ public function transform(array $from): array { $this->distributorCode = trim($from[3]); // $this->remark = mb_convert_encoding($from[4], 'UTF-8', 'gb2312'); $this->remark = $from[4]; return $this->transformers->transform($from); } /** * @param AvatrInterface $clue * @return CarSeriesClueInterface */ public function setClue(AvatrInterface $clue): CarSeriesClueInterface { $this->clue = $clue; return $this; } /** * @return bool */ public function isSuccess(): bool { $response = $this->getResponse(); return isset($response['success']) && $response['success']==true; } /** * @return array */ public function getResponse(): array { $response = json_decode($this->response, true); return is_array($response)? $response:[]; } /** * @return CarSeriesClueInterface */ public function reset(): CarSeriesClueInterface { $this->requestData = []; return $this; } }