feat: update report detail and verification flow

This commit is contained in:
wushumin
2026-05-22 13:31:02 +08:00
parent b98d6164a7
commit be64b8e5b7
14 changed files with 1336 additions and 282 deletions

View File

@@ -461,12 +461,21 @@ class AppraisalTasksController
$reportNo = trim((string)$request->input('zhongjian_report_no', ''));
$qrInput = trim((string)$request->input('qr_input', ''));
$files = $this->evidenceService()->normalize($request->input('report_files', []), $request, true);
$productInput = $request->input('product_info', null);
$productPayload = is_array($productInput) ? $this->normalizeProductInput($productInput) : null;
$attachments = $this->evidenceService()->normalize($request->input('attachments', []), $request, true);
$keyPoints = $this->normalizeKeyPointInput($request->input('key_points', []));
$resultText = trim((string)$request->input('result_text', ''));
$resultDesc = trim((string)$request->input('result_desc', ''));
if ($id <= 0) {
return api_error('任务 ID 不能为空', 422);
}
if ($reportNo === '') {
return api_error('中检报告编号不能为空', 422);
}
if ($resultText === '') {
return api_error('鉴定结论不能为空', 422);
}
if (!$files) {
return api_error('请至少上传 1 个中检报告文件', 422);
}
@@ -514,6 +523,15 @@ class AppraisalTasksController
$task = array_merge($task, $operatorGuard['task_update']);
}
if ($productPayload !== null) {
$this->saveOrderProductSnapshot((int)$task['order_id'], $productPayload, $now);
}
if (!$this->hasSubmittableProductInfo((int)$task['order_id'], $productPayload)) {
Db::rollback();
return api_error('提交中检报告前请先完善物品信息', 422);
}
Db::name('appraisal_tasks')->where('id', $id)->update([
'status' => 'completed',
'started_at' => $task['started_at'] ?: $now,
@@ -531,14 +549,14 @@ class AppraisalTasksController
'task_id' => $id,
'order_id' => (int)$task['order_id'],
'result_status' => 'zhongjian_report',
'result_text' => '以中检报告为准',
'result_desc' => '中检报告已回传并由平台录入。',
'result_text' => $resultText,
'result_desc' => $resultDesc,
'condition_grade' => '',
'condition_desc' => '',
'valuation_min' => 0,
'valuation_max' => 0,
'valuation_desc' => '',
'attachments_json' => json_encode($files, JSON_UNESCAPED_UNICODE),
'attachments_json' => $attachments ? json_encode($attachments, JSON_UNESCAPED_UNICODE) : null,
'external_remark' => '',
'internal_remark' => '中检报告编号:' . $reportNo,
'updated_at' => $now,
@@ -546,10 +564,12 @@ class AppraisalTasksController
$resultId = Db::name('appraisal_task_results')->where('task_id', $id)->value('id');
if ($resultId) {
Db::name('appraisal_task_results')->where('id', (int)$resultId)->update($resultPayload);
$savedResultId = (int)$resultId;
} else {
$resultPayload['created_at'] = $now;
Db::name('appraisal_task_results')->insert($resultPayload);
$savedResultId = (int)Db::name('appraisal_task_results')->insertGetId($resultPayload);
}
$this->saveTaskKeyPoints($savedResultId, $keyPoints, $now);
$this->createOrUpdateReportDraft((int)$task['order_id'], $task, $resultPayload, $now);
$report = $this->findLatestAppraisalReport((int)$task['order_id']);
@@ -578,7 +598,7 @@ class AppraisalTasksController
'order_id' => (int)$task['order_id'],
'node_code' => 'zhongjian_report_entered',
'node_text' => '中检报告已录入',
'node_desc' => '报告录入人已录入中检报告编号并上传报告文件。',
'node_desc' => '报告录入人已补全报告信息、录入中检报告编号并上传报告文件。',
'operator_type' => 'admin',
'operator_id' => $operatorId,
'occurred_at' => $now,
@@ -1814,7 +1834,7 @@ class AppraisalTasksController
'appraisal_no' => $order['appraisal_no'] ?? '',
'report_type' => 'appraisal',
'service_provider' => $task['service_provider'],
'institution_name' => $task['service_provider'] === 'zhongjian' ? '中检合作机构' : '安心验',
'institution_name' => $this->displayInstitutionName((string)$task['service_provider']),
'report_title' => $task['service_provider'] === 'zhongjian' ? '中检鉴定报告' : '安心验鉴定报告',
'report_status' => 'pending_publish',
'publish_time' => null,
@@ -1870,7 +1890,7 @@ class AppraisalTasksController
private function buildAppraisalSnapshot(string $serviceProvider, string $fallbackTime, ?array $firstReviewTask, ?array $finalReviewTask): array
{
$institutionName = $serviceProvider === 'zhongjian' ? '中检合作机构' : '安心验';
$institutionName = $this->displayInstitutionName($serviceProvider);
$appraiserName = $this->normalizeAssigneeName($firstReviewTask['assignee_name'] ?? '')
?: $this->normalizeAssigneeName($finalReviewTask['assignee_name'] ?? '');
$reviewerName = $appraiserName;
@@ -1899,6 +1919,11 @@ class AppraisalTasksController
return $name;
}
private function displayInstitutionName(string $serviceProvider): string
{
return $serviceProvider === 'zhongjian' ? '中检鉴定中心' : '安心检验';
}
private function guardTaskOperator(Request $request, array $task): array
{
$adminId = (int)$request->header('x-admin-id', 0);

View File

@@ -78,7 +78,7 @@ class ReportsController
'report_status_text' => $this->reportStatusText($item['report_status']),
'service_provider' => $item['service_provider'],
'service_provider_text' => $this->serviceProviderText($item['service_provider']),
'institution_name' => $item['institution_name'] ?: $this->defaultInstitutionName($item['service_provider']),
'institution_name' => $this->defaultInstitutionName((string)$item['service_provider']),
'publish_time' => $item['publish_time'],
'zhongjian_report_no' => (string)($item['zhongjian_report_no'] ?? ''),
'report_entry_admin_name' => (string)($item['report_entry_admin_name'] ?? ''),
@@ -163,7 +163,7 @@ class ReportsController
'report_status_text' => $this->reportStatusText($report['report_status']),
'service_provider' => $report['service_provider'],
'service_provider_text' => $this->serviceProviderText($report['service_provider']),
'institution_name' => $report['institution_name'] ?: $this->defaultInstitutionName($report['service_provider']),
'institution_name' => $this->defaultInstitutionName((string)$report['service_provider']),
'publish_time' => $report['publish_time'],
'zhongjian_report_no' => (string)($report['zhongjian_report_no'] ?? ''),
'report_entry_admin_id' => (int)($report['report_entry_admin_id'] ?? 0),
@@ -765,6 +765,6 @@ class ReportsController
private function defaultInstitutionName(string $serviceProvider): string
{
return $serviceProvider === 'zhongjian' ? '中检合作机构' : '安心验';
return $serviceProvider === 'zhongjian' ? '中检鉴定中心' : '安心验';
}
}

View File

@@ -49,7 +49,7 @@ class ReportsController
'service_provider' => $item['service_provider'],
'status' => $published ? '已出报告' : '待出报告',
'result_text' => $published ? '正品' : '待出报告',
'institution_name' => $item['institution_name'] ?: '安心验',
'institution_name' => $this->displayInstitutionName((string)$item['service_provider']),
'publish_time' => $item['publish_time'],
];
}, $rows);
@@ -94,7 +94,6 @@ class ReportsController
$content = Db::name('report_contents')->where('report_id', $reportData['id'])->find();
$verify = Db::name('report_verifies')->where('report_id', $reportData['id'])->find() ?: [];
$verify = $this->normalizeVerifyInfo($reportData, $verify);
$pdfUrl = $this->ensurePdfFile($request, $reportData, $content ?: [], $verify ?: []);
$evidenceAttachments = $this->evidenceService()->normalize($content['evidence_attachments_json'] ?? null, $request);
$zhongjianReportFiles = $this->evidenceService()->normalize($content['zhongjian_report_files_json'] ?? null, $request);
$defaultRiskNotice = (new ContentService())->getReportRiskNotice((string)($reportData['report_type'] ?? 'appraisal'));
@@ -105,6 +104,17 @@ class ReportsController
'valuation_snapshot' => $this->decodeJsonField($content['valuation_snapshot_json'] ?? null),
'risk_notice_text' => ($content['risk_notice_text'] ?? '') !== '' ? $content['risk_notice_text'] : $defaultRiskNotice,
];
$productDisplay = $this->buildProductDisplay($reportData, $payload['product_snapshot'], $payload['result_snapshot']);
$reportMedia = [
'images' => $this->filterAssetsByType($evidenceAttachments, 'image'),
];
$traceInfo = $this->buildTraceInfo(
(int)($reportData['order_id'] ?? 0),
$payload['appraisal_snapshot'],
$evidenceAttachments,
$request
);
$pdfUrl = $this->ensurePdfFile($request, $reportData, $content ?: [], $verify ?: [], $productDisplay, $reportMedia);
return api_success([
'report_header' => [
@@ -114,7 +124,7 @@ class ReportsController
'report_title' => $reportData['report_title'],
'report_status' => $reportData['report_status'],
'service_provider' => $reportData['service_provider'],
'institution_name' => $reportData['institution_name'],
'institution_name' => $this->displayInstitutionName((string)$reportData['service_provider']),
'publish_time' => $reportData['publish_time'],
'zhongjian_report_no' => (string)($reportData['zhongjian_report_no'] ?? ''),
'report_entry_admin_name' => (string)($reportData['report_entry_admin_name'] ?? ''),
@@ -126,6 +136,9 @@ class ReportsController
'valuation_info' => $payload['valuation_snapshot'],
'evidence_attachments' => $evidenceAttachments,
'zhongjian_report_files' => $zhongjianReportFiles,
'report_media' => $reportMedia,
'product_display' => $productDisplay,
'trace_info' => $traceInfo,
'risk_notice_text' => $payload['risk_notice_text'],
'verify_info' => [
'report_no' => $reportData['report_no'],
@@ -139,6 +152,75 @@ class ReportsController
]);
}
public function antiCounterfeitVerify(Request $request)
{
$reportNo = trim((string)$request->input('report_no', ''));
$verifyCode = trim((string)$request->input('verify_code', ''));
if ($reportNo === '' || $verifyCode === '') {
return api_error('报告编号和防伪查询码不能为空', 422);
}
if (!preg_match('/^\d{6}$/', $verifyCode)) {
return api_error('防伪查询码应为 6 位数字', 422);
}
$report = Db::name('reports')
->where('report_no', $reportNo)
->where('report_status', 'published')
->find();
if (!$report) {
return api_success([
'verify_passed' => false,
'verify_message' => '未查询到有效报告,请核对报告编号后重试。',
'verify_count' => 0,
]);
}
$tag = Db::name('material_tag_codes')
->where('report_id', (int)$report['id'])
->where('bind_status', 'bound')
->find();
if (!$tag) {
return api_success([
'verify_passed' => false,
'verify_message' => '当前报告尚未绑定防伪吊牌,暂不能完成防伪查询。',
'verify_count' => 0,
]);
}
$batch = Db::name('material_batches')->where('id', (int)$tag['batch_id'])->find();
$active = ($tag['status'] ?? 'active') !== 'invalid'
&& (!$batch || ($batch['status'] ?? 'active') !== 'invalid');
$passed = $active
&& hash_equals((string)$tag['verify_code'], $verifyCode)
&& (string)($tag['report_no'] ?: $report['report_no']) === $reportNo;
$now = date('Y-m-d H:i:s');
if ($passed) {
Db::name('material_tag_codes')->where('id', (int)$tag['id'])->update([
'verify_count' => (int)$tag['verify_count'] + 1,
'last_verified_at' => $now,
'updated_at' => $now,
]);
}
$this->insertMaterialTagVerifyLog($tag, $reportNo, $verifyCode, $passed, $request, $now);
if (!$active) {
return api_success([
'verify_passed' => false,
'verify_message' => '该防伪吊牌已失效,不能进行防伪查询。',
'verify_count' => (int)$tag['verify_count'],
]);
}
return api_success([
'verify_passed' => $passed,
'verify_message' => $passed
? '防伪查询通过,该报告编号与吊牌防伪查询码匹配。'
: '防伪查询码与当前报告不匹配,请核对吊牌后重试。',
'verify_count' => (int)$tag['verify_count'] + ($passed ? 1 : 0),
]);
}
private function decodeJsonField(mixed $value): array
{
if (is_array($value)) {
@@ -171,49 +253,41 @@ class ReportsController
return $verify;
}
private function ensurePdfFile(Request $request, array $report, array $content, array $verify): string
private function ensurePdfFile(Request $request, array $report, array $content, array $verify, array $productDisplay, array $reportMedia): string
{
$existingFile = Db::name('report_files')
->where('report_id', (int)$report['id'])
->where('file_type', 'pdf')
->find();
$publishTime = (string)($report['publish_time'] ?: date('Y-m-d H:i:s'));
$relativeDir = 'uploads/reports/' . date('Ymd', strtotime($publishTime));
$filename = $report['report_no'] . '-v2.pdf';
$relativePath = $relativeDir . '/' . $filename;
if ($existingFile && !empty($existingFile['file_url'])) {
$relativeUrl = ltrim((string)$existingFile['file_url'], '/');
if ($this->storage()->exists($relativeUrl)) {
if ($relativeUrl === $relativePath && !$this->shouldRebuildPdf($existingFile, $report, $content) && $this->storage()->exists($relativeUrl)) {
return $this->storage()->publicUrl($request, $relativeUrl);
}
}
$productInfo = $this->decodeJsonField($content['product_snapshot_json'] ?? null);
$resultInfo = $this->decodeJsonField($content['result_snapshot_json'] ?? null);
$appraisalInfo = $this->decodeJsonField($content['appraisal_snapshot_json'] ?? null);
$valuationInfo = $this->decodeJsonField($content['valuation_snapshot_json'] ?? null);
$publishTime = (string)($report['publish_time'] ?: date('Y-m-d H:i:s'));
$defaultRiskNotice = (new ContentService())->getReportRiskNotice((string)($report['report_type'] ?? 'appraisal'));
$relativeDir = 'uploads/reports/' . date('Ymd', strtotime($publishTime));
$filename = $report['report_no'] . '.pdf';
$relativePath = $relativeDir . '/' . $filename;
$generator = new ReportPdfGenerator();
$pdfBinary = $generator->generate([
'report_title' => $report['report_title'] ?? '鉴定报告',
'service_provider_text' => ($report['service_provider'] ?? 'anxinyan') === 'zhongjian' ? '中检鉴定' : '实物鉴定',
'institution_name' => $report['institution_name'] ?? '安心验',
'institution_name' => $this->displayInstitutionName((string)($report['service_provider'] ?? 'anxinyan')),
'report_no' => $report['report_no'] ?? '',
'publish_time' => $publishTime,
'result_text' => $resultInfo['result_text'] ?? '-',
'result_desc' => $resultInfo['result_desc'] ?? '-',
'product_name' => $productInfo['product_name'] ?? '-',
'category_brand' => trim(($productInfo['category_name'] ?? '-') . ' / ' . ($productInfo['brand_name'] ?? '-')),
'spec_info' => trim(($productInfo['color'] ?? '-') . ' / ' . ($productInfo['size_spec'] ?? '-')),
'appraisers' => trim((string)($appraisalInfo['appraiser_name'] ?? '-')),
'condition_grade' => $valuationInfo['condition_grade'] ?? '-',
'valuation_range' => sprintf(
'¥%s - ¥%s',
$valuationInfo['valuation_min'] ?? 0,
$valuationInfo['valuation_max'] ?? 0
),
'product_items' => $productDisplay['items'] ?? [],
'hero_image' => $this->firstPdfHeroImage($reportMedia['images'] ?? []),
'hero_image_labels' => $this->assetNameList($reportMedia['images'] ?? []),
'verify_info' => sprintf(
'%s / %s',
$verify['report_no'] ?? ($report['report_no'] ?? '-'),
@@ -222,7 +296,7 @@ class ReportsController
'risk_notice_text' => ($content['risk_notice_text'] ?? '') !== '' ? $content['risk_notice_text'] : ($defaultRiskNotice !== '' ? $defaultRiskNotice : '-'),
]);
$this->storage()->putContents($relativePath, $pdfBinary);
$this->storage()->putContentsWithMimeType($relativePath, $pdfBinary, 'application/pdf');
$now = date('Y-m-d H:i:s');
$filePayload = [
@@ -243,6 +317,268 @@ class ReportsController
return $this->storage()->publicUrl($request, $relativePath);
}
private function buildProductDisplay(array $report, array $productInfo, array $resultInfo): array
{
$items = [
[
'label' => '检测结论',
'value' => $this->textValue($resultInfo['result_text'] ?? '') ?: '-',
'remark' => $this->textValue($resultInfo['result_desc'] ?? ''),
],
[
'label' => '品牌',
'value' => $this->textValue($productInfo['brand_name'] ?? '') ?: '-',
'remark' => '',
],
];
foreach (($resultInfo['key_points'] ?? []) as $point) {
if (!is_array($point)) {
continue;
}
$label = $this->textValue($point['point_name'] ?? '');
if ($label === '') {
continue;
}
$items[] = [
'label' => $label,
'value' => $this->textValue($point['point_value'] ?? '') ?: '-',
'remark' => $this->textValue($point['point_remark'] ?? ''),
];
}
return [
'product_name' => $this->textValue($productInfo['product_name'] ?? '') ?: '-',
'institution_name' => $this->displayInstitutionName((string)($report['service_provider'] ?? 'anxinyan')),
'items' => $items,
];
}
private function buildTraceInfo(int $orderId, array $appraisalInfo, array $evidenceAttachments, Request $request): array
{
$logs = $orderId > 0
? Db::name('order_transfer_flow_logs')
->where('order_id', $orderId)
->whereIn('action_code', ['inbound_received', 'return_shipped'])
->order('id', 'asc')
->select()
->toArray()
: [];
$inboundLog = $this->findFlowLog($logs, 'inbound_received');
$returnLog = $this->findFlowLog($logs, 'return_shipped');
$inboundPayload = $this->decodeJsonObject($inboundLog['payload_json'] ?? null);
$returnPayload = $this->decodeJsonObject($returnLog['payload_json'] ?? null);
$appraisalFinishedAt = $this->appraisalFinishedAt($orderId, $appraisalInfo);
$appraisalVideoAssets = $this->filterAssetsByType($evidenceAttachments, 'video');
$nodes = [
[
'code' => 'inbound',
'title' => '入仓',
'occurred_at' => (string)($inboundLog['created_at'] ?? ''),
'status' => $inboundLog ? 'completed' : 'pending',
'assets' => $this->evidenceService()->normalize($inboundPayload['inbound_attachments'] ?? [], $request),
],
[
'code' => 'appraisal',
'title' => '鉴定',
'occurred_at' => $appraisalFinishedAt,
'status' => ($appraisalFinishedAt !== '' || $appraisalVideoAssets) ? 'completed' : 'pending',
'assets' => $appraisalVideoAssets,
],
[
'code' => 'return',
'title' => '寄回',
'occurred_at' => (string)($returnLog['created_at'] ?? ''),
'status' => $returnLog ? 'completed' : 'pending',
'assets' => $this->evidenceService()->normalize($returnPayload['packing_attachments'] ?? [], $request),
],
];
return ['nodes' => $nodes];
}
private function findFlowLog(array $logs, string $actionCode): ?array
{
$matched = null;
foreach ($logs as $log) {
if (($log['action_code'] ?? '') === $actionCode) {
$matched = $log;
}
}
return $matched;
}
private function appraisalFinishedAt(int $orderId, array $appraisalInfo): string
{
if ($orderId > 0) {
$submittedAt = Db::name('appraisal_tasks')
->where('order_id', $orderId)
->whereRaw('submitted_at IS NOT NULL')
->order('submitted_at', 'desc')
->value('submitted_at');
if ($submittedAt) {
return (string)$submittedAt;
}
}
return $this->textValue($appraisalInfo['appraisal_time'] ?? '');
}
private function filterAssetsByType(array $assets, string $fileType): array
{
return array_values(array_filter($assets, fn (array $asset) => (string)($asset['file_type'] ?? '') === $fileType));
}
private function shouldRebuildPdf(array $existingFile, array $report, array $content): bool
{
$fileUpdatedAt = strtotime((string)($existingFile['updated_at'] ?? '')) ?: 0;
if ($fileUpdatedAt <= 0) {
return true;
}
foreach ([$report['updated_at'] ?? '', $content['updated_at'] ?? ''] as $timestamp) {
if ((strtotime((string)$timestamp) ?: 0) > $fileUpdatedAt) {
return true;
}
}
return false;
}
private function firstPdfHeroImage(array $images): ?array
{
foreach ($images as $image) {
$binary = $this->readAssetBinary((string)($image['file_url'] ?? ''));
if ($binary === '' || strlen($binary) > 5 * 1024 * 1024) {
continue;
}
$info = @getimagesizefromstring($binary);
if (!$info) {
continue;
}
if ((int)($info[2] ?? 0) !== IMAGETYPE_JPEG) {
$converted = $this->convertImageBinaryToJpeg($binary);
if ($converted === '') {
continue;
}
$binary = $converted;
}
return [
'data' => $binary,
'width' => (int)$info[0],
'height' => (int)$info[1],
];
}
return null;
}
private function convertImageBinaryToJpeg(string $binary): string
{
if (!function_exists('imagecreatefromstring')) {
return '';
}
$image = @imagecreatefromstring($binary);
if (!$image) {
return '';
}
$width = imagesx($image);
$height = imagesy($image);
$canvas = imagecreatetruecolor($width, $height);
if (!$canvas) {
imagedestroy($image);
return '';
}
$background = imagecolorallocate($canvas, 255, 255, 255);
imagefill($canvas, 0, 0, $background);
imagecopy($canvas, $image, 0, 0, 0, 0, $width, $height);
ob_start();
imagejpeg($canvas, null, 86);
$jpeg = (string)ob_get_clean();
imagedestroy($canvas);
imagedestroy($image);
return $jpeg;
}
private function readAssetBinary(string $fileUrl): string
{
$fileUrl = trim($fileUrl);
if ($fileUrl === '') {
return '';
}
$relativePath = $this->storage()->storagePath($fileUrl);
$localPath = public_path() . '/' . $relativePath;
if (is_file($localPath)) {
return (string)file_get_contents($localPath);
}
if (preg_match('/^https?:\/\//i', $fileUrl)) {
$context = stream_context_create([
'http' => ['timeout' => 3],
'ssl' => ['verify_peer' => false, 'verify_peer_name' => false],
]);
return (string)(@file_get_contents($fileUrl, false, $context) ?: '');
}
return '';
}
private function assetNameList(array $assets): array
{
return array_values(array_filter(array_map(function (array $asset) {
return $this->textValue($asset['name'] ?? '') ?: $this->textValue($asset['file_id'] ?? '');
}, $assets)));
}
private function insertMaterialTagVerifyLog(array $tag, string $reportNo, string $verifyCode, bool $passed, Request $request, string $now): void
{
Db::name('material_tag_scan_logs')->insert([
'tag_code_id' => (int)$tag['id'],
'batch_id' => (int)$tag['batch_id'],
'report_id' => (int)($tag['report_id'] ?? 0) ?: null,
'report_no' => $reportNo,
'verify_type' => 'report_page_code',
'verify_code_input' => mb_substr($verifyCode, 0, 16),
'verify_passed' => $passed ? 1 : 0,
'ip' => (string)$request->getRealIp(),
'user_agent' => mb_substr((string)$request->header('user-agent', ''), 0, 500),
'scanned_at' => $now,
'created_at' => $now,
]);
}
private function decodeJsonObject(mixed $value): array
{
if (is_array($value)) {
return $value;
}
if (is_string($value) && $value !== '') {
$decoded = json_decode($value, true);
return is_array($decoded) ? $decoded : [];
}
return [];
}
private function textValue(mixed $value): string
{
return trim((string)($value ?? ''));
}
private function displayInstitutionName(string $serviceProvider): string
{
return $serviceProvider === 'zhongjian' ? '中检鉴定中心' : '安心检验';
}
private function buildPublicPageUrl(string $pagePath, array $query = []): string
{
$baseUrl = $this->normalizeH5BaseUrl($this->getSystemConfigValue('h5', 'page_base_url'));