Files
2024-06-13 14:07:19 +08:00

142 lines
5.9 KiB
PHP

<?php
defined('BASEPATH') or exit('No direct script access allowed');
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
class CarHome
{
const METHOD_GET = 'GET';
const METHOD_POST = 'POST';
const SAVE_ORDER = 'outOrder/saveOrderInfo';
private $base_url = 'https://apiahoh.autohome.com.cn/';
private $key = 'ahoh-crm-apiA0EoJwyA';
private $secret = 'lDMDpzflU3gj';
private $storeId = 3479; //之家空间站ID
private $outSystemCode = 1001;//外部系统编号 1001 爱能
protected $city_lists = [
430100, 430200, 430300, 430400, 430500,
430600, 430700, 430800, 430900, 431000,
431100, 431200, 431300, 433100
];
protected $other_city = 439900;//439900 其它城市
private $ci;
public function __construct()
{
$this->ci = &get_instance();
$this->client = new Client();
if (!is_product()) { //测试环境
$this->base_url = 'https://ahohcrm-test.autohome.com.cn/api/community/';
$this->storeId = 50;
}
$this->ci->load->model('auto/auto_api_log_model');
}
//autohomeOrderId Long 否 之家订单id 更新时必传
//outSystemCode int 是 外部系统编号 1001 爱能
//buyerType int 是 购⻋主体 1个⼈ 2公户
//buyerName String 是 客户姓名 不超过50个字符
//buyerPhone String 是 客户⼿机号
//specId int 是 之家⻋型ID 之家提供⻋型数据
//storeId int 是 之家空间站ID 之家提供空间站与⼆⽹城市对应关系
//outOrderId String 是 外部订单号
//outLeadId String 是 外部线索id
//outOrderCreateTime Date 是 订单创建时间 yyyy-MM-dd HH:mm:ss
//registerCityId int 是 城市ID 需确认是否统⼀
//invoiceAmount double 否 开票价格
//isRegisterInStore int 否 是否店内上牌 (0否1是)
//registerAmount double 否 上牌费⽤
//isInsured int 否 是否店内投保商业险 (0否1是)
//isLoan int 否 是否贷款 (0否1是)
//loanAmount double 否 贷款额度
//loanPeriods int 否 贷款期数
//monthlyPayment double 否 ⽉供
//carryCarDate Date 否 提⻋⽇期 yyyy-MM-dd
//payDate Date 否 付款⽇期 yyyy-MM-dd
//contractImg String 否 合同凭证 图⽚URL
//invoiceImg String 否 发票凭证 图⽚URL
//payImg String 否 付款凭证 图⽚URL
//outColor String 否 ⻋身颜⾊
//inColor String 否 内饰颜⾊
//deliveryDate Date 否 期望交付时间 yyyy-MM-dd
//downpaymentType int 否 ⾸付类型 1 现⾦ 2 0⾸付 3按揭
//confirmAmount double 否 定⾦
//discountAmount double 否 ⻋身优惠
//isLocalInvoice int 否 需要开具本地发票 1 是 0 否
//idCardNo String 否 身份证/营业执照号
//carDocumentImg String 否 ⻋辆证件 图⽚URL
//insuranceImg String 否 保险单 图⽚URL
//deliveryPhoto String 否 交⻋合照 图⽚URL
//priceRightsConfirmDoc String 否 买贵必赔权益确认书⽚ 图⽚URL
//返回结果 {"returncode":0,"message":null,"result":{“autohomeOrderId”:3162}}
/**
* 提交订单信息
* @param $data
* @return array|void
* @throws GuzzleException
*/
public function saveOrder($data, $oid)
{
try {
$post_url = $this->base_url . self::SAVE_ORDER;
$data['outSystemCode'] = $this->outSystemCode;
$data['storeId'] = $this->storeId;
//城市转换
$data['registerCityId'] = $this->city_lists[$data['registerCityId']] ?: $this->other_city;
debug_log("post_data:" . json_encode($data, JSON_UNESCAPED_UNICODE));
list($req, $header, $httpStatus) = $this->send($post_url, $data, self::METHOD_POST);
debug_log("result:" . $req);
$reqArr = json_decode($req, true);
$log_data = [
'order_id' => $oid,
'url' => $post_url,
'header' => json_encode($header, JSON_UNESCAPED_UNICODE),
'post_data' => json_encode($data, JSON_UNESCAPED_UNICODE),
'res_http_status' => $httpStatus,
'c_time' => time()
];
$req && $log_data['res'] = $req;
$this->ci->auto_api_log_model->add($log_data);
if (isset($reqArr['returncode']) && intval($reqArr['returncode']) == 0) {
return ['code' => 1, 'msg' => '操作成功', 'data' => $reqArr['result']];
} else {
return ['code' => 0, 'msg' => $reqArr['message']];
}
} catch (Exception $e) {
return ['code' => 0, 'msg' => $e->getMessage()];
}
}
/**
* 参数
* @return string
*/
public function authorization()
{
$res = "Basic " . base64_encode($this->key . ':' . $this->secret);
return $res;
}
public function send($url, $data = [], $type = self::METHOD_GET)
{
try {
$options = [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => $this->authorization()
],
];
if ($data) {
$options['json'] = $data;
}
$response = $this->client->request($type, $url, $options);
return [$response->getBody()->getContents(), $options, $response->getStatusCode()];
} catch (GuzzleException $e) {
debug_log($e->getMessage() . ' URL: ' . $url . ' Data: ' . json_encode($data));
throw $e;
}
}
}