|
|
<?php
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
|
use App\Exceptions\YuanhePackInfoException;
|
|
|
use App\Models\AppointmentConfig;
|
|
|
use App\Models\Course;
|
|
|
use App\Models\ThirdAppointmentLog;
|
|
|
use App\Models\User;
|
|
|
|
|
|
/**
|
|
|
* 元禾控股
|
|
|
*/
|
|
|
class YuanheRepository
|
|
|
{
|
|
|
public $baseUrl;
|
|
|
public $customerId;
|
|
|
public $authKey;
|
|
|
public $lastRequestTimestamp;
|
|
|
public $lastRequest;
|
|
|
|
|
|
public function __construct()
|
|
|
{
|
|
|
$this->baseUrl = env('YUANHE_BASE_URL');
|
|
|
$this->customerId = env('YUANHE_CUSTOMERID');
|
|
|
$this->authKey = env('YUANHE_AUTHKEY');
|
|
|
}
|
|
|
|
|
|
|
|
|
public function getHeader()
|
|
|
{
|
|
|
$timestamp = (string) floor(microtime(true) * 1000);
|
|
|
$this->lastRequestTimestamp = $timestamp;
|
|
|
$token = $this->customerId . $timestamp . $this->authKey;
|
|
|
$token = md5($token);
|
|
|
$token = strtoupper($token);
|
|
|
|
|
|
$header[] = 'Content-Type: application/json';
|
|
|
$header[] = "customerId: {$this->customerId}";
|
|
|
$header[] = "timestamp: {$timestamp}";
|
|
|
$header[] = "token: {$token}";
|
|
|
return $header;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 公司查询
|
|
|
*/
|
|
|
public function companyInfo($params)
|
|
|
{
|
|
|
$params = json_encode($params);
|
|
|
$url = $this->baseUrl . '/master-service/openapi/businessCollege/enterprise/info';
|
|
|
$header = $this->getHeader();
|
|
|
try {
|
|
|
$result = httpCurl($url, 'POST', $params, $header);
|
|
|
$result = json_decode($result, true);
|
|
|
if ($result['code'] == 200) {
|
|
|
return $result['data'];
|
|
|
} else {
|
|
|
return false;
|
|
|
}
|
|
|
} catch (\Exception $e) {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 公司模糊查询
|
|
|
*/
|
|
|
public function search($params)
|
|
|
{
|
|
|
$url = $this->baseUrl . '/master-service/openapi/businessCollege/enterprise/search';
|
|
|
$header = $this->getHeader();
|
|
|
try {
|
|
|
$result = httpCurl($url, 'GET', $params, $header);
|
|
|
$result = json_decode($result, true);
|
|
|
if ($result['code'] == 200) {
|
|
|
return $result['data'];
|
|
|
} else {
|
|
|
return [];
|
|
|
}
|
|
|
} catch (\Exception $e) {
|
|
|
return [];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 诊断用:复用小程序企业搜索的请求方式,但保留元禾完整响应。
|
|
|
*/
|
|
|
public function enterpriseSearchProbe(array $params): array
|
|
|
{
|
|
|
$url = $this->baseUrl . '/master-service/openapi/businessCollege/enterprise/search';
|
|
|
$headers = $this->getHeader();
|
|
|
$this->lastRequest = [
|
|
|
'method' => 'GET',
|
|
|
'url' => $url,
|
|
|
'query' => $params,
|
|
|
'headers' => $this->headersToMap($headers),
|
|
|
];
|
|
|
$rawResponse = httpCurl($url, 'GET', $params, $headers);
|
|
|
$response = json_decode($rawResponse, true);
|
|
|
|
|
|
return [
|
|
|
'raw_response' => $rawResponse,
|
|
|
'response' => is_array($response) ? $response : null,
|
|
|
'json_error' => is_array($response) ? null : json_last_error_msg(),
|
|
|
];
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 查询企业户聚合信息。
|
|
|
*
|
|
|
* 此方法保留元禾响应外层的 code/data/msg/status,供调用方判定一次
|
|
|
* 企业户调用是否明确成功;不要像 companyInfo 一样仅返回 data。
|
|
|
*/
|
|
|
public function enterprisePackInfo(array $params): array
|
|
|
{
|
|
|
$url = $this->baseUrl . '/master-service/openapi/businessCollege/enterprise/packInfo';
|
|
|
$header = $this->getHeader();
|
|
|
$payload = json_encode($params, JSON_UNESCAPED_UNICODE);
|
|
|
$this->lastRequest = [
|
|
|
'method' => 'POST',
|
|
|
'url' => $url,
|
|
|
'json' => $params,
|
|
|
'headers' => $this->headersToMap($header),
|
|
|
];
|
|
|
|
|
|
$ch = curl_init();
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
|
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
|
|
|
if (stripos($url, 'https://') !== false) {
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
|
|
curl_setopt($ch, CURLOPT_SSLVERSION, 1);
|
|
|
}
|
|
|
|
|
|
$raw = curl_exec($ch);
|
|
|
$curlError = curl_error($ch);
|
|
|
$httpStatus = (int) curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
|
|
|
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE) ?: null;
|
|
|
curl_close($ch);
|
|
|
|
|
|
if ($raw === false) {
|
|
|
throw new YuanhePackInfoException(
|
|
|
'元禾 packInfo 网络调用失败:' . ($curlError ?: '未知 cURL 错误'),
|
|
|
$httpStatus ?: null,
|
|
|
$contentType
|
|
|
);
|
|
|
}
|
|
|
|
|
|
$result = json_decode($raw, true);
|
|
|
if (!is_array($result)) {
|
|
|
throw new YuanhePackInfoException(
|
|
|
'元禾 packInfo 响应不是有效 JSON 对象:' . json_last_error_msg(),
|
|
|
$httpStatus ?: null,
|
|
|
$contentType,
|
|
|
$raw
|
|
|
);
|
|
|
}
|
|
|
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
|
protected function headersToMap(array $headers): array
|
|
|
{
|
|
|
$mapped = [];
|
|
|
foreach ($headers as $header) {
|
|
|
[$name, $value] = array_pad(explode(':', $header, 2), 2, '');
|
|
|
$mapped[trim($name)] = trim($value);
|
|
|
}
|
|
|
|
|
|
return $mapped;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 数据推送
|
|
|
*/
|
|
|
public function pushCourses(Course $course, User $user,&$out)
|
|
|
{
|
|
|
if (empty($user->company)) {
|
|
|
return false;
|
|
|
}
|
|
|
$params = [
|
|
|
'classTeacher' => $course->teacher->name??'',
|
|
|
'courseName' => $course->name,
|
|
|
'description' => $user->company->businessScope??'',
|
|
|
'enterpriseName' => $user->company->company_name,
|
|
|
'creditCode' => $user->company->credit_code,
|
|
|
'groupId' => '1030004',
|
|
|
'openTime' => $course->start_date
|
|
|
];
|
|
|
$params = json_encode($params, JSON_UNESCAPED_UNICODE);
|
|
|
$url = $this->baseUrl . '/master-service/openapi/businessCollege/shareInfo/push';
|
|
|
$header = $this->getHeader();
|
|
|
try {
|
|
|
$result = httpCurl($url, 'POST', $params, $header);
|
|
|
$result = json_decode($result, true);
|
|
|
if ($result['code'] == 200) {
|
|
|
return true;
|
|
|
} else {
|
|
|
$out = $result['msg'];
|
|
|
return false;
|
|
|
}
|
|
|
} catch (\Exception $e) {
|
|
|
$out = $e->getMessage();
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|