feat: 客户页面改为站内查看存证和证书

This commit is contained in:
Nixevol
2026-11-15 12:15:04 +08:00
parent 6669722c00
commit 2955efb278
15 changed files with 253 additions and 70 deletions
@@ -58,15 +58,7 @@
<span class="chain-v">{{ formatTime(blockchain.onchainAt) }}</span>
</div>
<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="blockchain.txHash" class="chain-link" @click="openProof">{{ t('chain.explorer') }}</button>
<button ref="certificateButton" v-if="canViewCertificate" class="chain-link certificate-link" :disabled="opening || preview" @click="viewCertificate">
{{ t('chain.certificateView') }}
</button>
@@ -76,29 +68,54 @@
<div v-else class="chain-empty muted">{{ t('chain.notOnchain') }}</div>
</section>
<Teleport to="body">
<dialog ref="proofDialog" class="certificate-dialog" aria-labelledby="proof-dialog-title" @cancel.prevent="closeProof">
<button class="certificate-close" :aria-label="t('viewer.close')" @click="closeProof">×</button>
<h3 id="proof-dialog-title">{{ t('chain.proofTitle') }}</h3>
<div class="sheet-body">
<div v-for="row in proofRows" :key="row.label" class="sheet-row">
<span>{{ row.label }}</span>
<strong>{{ row.value }}</strong>
<button v-if="row.copy" class="icon-copy" :aria-label="t('chain.copy')" @click="$emit('copy', row.copy)">
<svg viewBox="0 0 24 24" width="15" height="15" aria-hidden="true">
<rect x="9" y="9" width="11" height="11" rx="2" fill="none" stroke="currentColor" stroke-width="1.7" />
<path d="M5 15V5a2 2 0 0 1 2-2h8" fill="none" stroke="currentColor" stroke-width="1.7" />
</svg>
</button>
</div>
</div>
</dialog>
<dialog ref="certificateDialog" class="certificate-dialog" aria-labelledby="certificate-dialog-title" @cancel.prevent="closeCertificateDialog">
<button class="certificate-close" :aria-label="t('viewer.close')" @click="closeCertificateDialog">×</button>
<h3 id="certificate-dialog-title">{{ t('chain.certificateView') }}</h3>
<div v-if="!certificateError" class="certificate-spinner" aria-hidden="true" />
<div aria-live="polite">
<div v-if="!certificateError && !certificateRecord" class="certificate-spinner" aria-hidden="true" />
<div class="sheet-body" aria-live="polite">
<p v-if="certificateError" role="alert">{{ certificateError }}</p>
<div v-else-if="certificateRecord" class="certificate-sheet">
<div v-for="row in certificateRows" :key="row.label" class="sheet-row">
<span>{{ row.label }}</span>
<strong>{{ row.value }}</strong>
</div>
</div>
<ol v-else class="certificate-steps">
<li v-for="(key, index) in certificateSteps" :key="key" :class="{ current: certificatePhase === index, done: certificatePhase > index }">
<span aria-hidden="true">{{ certificatePhase > index ? '✓' : index + 1 }}</span>{{ t(key) }}
</li>
</ol>
</div>
<button v-if="certificateError && retryable" class="certificate-retry" @click="viewCertificate">{{ t('chain.certificateCheckAgain') }}</button>
<div v-if="certificateRecord || (certificateError && retryable)" class="dialog-actions">
<button v-if="certificateRecord" class="certificate-retry" type="button" @click="downloadCertificate">{{ t('chain.certificateDownload') }}</button>
<button v-else class="certificate-retry" type="button" @click="viewCertificate">{{ t('chain.certificateCheckAgain') }}</button>
</div>
</dialog>
</Teleport>
</template>
<script setup lang="ts">
import { computed, onUnmounted, ref, watch } from 'vue';
import { fetchTraceCertificate, requestTraceCertificate, type TraceCertificateResult } from '@/api';
import { fetchTraceCertificate, requestTraceCertificate, type TraceCertificateRecord, type TraceCertificateResult } from '@/api';
import { downloadCertificatePng } from '@/utils/certificateImage';
import { useI18n } from 'vue-i18n';
import dayjs from 'dayjs';
import { safeTraceUrl } from './media';
type ProofStatus = 'PENDING' | 'SUBMITTING' | 'CONFIRMING' | 'ONCHAIN' | 'FAILED' | 'UNKNOWN';
const STATUSES = new Set<ProofStatus>([
@@ -121,7 +138,7 @@ const props = defineProps<{
contractAddress?: string | null;
blockHeight?: string | number | null;
onchainAt?: string | null;
explorerUrl?: string | null;
blockHash?: string | null;
failureReason?: string | null;
chainName?: string | null;
} | null;
@@ -131,11 +148,13 @@ const { t } = useI18n();
const opening = ref(false);
const certificateError = ref('');
const canViewCertificate = computed(() => props.blockchain?.status === 'ONCHAIN' && Boolean(props.blockchain.txHash) && props.blockchain.chainName === '天河链');
const proofDialog = ref<HTMLDialogElement>();
const certificateDialog = ref<HTMLDialogElement>();
const certificateButton = ref<HTMLButtonElement>();
const certificatePhase = ref(0);
const certificateRecord = ref<TraceCertificateRecord | null>(null);
const retryable = ref(true);
const certificateSteps = ['chain.certificateRetrieving', 'chain.certificateAcquiring', 'chain.certificateRedirecting'];
const certificateSteps = ['chain.certificateRetrieving', 'chain.certificateAcquiring', 'chain.certificatePreparing'];
let certificateTimer: ReturnType<typeof setTimeout> | undefined;
let certificateRun = 0;
let certificatePolls = 0;
@@ -147,9 +166,16 @@ function stopCertificateWait() {
}
function closeCertificateDialog() {
stopCertificateWait();
certificateRecord.value = null;
certificateDialog.value?.close();
certificateButton.value?.focus();
}
function openProof() {
proofDialog.value?.showModal();
}
function closeProof() {
proofDialog.value?.close();
}
function failCertificate(messageKey: string, canRetry = true) {
certificateError.value = t(messageKey);
retryable.value = canRetry;
@@ -157,11 +183,10 @@ function failCertificate(messageKey: string, canRetry = true) {
}
function showCertificateResult(result: TraceCertificateResult, run: number, code: string) {
if (run !== certificateRun) return;
if (result.status === 'READY' && result.url) {
if (result.status === 'READY' && result.certificate) {
certificatePhase.value = 2;
certificateTimer = setTimeout(() => {
if (run === certificateRun) window.location.assign(result.url!);
}, 350);
certificateRecord.value = result.certificate;
opening.value = false;
return;
}
if (result.status === 'FAILED') return failCertificate('chain.certificateFailed', false);
@@ -203,15 +228,47 @@ const status = computed<ProofStatus>(() => {
const value = props.blockchain?.status as ProofStatus | undefined;
return value && STATUSES.has(value) ? value : 'ONCHAIN';
});
const explorerUrl = computed(() => safeTraceUrl(props.blockchain?.explorerUrl || undefined));
const proofRows = computed(() => {
const chain = props.blockchain;
if (!chain) return [];
return [
{ label: t('chain.hash'), value: chain.txHash || '-', copy: chain.txHash || '' },
{ label: t('chain.contract'), value: chain.contractAddress || '-', copy: chain.contractAddress || '' },
{ label: t('chain.blockHeight'), value: chain.blockHeight == null ? '-' : String(chain.blockHeight), copy: '' },
{ label: t('chain.blockHash'), value: chain.blockHash || '-', copy: chain.blockHash || '' },
{ label: t('chain.onchainAt'), value: chain.onchainAt ? formatTime(chain.onchainAt) : '-', copy: '' },
];
});
const certificateRows = computed(() => {
const record = certificateRecord.value;
if (!record) return [];
return [
{ label: t('chain.certificateContent'), value: record.text },
{ label: t('chain.certificateDigest'), value: record.digest },
{ label: t('chain.hash'), value: record.txHash },
{ label: t('chain.certificateAuthor'), value: record.author },
{ label: t('chain.certificateTime'), value: formatTime(record.createdAt) },
];
});
function downloadCertificate() {
const record = certificateRecord.value;
if (!record) return;
downloadCertificatePng({
title: t('chain.certificateView'),
filename: `certificate-${props.shortCode}.png`,
rows: certificateRows.value,
});
}
function ellipsis(value?: string | null) {
if (!value) return '-';
return value.length > 16 ? `${value.slice(0, 8)}…${value.slice(-6)}` : value;
}
function formatTime(value: string) {
return dayjs(value).format('YYYY-MM-DD HH:mm');
function formatTime(value?: string | null) {
if (!value) return '-';
const parsed = dayjs(value);
return parsed.isValid() ? parsed.format('YYYY-MM-DD HH:mm') : '-';
}
</script>
@@ -301,9 +358,14 @@ function formatTime(value: string) {
.chain-link {
display: inline-block;
margin-top: 10px;
padding: 0;
border: 0;
background: none;
color: var(--brand-2);
font: inherit;
font-size: 14px;
text-decoration: none;
cursor: pointer;
}
.chain-empty {
font-size: 13px;
@@ -312,7 +374,13 @@ function formatTime(value: string) {
.chain-actions { display: flex; align-items: center; gap: 24px; flex-wrap: wrap; }
.certificate-link { margin-left: auto; padding: 0; border: 0; background: none; cursor: pointer; font-family: inherit; }
.certificate-link:disabled { opacity: .55; cursor: default; }
.certificate-dialog { width: min(360px, calc(100vw - 40px)); box-sizing: border-box; padding: 28px; border: 1px solid var(--border); border-radius: 16px; background: var(--card-bg); color: var(--text); box-shadow: 0 16px 60px #0003; }
.certificate-dialog { width: min(420px, calc(100vw - 40px)); max-height: min(640px, calc(100vh - 48px)); box-sizing: border-box; padding: 28px; border: 1px solid var(--border); border-radius: 16px; background: var(--card-bg); color: var(--text); box-shadow: 0 16px 60px #0003; }
.certificate-dialog[open] { display: flex; flex-direction: column; }
.sheet-body { overflow: auto; min-height: 0; }
.sheet-row { display: grid; grid-template-columns: 72px minmax(0, 1fr) auto; gap: 8px; align-items: start; padding: 8px 0; border-bottom: 1px dashed var(--trace-divider); font-size: 13px; }
.sheet-row span { color: var(--muted); }
.sheet-row strong { font-weight: 500; word-break: break-all; white-space: pre-wrap; }
.dialog-actions { padding-top: 16px; }
.certificate-dialog::backdrop { background: #0007; }
.certificate-dialog h3 { margin: 0 0 22px; font-size: 18px; }
.certificate-close { position: absolute; top: 9px; right: 12px; border: 0; background: transparent; color: var(--muted); font-size: 25px; cursor: pointer; }
+8 -1
View File
@@ -27,5 +27,12 @@ export function openTraceViewer(items: PublicMediaItem[], index: number): void {
export function safeTraceUrl(value?: string): string {
const url = traceMediaUrl(value);
return /^(https?:\/\/|\/)/i.test(url) ? url : '';
if (!/^(https?:\/\/|\/)/i.test(url)) return '';
if (url.startsWith('/')) return url;
try {
if (/(^|\.)tianhecloud\.com$/i.test(new URL(url).hostname)) return '';
} catch {
return '';
}
return url;
}