feat: 支持消费者按实物点击生成天河链存证证书

This commit is contained in:
Nixevol
2026-09-20 01:40:04 +08:00
parent 3a6dfd7f24
commit f2d24dff0d
23 changed files with 810 additions and 23 deletions
+12
View File
@@ -43,3 +43,15 @@ export function fetchTracePreview(shortCode: string, token: string, state: 'inac
export function fetchAppInfo(): Promise<any> {
return instance.get('/settings/public') as unknown as Promise<any>;
}
export interface TraceCertificateResult {
status: 'NONE' | 'PENDING' | 'SUBMITTING' | 'CHECKING' | 'WAITING' | 'READY' | 'UNKNOWN' | 'FAILED';
url: string | null;
generatedAt: string | null;
}
export function requestTraceCertificate(shortCode: string): Promise<TraceCertificateResult> {
return instance.post(`/public/trace/${encodeURIComponent(shortCode)}/certificate`, {}) as unknown as Promise<TraceCertificateResult>;
}
export function fetchTraceCertificate(shortCode: string): Promise<TraceCertificateResult> {
return instance.get(`/public/trace/${encodeURIComponent(shortCode)}/certificate`) as unknown as Promise<TraceCertificateResult>;
}
@@ -57,22 +57,31 @@
<span class="chain-k">{{ t('chain.onchainAt') }}</span>
<span class="chain-v">{{ formatTime(blockchain.onchainAt) }}</span>
</div>
<a
v-if="explorerUrl"
class="chain-link"
:href="explorerUrl"
target="_blank"
rel="noopener noreferrer"
>
{{ t('chain.explorer') }} →
</a>
<div class="chain-actions">
<a
v-if="explorerUrl"
class="chain-link"
:href="explorerUrl"
target="_blank"
rel="noopener noreferrer"
>
{{ t('chain.explorer') }} →
</a>
<button v-if="canViewCertificate" class="chain-link certificate-link" :disabled="opening || preview" @click="viewCertificate">
{{ t(opening ? 'chain.certificateOpening' : 'chain.certificateView') }} →
</button>
</div>
<p v-if="preview && canViewCertificate" class="muted chain-status-note">{{ t('chain.certificatePreview') }}</p>
<p v-if="certificateError" role="alert" class="chain-status-note">{{ certificateError }}</p>
</template>
<div v-else class="chain-empty muted">{{ t('chain.notOnchain') }}</div>
</section>
</template>
<script setup lang="ts">
import { computed } from 'vue';
import { computed, ref } from 'vue';
import { useRouter } from 'vue-router';
import { fetchTraceCertificate, requestTraceCertificate } from '@/api';
import { useI18n } from 'vue-i18n';
import dayjs from 'dayjs';
import { safeTraceUrl } from './media';
@@ -90,6 +99,8 @@ const STATUSES = new Set<ProofStatus>([
const props = defineProps<{
title: string;
archiveNo?: string | null;
shortCode: string;
preview?: boolean;
blockchain?: {
status?: string;
txHash?: string | null;
@@ -98,10 +109,38 @@ const props = defineProps<{
onchainAt?: string | null;
explorerUrl?: string | null;
failureReason?: string | null;
chainName?: string | null;
} | null;
}>();
defineEmits<{ (event: 'copy', value: string): void }>();
const { t } = useI18n();
const router = useRouter();
const opening = ref(false);
const certificateError = ref('');
const canViewCertificate = computed(() => status.value === 'ONCHAIN' && props.blockchain?.chainName === '天河链');
async function viewCertificate() {
if (opening.value || props.preview) return;
certificateError.value = '';
const href = router.resolve({ path: `/certificate/${props.shortCode}` }).href;
// Open while the click gesture is active. The waiting page never starts a deposit itself.
const popup = window.open(href, '_blank');
if (popup) popup.opener = null;
opening.value = true;
try {
const cached = await fetchTraceCertificate(props.shortCode);
const result = cached.status === 'NONE' || cached.status === 'UNKNOWN'
? await requestTraceCertificate(props.shortCode) : cached;
if (!popup) {
await router.push(`/certificate/${props.shortCode}`);
} else if (result.status === 'READY' && result.url) {
popup.location.replace(result.url);
}
} catch {
if (popup && !popup.closed) popup.close();
certificateError.value = t('chain.certificateRequestFailed');
} finally { opening.value = false; }
}
const status = computed<ProofStatus>(() => {
const value = props.blockchain?.status as ProofStatus | undefined;
@@ -213,4 +252,7 @@ function formatTime(value: string) {
font-size: 13px;
padding: 8px 0;
}
.chain-actions { display: flex; align-items: center; gap: 24px; flex-wrap: wrap; }
.certificate-link { padding: 0; border: 0; background: none; cursor: pointer; font-family: inherit; }
.certificate-link:disabled { opacity: .55; cursor: default; }
</style>
@@ -41,6 +41,8 @@
:title="moduleTitle(module)"
:archive-no="data.artwork.archiveNo"
:blockchain="data.blockchain"
:short-code="shortCode"
:preview="data.preview === true"
@copy="(value) => $emit('copy', value)"
/>
<trace-image-text-module
@@ -78,6 +80,7 @@ import TraceHorizontalGalleryModule from './TraceHorizontalGalleryModule.vue';
const props = withDefaults(
defineProps<{
data: any;
shortCode: string;
layout?: TraceLayout;
timelineItems?: any[];
carouselInterval?: number;
+27 -4
View File
@@ -1,6 +1,11 @@
{
"app": { "name": "Artwork Traceability" },
"home": { "title": "Artwork Traceability & Authentication", "hint": "Scan the anti-counterfeit QR code on the artwork to view its provenance" },
"app": {
"name": "Artwork Traceability"
},
"home": {
"title": "Artwork Traceability & Authentication",
"hint": "Scan the anti-counterfeit QR code on the artwork to view its provenance"
},
"anti": {
"firstTitle": "Genuine · First Query",
"repeatTitle": "Authenticity Verified",
@@ -57,7 +62,21 @@
"explorer": "View Proof",
"copy": "Copy",
"copied": "Copied",
"notOnchain": "This artwork has not been recorded on-chain yet."
"notOnchain": "This artwork has not been recorded on-chain yet.",
"certificateView": "View certificate",
"certificateOpening": "Opening…",
"certificatePreview": "Certificates cannot be generated in preview mode.",
"certificateRequestFailed": "Unable to request a certificate. Please try later or contact the platform.",
"certificateGenerating": "Your certificate is being prepared. The TiChain certificate page will open when it is ready.",
"certificateShared": "One certificate is generated for this physical item and reused on future visits.",
"certificateOpen": "Open certificate",
"certificateCheckAgain": "Check again",
"certificateBack": "Back",
"certificateReady": "Your certificate is ready.",
"certificateFailed": "The attestation request was rejected. Please contact the platform.",
"certificateUnknown": "The result is not yet confirmed. Check again later; the attestation will not be submitted again.",
"certificateNotRequested": "No certificate has been requested. Select View certificate on the item trace page.",
"certificateUnavailable": "Unable to check the certificate. Please try again later."
},
"field": {
"viewFile": "View file"
@@ -91,5 +110,9 @@
"next": "Next"
},
"artist": "Artist",
"lang": { "zh": "Simplified Chinese", "en": "English", "switch": "Switch language" }
"lang": {
"zh": "Simplified Chinese",
"en": "English",
"switch": "Switch language"
}
}
+27 -4
View File
@@ -1,6 +1,11 @@
{
"app": { "name": "艺术品可信溯源" },
"home": { "title": "艺术品可信溯源与认证", "hint": "请扫描艺术品上的防伪二维码以查看溯源信息" },
"app": {
"name": "艺术品可信溯源"
},
"home": {
"title": "艺术品可信溯源与认证",
"hint": "请扫描艺术品上的防伪二维码以查看溯源信息"
},
"anti": {
"firstTitle": "正品 · 首次查询",
"repeatTitle": "正品验证",
@@ -57,7 +62,21 @@
"explorer": "查看存证",
"copy": "复制",
"copied": "已复制",
"notOnchain": "该作品暂未上链存证。"
"notOnchain": "该作品暂未上链存证。",
"certificateView": "查看证书",
"certificateOpening": "正在打开…",
"certificatePreview": "预览模式不生成证书。",
"certificateRequestFailed": "暂时无法生成证书,请稍后再试或联系平台。",
"certificateGenerating": "证书生成中,请稍候。完成后将自动打开天河链证书页面。",
"certificateShared": "同一件实物仅生成一次,之后直接查看已有证书。",
"certificateOpen": "打开证书",
"certificateCheckAgain": "重新查询",
"certificateBack": "返回",
"certificateReady": "证书已就绪。",
"certificateFailed": "存证申请未成功,请联系平台处理。",
"certificateUnknown": "结果暂未确认。稍后可重新查询,系统不会重复提交存证。",
"certificateNotRequested": "尚未申请证书,请从该实物溯源页点击“查看证书”。",
"certificateUnavailable": "暂时无法查询证书,请稍后重试。"
},
"field": {
"viewFile": "查看文件"
@@ -91,5 +110,9 @@
"next": "下一个"
},
"artist": "艺术家",
"lang": { "zh": "简体中文", "en": "English", "switch": "切换语言" }
"lang": {
"zh": "简体中文",
"en": "English",
"switch": "切换语言"
}
}
+1
View File
@@ -5,6 +5,7 @@ export default createRouter({
routes: [
{ path: '/', component: () => import('@/views/Home.vue') },
{ path: '/trace/:code', component: () => import('@/views/Trace.vue') },
{ path: '/certificate/:code', component: () => import('@/views/Certificate.vue') },
// 后台预览模式:复用 Trace.vue,凭 ?token= 时间令牌访问,不激活/不计数
{ path: '/trace/preview/:code', component: () => import('@/views/Trace.vue') },
{ path: '/:pathMatch(.*)*', redirect: '/' },
+79
View File
@@ -0,0 +1,79 @@
<template>
<main class="certificate-page">
<section class="card certificate-card" aria-live="polite">
<h1>{{ t('chain.certificateView') }}</h1>
<p>{{ t(message) }}</p>
<p class="muted">{{ t('chain.certificateShared') }}</p>
<a v-if="url" class="action" :href="url" rel="noopener noreferrer">{{ t('chain.certificateOpen') }} →</a>
<button v-else-if="retryable" class="action" :disabled="busy" @click="resume">{{ t('chain.certificateCheckAgain') }}</button>
<button class="back" @click="router.back()">{{ t('chain.certificateBack') }}</button>
</section>
</main>
</template>
<script setup lang="ts">
import { onMounted, onUnmounted, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { useI18n } from 'vue-i18n';
import { fetchTraceCertificate, requestTraceCertificate } from '@/api';
const route = useRoute();
const router = useRouter();
const { t } = useI18n();
const message = ref('chain.certificateGenerating');
const retryable = ref(false);
const busy = ref(false);
const url = ref('');
let timer: ReturnType<typeof setTimeout> | undefined;
let stopped = false;
let polls = 0;
async function poll() {
if (stopped) return;
try {
const result = await fetchTraceCertificate(String(route.params.code));
if (stopped) return;
if (result.status === 'READY' && result.url) {
url.value = result.url;
message.value = 'chain.certificateReady';
window.location.replace(result.url);
return;
}
if (result.status === 'FAILED') { message.value = 'chain.certificateFailed'; return; }
if (result.status === 'UNKNOWN') {
message.value = 'chain.certificateUnknown'; retryable.value = true; return;
}
// A GET or link preview must never generate a certificate.
if (result.status === 'NONE' && polls >= 5) { message.value = 'chain.certificateNotRequested'; return; }
if (++polls >= 60) { message.value = 'chain.certificateUnknown'; retryable.value = true; return; }
timer = setTimeout(poll, 5000);
} catch {
if (!stopped) { message.value = 'chain.certificateUnavailable'; retryable.value = true; }
}
}
async function resume() {
if (busy.value) return;
busy.value = true;
try {
// Do not create a new request from this waiting page, even when opened directly.
const state = await fetchTraceCertificate(String(route.params.code));
if (state.status === 'NONE') { message.value = 'chain.certificateNotRequested'; retryable.value = false; return; }
if (state.status === 'UNKNOWN') await requestTraceCertificate(String(route.params.code));
polls = 0; retryable.value = false; message.value = 'chain.certificateGenerating';
await poll();
} catch { message.value = 'chain.certificateRequestFailed'; }
finally { busy.value = false; }
}
onMounted(poll);
onUnmounted(() => { stopped = true; if (timer) clearTimeout(timer); });
</script>
<style scoped>
.certificate-page { max-width: 600px; margin: 8vh auto; padding: 20px; }
.certificate-card { padding: 28px; text-align: center; line-height: 1.7; }
h1 { font-size: 23px; }
.action, .back { display: block; margin: 20px auto 0; padding: 10px 18px; border-radius: 8px; font: inherit; cursor: pointer; }
.action { color: white; background: var(--brand); border: none; text-decoration: none; }
.back { background: transparent; border: 1px solid var(--border); color: var(--muted); }
</style>
+1
View File
@@ -84,6 +84,7 @@
<template v-else-if="data">
<trace-module-renderer
:data="data"
:short-code="String(route.params.code || '')"
:layout="data.artwork.traceLayout"
:timeline-items="timelineItems"
:carousel-interval="carouselInterval"