You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
3.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Console\Commands;
use App\Exceptions\YuanhePackInfoException;
use App\Models\Company;
use App\Models\CompanyQccAccount;
use App\Repositories\YuanheRepository;
use Illuminate\Console\Command;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;
class VerifyQccEnterprisePackInfo extends Command
{
protected $signature = 'qcc:verify-pack-info
{company_id : 候选、已占额或待人工确认企业的本地企业 ID}
{--timeout=60 : 请求超时时间(秒,正整数)}
{--request : 额外输出本次请求地址、请求头和请求体(不含 authKey)}
{--raw : 兼容旧用法,默认即输出完整响应}';
protected $description = '诊断元禾 packInfo 接口,按 code=200 判断成功,默认仅输出响应,不修改企业状态';
public function handle(YuanheRepository $yuanheRepository): int
{
$timeout = filter_var($this->option('timeout'), FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
if ($timeout === false) {
$this->error('timeout 必须为正整数(单位:秒)。');
return self::FAILURE;
}
$company = Company::find($this->argument('company_id'));
if (!$company) {
$this->error('企业不存在。');
return self::FAILURE;
}
$account = $company->qccAccount;
if (!$account || !in_array($account->status, [CompanyQccAccount::STATUS_SELECTED, CompanyQccAccount::STATUS_OCCUPIED, CompanyQccAccount::STATUS_UNKNOWN], true)) {
$status = $account ? $account->status : 'none';
$this->error("企业必须处于候选(selected)、已占额(occupied)或待人工确认(unknown)状态,当前状态:{$status}。");
return self::FAILURE;
}
if (empty(trim((string) $company->credit_code))) {
$this->error('企业缺少统一社会信用代码,无法调用 packInfo。');
return self::FAILURE;
}
try {
$response = $yuanheRepository->enterprisePackInfo([
'creditCode' => $company->credit_code,
'enterpriseName' => $company->company_name,
], $timeout);
} catch (Throwable $exception) {
$this->printRequest($yuanheRepository);
// 非 JSON 响应保留原文;没有响应时输出错误。
$result = $exception instanceof YuanhePackInfoException && $exception->rawResponse !== null
? $exception->rawResponse
: $exception->getMessage();
$this->output->writeln($result, OutputInterface::OUTPUT_RAW);
return self::FAILURE;
}
$this->printRequest($yuanheRepository);
$this->output->writeln(
json_encode($response, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT),
OutputInterface::OUTPUT_RAW
);
return in_array($response['code'] ?? null, [200, '200'], true) ? self::SUCCESS : self::FAILURE;
}
private function printRequest(YuanheRepository $repository): void
{
if (!$this->option('request')) {
return;
}
$this->output->writeln('请求信息(不含 authKey):', OutputInterface::OUTPUT_RAW);
$this->output->writeln(
$repository->lastRequest
? json_encode($repository->lastRequest, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)
: '未生成请求信息。',
OutputInterface::OUTPUT_RAW
);
$this->output->writeln('返回结果:', OutputInterface::OUTPUT_RAW);
}
}