增加代付接口
This commit is contained in:
@@ -0,0 +1,254 @@
|
||||
<?php
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
|
||||
class DaiFu
|
||||
{
|
||||
const METHOD_GET = 'GET';
|
||||
const METHOD_POST = 'POST';
|
||||
const UPLOAD_ID_CARD_IMAGE = "UPLOAD_ID_CARD_IMAGE"; //证件照上传接口
|
||||
const QUERY_ID_CARD_IMAGE = "QUERY_ID_CARD_IMAGE"; //证件照处理结果查询接口
|
||||
const SIGN_UP = "SIGN_UP"; //签约接口
|
||||
const QUERY_SIGN_UP = "QUERY_SIGN_UP";//查询签约结果接口
|
||||
private $logDir = 'carHome';
|
||||
protected $ci;
|
||||
|
||||
private $baseUrl;
|
||||
private $clientKey;
|
||||
private $privateKey;
|
||||
private $agentParentId;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->ci = &get_instance();
|
||||
if (!is_product()) { //测试环境
|
||||
$this->baseUrl = 'https://app-uat.zkzs6.com/api/v1';
|
||||
// $this->baseUrl = 'https://api.ss.haodian.cn/plan/callBack/test';
|
||||
$this->clientKey = "808641732177002496";
|
||||
$this->privateKey = "5d8fd442a18da9ae3f39916d6504127k";
|
||||
$this->agentParentId = "655342131023806464";
|
||||
}
|
||||
$this->ci->load->library('myResponse');
|
||||
}
|
||||
|
||||
/**
|
||||
* 公共参数
|
||||
* @param $method
|
||||
* @return array
|
||||
*/
|
||||
public function getCommonData($method)
|
||||
{
|
||||
$formData = [
|
||||
"name" => $method,
|
||||
"client_key" => $this->clientKey,
|
||||
"timestamp" => $this->getNowTime(),
|
||||
];
|
||||
return $formData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 证件上传
|
||||
* @return MyResponse
|
||||
*/
|
||||
public function uploadIdCardImage()
|
||||
{
|
||||
try {
|
||||
$baseData = $this->getCommonData(self::UPLOAD_ID_CARD_IMAGE);
|
||||
$imgUrl = "https://img.liche.cn/liche/2025/09/b6ef0ca29d6f1485/4d49f2821472034e.png";
|
||||
$frontImage = $this->urlToBase64($imgUrl);
|
||||
$data = [
|
||||
'request_id' => time(),
|
||||
'id_card' => '350802199111298215',
|
||||
'real_name' => '林聪聪',
|
||||
'cellphone' => '18350451617',
|
||||
'upload_way' => 'DIRECT', //求方式:DIRECT(直接上传),目前仅支持 DIRECT
|
||||
'front_image' => $frontImage,
|
||||
'back_image' => $frontImage,
|
||||
];
|
||||
$baseData['data'] = $data;
|
||||
$reqData = [
|
||||
'req' => json_encode($baseData, JSON_UNESCAPED_UNICODE)
|
||||
];
|
||||
$reqData['sign'] = $this->sign($reqData['req']);
|
||||
list($responseContent, $statusCode) = $this->httpRequestPost($this->baseUrl, $reqData);
|
||||
if ($statusCode != 200) {
|
||||
throw new Exception("请求失败");
|
||||
}
|
||||
$responseData = json_decode($responseContent, true);
|
||||
if ($responseData['code']) {
|
||||
throw new Exception($responseData['message']);
|
||||
}
|
||||
return new MyResponse(EXIT_SUCCESS, '提交成功', $responseData['data']);
|
||||
} catch (Exception $e) {
|
||||
return new MyResponse(EXIT_ERROR, $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $idCard
|
||||
* @return MyResponse
|
||||
*/
|
||||
public function queryIdCardImage($idCard)
|
||||
{
|
||||
try {
|
||||
$baseData = $this->getCommonData(self::QUERY_ID_CARD_IMAGE);
|
||||
$data = [
|
||||
'request_id' => time(),
|
||||
'id_card' => $idCard
|
||||
];
|
||||
$baseData['data'] = $data;
|
||||
$reqData = [
|
||||
'req' => json_encode($baseData, JSON_UNESCAPED_UNICODE)
|
||||
];
|
||||
$reqData['sign'] = $this->sign($reqData['req']);
|
||||
list($responseContent, $statusCode) = $this->httpRequestPost($this->baseUrl, $reqData);
|
||||
if ($statusCode != 200) {
|
||||
throw new Exception("请求失败");
|
||||
}
|
||||
$responseData = json_decode($responseContent, true);
|
||||
if ($responseData['code']) {
|
||||
throw new Exception($responseData['message']);
|
||||
}
|
||||
return new MyResponse(EXIT_SUCCESS, '提交成功', $responseData['data']);
|
||||
} catch (Exception $e) {
|
||||
return new MyResponse(EXIT_ERROR, $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 签约
|
||||
* @param $requestId
|
||||
* @param $idCard
|
||||
* @param $realName
|
||||
* @param $cellphone
|
||||
* @return MyResponse
|
||||
*/
|
||||
public function signUp($requestId, $idCard, $realName, $cellphone)
|
||||
{
|
||||
try {
|
||||
$baseData = $this->getCommonData(self::SIGN_UP);
|
||||
$data = [
|
||||
'request_id' => $requestId,
|
||||
'id_card' => $idCard,
|
||||
'real_name' => $realName,
|
||||
'cellphone' => $cellphone,
|
||||
'agent_parent_id' => $this->agentParentId
|
||||
];
|
||||
$baseData['data'] = $data;
|
||||
$reqData = [
|
||||
'req' => json_encode($baseData, JSON_UNESCAPED_UNICODE)
|
||||
];
|
||||
$reqData['sign'] = $this->sign($reqData['req']);
|
||||
list($responseContent, $statusCode) = $this->httpRequestPost($this->baseUrl, $reqData);
|
||||
if ($statusCode != 200) {
|
||||
throw new Exception("请求失败");
|
||||
}
|
||||
$responseData = json_decode($responseContent, true);
|
||||
if ($responseData['code']) {
|
||||
throw new Exception($responseData['message']);
|
||||
}
|
||||
return new MyResponse(EXIT_SUCCESS, '提交成功', $responseData['data']);
|
||||
} catch (Exception $e) {
|
||||
return new MyResponse(EXIT_ERROR, $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function querySignUp($requestId, $idCard, $realName, $cellphone)
|
||||
{
|
||||
try {
|
||||
$baseData = $this->getCommonData(self::QUERY_SIGN_UP);
|
||||
$data = [
|
||||
'request_id' => $requestId,
|
||||
'id_card' => $idCard,
|
||||
'real_name' => $realName,
|
||||
'cellphone' => $cellphone,
|
||||
'agent_parent_id' => $this->agentParentId
|
||||
];
|
||||
$baseData['data'] = $data;
|
||||
$reqData = [
|
||||
'req' => json_encode($baseData, JSON_UNESCAPED_UNICODE)
|
||||
];
|
||||
$reqData['sign'] = $this->sign($reqData['req']);
|
||||
list($responseContent, $statusCode) = $this->httpRequestPost($this->baseUrl, $reqData);
|
||||
if ($statusCode != 200) {
|
||||
throw new Exception("请求失败");
|
||||
}
|
||||
$responseData = json_decode($responseContent, true);
|
||||
if ($responseData['code']) {
|
||||
throw new Exception($responseData['message']);
|
||||
}
|
||||
return new MyResponse(EXIT_SUCCESS, '提交成功', $responseData['data']);
|
||||
} catch (Exception $e) {
|
||||
return new MyResponse(EXIT_ERROR, $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private function httpRequestPost($url, $formData = [])
|
||||
{
|
||||
$logPath = "httpRequestPost.log";
|
||||
try {
|
||||
$client = new Client();
|
||||
debug_log("请求地址: {$url}", $logPath, $this->logDir);
|
||||
debug_log("请求参数:" . json_encode($formData, JSON_UNESCAPED_UNICODE), $logPath, $this->logDir);
|
||||
$response = $client->post($url, [
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/x-www-form-urlencoded',
|
||||
],
|
||||
'form_params' => $formData
|
||||
]);
|
||||
$responseContent = $response->getBody()->getContents();
|
||||
debug_log("返回结果:" . $responseContent, $logPath, $this->logDir);
|
||||
return [$responseContent, $response->getStatusCode()];
|
||||
} catch (RequestException $e) {
|
||||
debug_log("", $logPath, $this->logDir);
|
||||
return [$e->getMessage(), 0];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成HMAC签名
|
||||
* @param string $data 待签名的数据
|
||||
* @return string Base64编码的签名结果
|
||||
* @throws Exception 当算法不支持时抛出异常
|
||||
*/
|
||||
function sign($data)
|
||||
{
|
||||
// 计算HMAC哈希值(二进制格式)
|
||||
$hmac = hash_hmac('sha256', $data, $this->privateKey, true);
|
||||
// 转换为Base64编码并返回
|
||||
return base64_encode($hmac);
|
||||
}
|
||||
|
||||
private function getNowTime()
|
||||
{
|
||||
$currentTime = new DateTime();
|
||||
$formattedCurrent = $currentTime->format('Y-m-d\TH:i:s.u');
|
||||
return substr($formattedCurrent, 0, 23);
|
||||
}
|
||||
|
||||
/**
|
||||
* 简化版:网络图片URL转Base64编码
|
||||
* @param string $url 图片URL
|
||||
* @return string|false 转换结果
|
||||
*/
|
||||
public function urlToBase64($url)
|
||||
{
|
||||
// 设置超时时间
|
||||
$context = stream_context_create([
|
||||
'http' => ['timeout' => 10],
|
||||
'ssl' => ['verify_peer' => false] // 跳过SSL验证
|
||||
]);
|
||||
|
||||
// 获取图片内容
|
||||
$imageData = @file_get_contents($url, false, $context);
|
||||
if (!$imageData) return false;
|
||||
|
||||
// 获取MIME类型
|
||||
$finfo = new finfo(FILEINFO_MIME_TYPE);
|
||||
$mime = $finfo->buffer($imageData);
|
||||
|
||||
// 生成Base64字符串
|
||||
return 'data:' . $mime . ';base64,' . base64_encode($imageData);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user