feat: 强化出库上链一致性(确认中即已上链/出库时间必填/上链须先出库)

This commit is contained in:
Nixevol
2026-07-10 17:53:57 +08:00
parent f364d2d194
commit e1e1cf7308
6 changed files with 24 additions and 5 deletions
+1
View File
@@ -250,6 +250,7 @@
- 出库/上链防重复(2026-07-10):按「一物一码只能出库一次、已上链不可再上」补齐幂等校验。① 出库:`outbound.service.updateSingle`(单件)、`import-preview.processor`(导入预览)、`import.processor`(导入提交,防预览→提交竞态)三处均对 `code.outTime` 已存在拒绝「该追溯码已登记出库,不可重复出库」。② 上链:`attestation.service.trigger` 新增查重——存在 ONCHAIN/CONFIRMING/PENDING 的 proof 即拒绝(已上链→「不可重复上链」,处理中→「请稍候」);仅 FAILED 可通过独立 `retry` 接口重试(retry 原地更新既有 proof、不走 trigger,不受影响)。实测重复上链/重复出库均返回 400 拒绝。
- 天河链确认节奏调整(2026-07-10):`onchain.processor` 确认查询改为「提交完立即查 1 次 → 未到 10s → 再 30s」共 3 次自动尝试,之后停等手动重新确认(原为 30s/60s 两次)。用延迟数组 `AUTO_CONFIRM_DELAYS_MS=[0,10_000,30_000]`、`MAX_AUTO_CONFIRM_ATTEMPTS=数组长度`,第 attempt 次延迟取 `[attempt-1]`。实测:提交同秒即查到 mint 已进块(status 0x0、blockHeight、tokenId),直接 ONCHAIN。
- 天河链方案 B 落地(存证+发行+轮换,2026-07-10,dev/refine):按甲方决策「其余全做、媒体待定」实现。① 数据模型:`blockchain_proof` 加 `token_id`/`contract_address`(迁移 `20260710110000_add_proof_token_fields`);schema 补 `@db.VarChar` 注解消除历史 drift(operation_log.path 512 / outbound_import_session.file_key 512 / backup_record.file_path 1024)。② `TianheProvider` 新增 `publish`(接口2,operateId≤20,name≤42,author≤120,feature≤500)与 `updateUserKey`(接口7)。③ `BlockchainService.attestWithTianhe`:upload 存证→publish 铸 1 NFT(productId=追溯码、feature=存证链接)→ 主 txHash 切为 mint 交易、confirmationRequired=true 走接口9确认→ONCHAIN;合约地址存 `tianheContractAddress`、已发行最大 tokenId 存 `tianheMaxTokenId`(指定合约时作 baseTokenId,队列串行无争用);缺 product 时退化纯存证。`confirmTransaction` 确认成功回填 tokenId、更新 maxTokenId。④ `rotateTianheUserKey`:生成新随机 key→接口7链上改→持久化→接口8校验地址,`/settings/tianhe/identity/rotate` + 设置页「轮换密钥」(二次确认+回显强提示)。⑤ C 端:public-trace + Trace.vue 区块链卡增 tokenId/合约地址展示(i18n chain.tokenId/chain.contract)。⑥ 实测(测试环境):追溯码 412680781069 走通 upload→publish(合约 0xc96ae8a4... tokenId=0)→确认(区块高度 5820347)→ONCHAIN,C 端展示数字藏品编号 0/合约地址/区块高度;轮换 UserKey 成功(钱包地址不变),新 key 回填测试文档。验证:4 包构建 + 27 单测通过。**唯一待定:媒体上链层级 L1/L2/L3 待甲方确认「链上放什么」,当前维持 JSON 存证。**
- 出库/上链一致性强化(2026-07-10,分支 `dev/tianhe-no-metadata-upload`):按甲方口径补三条不变量。① 确认中即已上链:前端 `rowIsOnchain` 由仅认 ONCHAIN 改为认 `ONCHAIN/CONFIRMING/PENDING`(这些状态菜单均显示「上链信息」、不给再触发;仅 FAILED 显示「上链」可重试),与后端 `attestation.trigger` 早有的防重复拦截对齐(天河 publish 一返回 mint 哈希即已广播上链、必进块,除非明确失败,故确认中不可重复上链);`targetProof` 回退用 `latestAnyProof` 使确认中也能展示上链信息。② 出库时间必填默认此刻:弹窗未出库时默认 `dayjs()` 此刻、前端空值拦截、后端 `outbound.updateSingle` 强制 `outTime` 非空。③ 上链必先出库:后端 `attestation.trigger` 增 `isOutbound(code)` 校验,未出库拒绝「请先登记出库再上链」,杜绝「已上链未登记」不一致(防直连 API 绕过)。注:这些为通用改进,暂随本分支;方案二合并时一并进 dev/refine,若方案二被否再单独回植。
- 天河链去掉元数据上链存证、改为仅发行(2026-07-10,分支 `dev/tianhe-no-metadata-upload`,待甲方展示确认后再合并):甲方定「不上传元数据、作品详情/媒体存我方 OSS」。上链链路由「upload 存证 + publish 发行」精简为「仅 publish 发行」,每件省一次上链调用(按次计费更省)。改动:① `AttestPayload.product` 增 `feature`;② `attestation.service` 注入 `StorageService`,`product.feature=作品主图公开URL`(天河 feature 语义=作品媒体地址,正是甲方放 OSS 的图,藏品/交易详情页可展示);③ `onchain.processor.productInfo` 透传 feature;④ `blockchain.service.attestWithTianhe` 删除 `provider.attest(upload)` 调用,直接 publish(feature),raw 去掉 upload 痕迹。`TianheProvider.attest`(upload) 方法保留(AttestationProvider 接口所需 + 便于甲方后续加回,仅需在 attestWithTianhe 恢复两步调用)。dataHash 仍本地计算并展示(作品数据指纹,非天河链上存证)。构建+11 单测通过。分支未合并。
- C端「查看存证」改跳天河区块浏览器交易详情页(2026-07-10):原 `explorerUrl` 存的是 upload 返回的 UDFS 裸 JSON 链接(打开只有文本,不规范)。实测天河浏览器 URL 规则后改为跳交易详情页 `https://{test.}explorer.tianhecloud.com/#/tiChain/transDetail?hash=<mintTxHash>`(显示状态/区块高度/发行动作/合约/productId=追溯码/feature=存证链接,规范完整)。实现:`TianheProvider.explorerTxUrl()` 按 API base 是否含 `test.` 自动切测试/生产浏览器域名;`blockchain.service.attestWithTianhe` 的 `explorerUrl` 改用它,UDFS 链接保留在 `raw.attestUrl`(浏览器 feature 字段也含)。确认流程不覆盖 explorerUrl。C 端/后台经 `public-trace`、trace-code 列表自动跟随。仅对新上链记录生效,旧记录重上链即更新。藏品详情页 `nfrDetail?nfrId=&contractAddress=` 因不传媒体会显示「未获取到媒体内容」故不选它。API 构建+11 单测通过。
- 天河链范围甲方定稿(2026-07-10 微信):① 范围=三步接入的第①②步+第③步「首发」——链上注册→部署合约→铸造链上商品→首发,**首发=铸造到平台钱包(publish 已覆盖,无需额外接口)**;不做第③步 NFT 二级交易(transfer 转让 / burn 合并·销毁)。甲方「不做 NFT」= 不做二级交易,铸造/发行照做,**方案B(存证+发行)保留不动**。② **媒体不上链**:图片/视频全部只存我方服务器/OSS,天河只放存证 JSON;L1/L2/L3 媒体上链全部不做(原「媒体缺口/待甲方确认」项关闭);副作用已知接受:哈希锁结构化溯源信息、不锁媒体本体。③ **不做删除功能**:上链不可删(区块链本质),现状符合(删除不触链、已上链受限删除)。④ **计费=按每次调用上链接口计费**(非按容量),每件=upload+publish 两次调用,是否每件都上链为纯运营决策。结论:代码无需改动,仅锁定决策到 `aidocs/天河链对接分析与建议.md` §七/§八。
+10 -3
View File
@@ -326,7 +326,9 @@ function rowIsOutbound(row: any) {
return Boolean(row.outTime || getOutImages(row).length);
}
function rowIsOnchain(row: any) {
return Boolean(row?.latestProof);
// 已提交上链即视为已上链:ONCHAIN(已确认)/CONFIRMING(确认中)/PENDING(待处理) 均不允许重复上链,仅 FAILED 可重试
const s = row?.latestAnyProof?.status;
return s === 'ONCHAIN' || s === 'CONFIRMING' || s === 'PENDING';
}
function outboundMenuLabel(row: any) {
if (rowIsOnchain(row)) return t('pages.traceCode.onchainInfo');
@@ -336,7 +338,7 @@ function outboundMenuLabel(row: any) {
const targetOutbound = computed(() => rowIsOutbound(outboundTarget.value));
const targetOnchain = computed(() => rowIsOnchain(outboundTarget.value));
const outboundReadonly = computed(() => targetOutbound.value || targetOnchain.value);
const targetProof = computed<any>(() => outboundTarget.value?.latestProof ?? null);
const targetProof = computed<any>(() => outboundTarget.value?.latestProof ?? outboundTarget.value?.latestAnyProof ?? null);
const outboundModalTitle = computed(() => outboundMenuLabel(outboundTarget.value));
const columns = computed<DataTableColumns<any>>(() => {
@@ -819,7 +821,8 @@ function getOutImages(row: any) {
function openOutboundModal(row: any) {
outboundTarget.value = row;
outboundForm.outTime = formatDateTime(row.outTime);
// 出库时间必填:未出库时默认此刻;已出库时回显既有时间(只读)
outboundForm.outTime = row.outTime ? formatDateTime(row.outTime) : dayjs().format('YYYY-MM-DD HH:mm');
outboundForm.remark = row.remark || '';
outImages.value = getOutImages(row);
outboundModal.value = true;
@@ -827,6 +830,10 @@ function openOutboundModal(row: any) {
async function saveOutbound(triggerOnchain: boolean) {
if (!outboundTarget.value) return;
if (!outboundReadonly.value && !outboundForm.outTime) {
message.warning(t('pages.outbound.outTimeRequired'));
return;
}
if (triggerOnchain) {
outboundTriggering.value = true;
} else {
+2 -1
View File
@@ -466,7 +466,8 @@
"FAILED": "Failed",
"EXPIRED": "Expired"
},
"onchainAutoTip": "On-chain hash, attestation link, and on-chain time are written automatically from the configured blockchain API response."
"onchainAutoTip": "On-chain hash, attestation link, and on-chain time are written automatically from the configured blockchain API response.",
"outTimeRequired": "Outbound time is required"
},
"blockchain": {
"title": "Blockchain Attestation",
+2 -1
View File
@@ -466,7 +466,8 @@
"FAILED": "失败",
"EXPIRED": "已过期"
},
"onchainAutoTip": "链上哈希、存证链接、上链时间由当前配置的区块链接口返回后自动写入。"
"onchainAutoTip": "链上哈希、存证链接、上链时间由当前配置的区块链接口返回后自动写入。",
"outTimeRequired": "出库时间不能为空"
},
"blockchain": {
"title": "区块链存证",
@@ -5,6 +5,7 @@ import { ErrorCode, ProofStatus, QUEUE_NAMES } from '@artveris/shared';
import { PrismaService } from '../prisma/prisma.service';
import { BusinessException } from '../common/exceptions/business.exception';
import { StorageService } from '../storage/storage.service';
import { isOutbound } from '../outbound/outbound.util';
import { BlockchainService } from './blockchain.service';
@Injectable()
@@ -36,6 +37,10 @@ export class AttestationService {
if (!code.artworkId || !code.artwork) {
throw new BusinessException(ErrorCode.BAD_REQUEST, '追溯码未绑定作品,无法上链');
}
// 上链代表已登记出库:未出库不允许上链(杜绝「已上链未登记」的不一致状态)
if (!isOutbound(code)) {
throw new BusinessException(ErrorCode.BAD_REQUEST, '该追溯码尚未登记出库,请先登记出库再上链');
}
// 防重复上链:已存在「上链成功/上链中/待上链」的存证则拒绝;仅失败(FAILED)的可再次发起。
const active = await this.prisma.blockchainProof.findFirst({
@@ -38,6 +38,10 @@ export class OutboundService {
if (isOutbound(code)) {
throw new BusinessException(ErrorCode.BAD_REQUEST, '该追溯码已登记出库,不可重复出库');
}
// 出库时间必填:出库登记必须有出库时间
if (!dto.outTime) {
throw new BusinessException(ErrorCode.BAD_REQUEST, '出库时间不能为空');
}
const imageKeys = dto.outImageKeys ?? (dto.outImageKey ? [dto.outImageKey] : []);
const data: Prisma.TraceCodeUpdateInput = {};
if ('outTime' in dto) data.outTime = dto.outTime ? new Date(dto.outTime) : null;