#feature: add test mail generator
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
namespace App\Clients;
|
||||
|
||||
use Illuminate\Http\Client\PendingRequest;
|
||||
use Illuminate\Http\Client\Response;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* CRM Service HTTP 客户端
|
||||
*
|
||||
* 用于调用 agent-be 依赖的 CRM 服务接口,主要用于进产诊断中的一级代理账期判断。
|
||||
*/
|
||||
class CrmClient
|
||||
{
|
||||
private string $baseUrl;
|
||||
|
||||
private int $timeout;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->baseUrl = rtrim((string) config('services.crm.base_uri'), '/');
|
||||
$this->timeout = (int) config('services.crm.timeout', 15);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否已配置 CRM 接口
|
||||
*/
|
||||
public function isConfigured(): bool
|
||||
{
|
||||
return $this->baseUrl !== '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一级代理详情,包含 productList[].productCode / agentAccountingPeriod
|
||||
*
|
||||
* 对应 agent-be Client::getAgentByCode -> GET /api/group/detail/{code}
|
||||
*
|
||||
* @return array|null 成功返回响应 JSON 解码数据;失败返回 null。
|
||||
*/
|
||||
public function getAgentByCode(string $rootAgentCode): ?array
|
||||
{
|
||||
if (!$this->isConfigured()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
$response = $this->http()->get($this->baseUrl.'/api/group/detail/'.$rootAgentCode);
|
||||
|
||||
if (!$response->successful()) {
|
||||
Log::warning('CRM getAgentByCode non-2xx', [
|
||||
'code' => $rootAgentCode,
|
||||
'status' => $response->status(),
|
||||
'body' => $response->body(),
|
||||
]);
|
||||
return null;
|
||||
}
|
||||
|
||||
return $response->json();
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('CRM getAgentByCode failed', [
|
||||
'code' => $rootAgentCode,
|
||||
'message' => $e->getMessage(),
|
||||
]);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析 getAgentByCode 返回,构建 productCode => hasCredit 的映射
|
||||
*/
|
||||
public function firstAgentCreditMap(string $rootAgentCode): array
|
||||
{
|
||||
$data = $this->getAgentByCode($rootAgentCode);
|
||||
if (!is_array($data)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// 与 agent-be AgentCredit::getFirstAgentCredit 保持一致:要求 code == 200
|
||||
if (!isset($data['code']) || (int) $data['code'] !== 200) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$productList = $data['data']['productList'] ?? [];
|
||||
$map = [];
|
||||
foreach ($productList as $product) {
|
||||
$productCode = (string) ($product['productCode'] ?? '');
|
||||
if ($productCode === '') {
|
||||
continue;
|
||||
}
|
||||
$map[$productCode] = ((int) ($product['agentAccountingPeriod'] ?? 0)) > 0;
|
||||
}
|
||||
|
||||
return $map;
|
||||
}
|
||||
|
||||
private function http(): PendingRequest
|
||||
{
|
||||
return Http::timeout($this->timeout)
|
||||
->withoutVerifying()
|
||||
->acceptJson();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user