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

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