118 lines
3.6 KiB
PHP
118 lines
3.6 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace app\miniapi\service\CustomerCenter;
|
|
|
|
use api\Httpcurl;
|
|
use think\facade\Log;
|
|
|
|
final class CustomerCenterService
|
|
{
|
|
public function applicants(int $userId): array
|
|
{
|
|
return $this->request('applicants', $userId);
|
|
}
|
|
|
|
public function applications(int $userId, int $page, int $pageSize): array
|
|
{
|
|
return $this->request('applications', $userId, [
|
|
'page' => max(1, $page),
|
|
'page_size' => max(1, min(50, $pageSize)),
|
|
]);
|
|
}
|
|
|
|
public function orders(int $userId, string $payStatus, int $page, int $pageSize): array
|
|
{
|
|
return $this->request('orders', $userId, [
|
|
'pay_status' => $payStatus,
|
|
'page' => max(1, $page),
|
|
'page_size' => max(1, min(50, $pageSize)),
|
|
'business_types' => 'evus,esta',
|
|
]);
|
|
}
|
|
|
|
public function applicant(int $userId, int $applicantId): array
|
|
{
|
|
return $this->request('applicant', $userId, ['applicant_id' => $applicantId]);
|
|
}
|
|
|
|
public function saveApplicant(int $userId, array $params): array
|
|
{
|
|
return $this->request('saveApplicant', $userId, $params);
|
|
}
|
|
|
|
public function removeApplicant(int $userId, int $applicantId): array
|
|
{
|
|
return $this->request('removeApplicant', $userId, ['applicant_id' => $applicantId]);
|
|
}
|
|
|
|
public function syncEvusApplicant(
|
|
int $userId,
|
|
int $applicantId,
|
|
string $orderSn,
|
|
array $profile
|
|
): array {
|
|
return $this->request('syncEvusApplicant', $userId, [
|
|
'applicant_id' => $applicantId,
|
|
'order_sn' => $orderSn,
|
|
'profile_json' => json_encode(
|
|
$profile,
|
|
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_SUBSTITUTE
|
|
),
|
|
], 5);
|
|
}
|
|
|
|
public function payload(int $userId, array $params = []): array
|
|
{
|
|
return ['user_id' => $userId] + $params;
|
|
}
|
|
|
|
public function endpoint(string $userUrl, string $action): string
|
|
{
|
|
if (!in_array($action, [
|
|
'applicants',
|
|
'applications',
|
|
'orders',
|
|
'applicant',
|
|
'saveApplicant',
|
|
'removeApplicant',
|
|
'syncEvusApplicant',
|
|
], true)) {
|
|
return '';
|
|
}
|
|
|
|
$userUrl = rtrim(trim($userUrl), '/');
|
|
|
|
$endpoint = preg_replace(
|
|
'#/users/[^/?]+(?:\?.*)?$#i',
|
|
'/customer_center/' . $action,
|
|
$userUrl
|
|
);
|
|
|
|
return is_string($endpoint) && $endpoint !== $userUrl ? $endpoint : '';
|
|
}
|
|
|
|
private function request(string $action, int $userId, array $params = [], int $timeout = 0): array
|
|
{
|
|
$url = $this->endpoint(trim((string)config('app.user_url')), $action);
|
|
if ($url === '') {
|
|
return ['code' => 0, 'msg' => '客户中心接口地址异常', 'data' => []];
|
|
}
|
|
|
|
$payload = $this->payload($userId, $params);
|
|
$response = Httpcurl::request($url, 'post', $payload, [], $timeout);
|
|
if (!is_array($response) || !isset($response[0]) || !empty($response[3])) {
|
|
Log::error('miniapi customer center ' . $action . ' request failed');
|
|
return ['code' => 0, 'msg' => '服务异常,请稍后重试', 'data' => []];
|
|
}
|
|
|
|
$result = json_decode($response[0], true);
|
|
if (!is_array($result)) {
|
|
Log::error('miniapi customer center ' . $action . ' response decode failed');
|
|
return ['code' => 0, 'msg' => '服务异常,请稍后重试', 'data' => []];
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
}
|