Files
spacestation/common/libraries/carHome/DaiFu.php
T
2025-11-06 22:53:55 +08:00

281 lines
10 KiB
PHP

<?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 = 'https://app.zkzs6.com/api/v1';
private $clientKey = '808641732177002496';
private $privateKey = '5d8fd442a18da9ae3f39916d6504127k';
private $agentParentId = '655342131023806464';
public function __construct()
{
$this->ci = &get_instance();
if (!is_product()) { //测试环境
$this->baseUrl = 'https://app-uat.zkzs6.com/api/v1';
$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($requestId, $frontImageUrl, $backImageUrl, $idCard, $realName, $cellPhone,
$bankName = '', $bankCard = '', $autoSign = 1)
{
try {
$baseData = $this->getCommonData(self::UPLOAD_ID_CARD_IMAGE);
$frontImgData = $this->urlToBase64($frontImageUrl);
if (!$frontImgData->isSuccess() || !$frontImgData->getData()['data']) {
throw new Exception("获取身份证正面图片失败");
}
$backImgData = $this->urlToBase64($backImageUrl);
if (!$backImgData->isSuccess() || !$backImgData->getData()['data']) {
throw new Exception("获取身份证反面图片失败");
}
$frontImage = $frontImgData->getData()['data'];
$backImage = $backImgData->getData()['data'];
$data = [
'request_id' => $requestId,
'id_card' => $idCard,
'real_name' => $realName,
'cellphone' => $cellPhone,
'upload_way' => 'DIRECT', //求方式:DIRECT(直接上传),目前仅支持 DIRECT
'front_image' => $frontImage,
'back_image' => $backImage,
'auto_sign' => $autoSign
];
$bankName && $data['bank_name'] = $bankName;
$bankCard && $data['bank_card'] = $bankCard;
$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
* @return MyResponse
*/
public function queryIdCardImage($requestId, $idCard)
{
try {
$baseData = $this->getCommonData(self::QUERY_ID_CARD_IMAGE);
$data = [
'request_id' => $requestId,
'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());
}
}
/**
* 查询签约结果
* @param $requestId
* @param $idCard
* @param $realName
* @param $cellphone
* @return MyResponse
*/
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 MyResponse
*/
public function urlToBase64($url)
{
try {
// 设置超时时间
$context = stream_context_create([
'http' => ['timeout' => 10],
'ssl' => ['verify_peer' => false] // 跳过SSL验证
]);
// 获取图片内容
$imageData = @file_get_contents($url, false, $context);
if (!$imageData) {
throw new Exception("无法获取图片内容");
}
// 获取MIME类型
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime = $finfo->buffer($imageData);
$responseData['data'] = 'data:' . $mime . ';base64,' . base64_encode($imageData);
return new MyResponse(EXIT_SUCCESS, '操作成功', $responseData);
} catch (Exception $e) {
return new MyResponse(EXIT_ERROR, $e->getMessage());
}
}
}