167 lines
5.0 KiB
PHP
167 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace Stemcell\Customer\Helper;
|
|
|
|
use Magento\Framework\App\Helper\Context;
|
|
use Magento\Framework\Exception\LocalizedException;
|
|
use Stemcell\Customer\Api\Data\AvatrInterface;
|
|
use Stemcell\Customer\Helper\Avatr\Transformers;
|
|
|
|
class Avatr extends \Magento\Framework\App\Helper\AbstractHelper implements CarSeriesClueInterface
|
|
{
|
|
protected array $config = [
|
|
'media_id' => 6110,
|
|
'media_name' => 'SHANGHAILIQI',
|
|
'ak' => 6110,
|
|
'sk' => 'OvsbfYqDXcqr',
|
|
'aes_key' => 'oEr4HdIIouvveWL0Jjk74tA8Cf3fkaqQ',
|
|
'aes_iv' => 'WUPXlZGkfu7yo4gi',
|
|
'host' => 'https://sl-api-amstp.avatr.com'
|
|
];
|
|
|
|
protected int $timestamp = 0;
|
|
|
|
protected array $requestData = [];
|
|
private \Magento\Framework\HTTP\Client\Curl $curl;
|
|
|
|
private \Stemcell\Customer\Model\Avatr $avatr;
|
|
private Transformers $transformers;
|
|
|
|
private string $clueCreateEndpoint = '/api/media/data/save/v2';
|
|
|
|
private string $response;
|
|
|
|
public function __construct(
|
|
Context $context,
|
|
\Magento\Framework\HTTP\Client\Curl $curl,
|
|
Transformers $transformers
|
|
) {
|
|
parent::__construct($context);
|
|
$this->curl = $curl;
|
|
$this->transformers = $transformers;
|
|
}
|
|
|
|
public function setConfig(array $config = []): static
|
|
{
|
|
$this->config = array_merge($this->config, $config);
|
|
return $this;
|
|
}
|
|
|
|
public function setClue(AvatrInterface $clue):self {
|
|
$this->avatr = $clue;
|
|
return $this;
|
|
}
|
|
|
|
public function setRequestData(array $requestData): static
|
|
{
|
|
$this->requestData = $requestData;
|
|
return $this;
|
|
}
|
|
public function getRequestData():array
|
|
{
|
|
if (!empty($this->requestData)) {
|
|
return $this->requestData;
|
|
}
|
|
$leadsCreateTime = $this->avatr->getCreatedAt();
|
|
if (!$leadsCreateTime) {
|
|
$leadsCreateTime = date("Y-m-d H:i:s", $this->getTimestamp());
|
|
}
|
|
return $this->requestData = [
|
|
'mediaId' => $this->config['media_id'],
|
|
'mediaName' => $this->config['media_name'],
|
|
'clueId' => 'shlq-'.str_pad($this->avatr->getId(), 12, '0', STR_PAD_LEFT),
|
|
'name' => $this->avatr->getName(),
|
|
'leadsCreateTime' => $leadsCreateTime,
|
|
'phone' => $this->getEncryptedPhone($this->avatr->getTelephone()),
|
|
'province' => $this->avatr->getProvinceId().'',
|
|
'city' => $this->avatr->getCityId().'',
|
|
'carStyle' => $this->avatr->getFavoriteVehicle(),
|
|
'campaignId' => '202402_CRP_SHANGHAILIQI_S1',
|
|
'campaignName' => '202402_线索资源池_上海力企_启动期'
|
|
];
|
|
}
|
|
|
|
protected function getEncryptedPhone(string $content, $cipher = 'AES-256-CBC'):string {
|
|
return base64_encode(
|
|
openssl_encrypt(
|
|
$content,
|
|
$cipher,
|
|
$this->config['aes_key'],
|
|
OPENSSL_RAW_DATA,
|
|
$this->config['aes_iv']
|
|
)
|
|
);
|
|
}
|
|
|
|
protected function getTimestamp(): int
|
|
{
|
|
if ($this->timestamp !== 0) {
|
|
return $this->timestamp;
|
|
}
|
|
|
|
return $this->timestamp = time();
|
|
}
|
|
|
|
protected function sign(
|
|
string $uri = '/api/media/data/save/v2',
|
|
string $algo = 'sha1',
|
|
string $httpMethod = 'POST',
|
|
string $contentType = 'application/json',
|
|
): string
|
|
{
|
|
$data = $this->getRequestData();
|
|
ksort($data);
|
|
|
|
$signString = sprintf(
|
|
'%s %s %s %s %s',
|
|
$httpMethod,
|
|
$uri,
|
|
$contentType,
|
|
$this->getTimestamp(),
|
|
str_replace('\\/', '/', json_encode($data, JSON_UNESCAPED_UNICODE))
|
|
);
|
|
// echo '签名前数据: '.$signString.'<br/><br/>';
|
|
return base64_encode(hash_hmac($algo, $signString, $this->config['sk'], true));
|
|
}
|
|
|
|
public function post() : self {
|
|
$body = json_encode($this->getRequestData());
|
|
|
|
$this->_logger->debug('config: ', $this->config);
|
|
$authorization = sprintf('AVATR %s:%s', $this->config['ak'], $this->sign($this->clueCreateEndpoint));
|
|
|
|
$this->curl->addHeader("Content-Type", "application/json");
|
|
$this->curl->addHeader('Timestamp', $this->getTimestamp());
|
|
$this->curl->addHeader('Authorization', $authorization);
|
|
$this->curl->post( $this->config['host'].$this->clueCreateEndpoint, $body);
|
|
|
|
$this->response = $this->curl->getBody();
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @throws LocalizedException
|
|
*/
|
|
public function transform(array $from): array
|
|
{
|
|
return $this->transformers->transform($from);
|
|
}
|
|
|
|
public function getResponse() : array
|
|
{
|
|
$response = json_decode($this->response, true);
|
|
return is_array($response)? $response:[];
|
|
}
|
|
public function isSuccess(): bool
|
|
{
|
|
$response = $this->getResponse();
|
|
return isset($response['success']) && $response['success']==true;
|
|
}
|
|
|
|
public function reset(): self
|
|
{
|
|
$this->requestData = [];
|
|
return $this;
|
|
}
|
|
}
|