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
+8 -1
View File
@@ -44,10 +44,17 @@ export function fetchAppInfo(): Promise<any> {
return instance.get('/settings/public') as unknown as Promise<any>;
}
export interface TraceCertificateRecord {
text: string;
digest: string;
txHash: string;
author: string;
createdAt: string | null;
}
export interface TraceCertificateResult {
status: 'NONE' | 'PENDING' | 'SUBMITTING' | 'CHECKING' | 'WAITING' | 'READY' | 'UNKNOWN' | 'FAILED';
url: string | null;
generatedAt: string | null;
certificate: TraceCertificateRecord | null;
}
export function requestTraceCertificate(shortCode: string): Promise<TraceCertificateResult> {
return instance.post(`/public/trace/${encodeURIComponent(shortCode)}/certificate`, {}) as unknown as Promise<TraceCertificateResult>;
@@ -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;
}
+9 -2
View File
@@ -59,6 +59,8 @@
"contract": "Contract",
"blockHeight": "Block Height",
"onchainAt": "On-chain Time",
"blockHash": "Block Hash",
"proofTitle": "Attestation",
"explorer": "View Proof",
"copy": "Copy",
"copied": "Copied",
@@ -71,8 +73,13 @@
"certificateUnavailable": "Unable to check the certificate. Please try again later.",
"certificateRetrieving": "Retrieving attestation details",
"certificateAcquiring": "Preparing your certificate",
"certificateRedirecting": "Opening your certificate",
"certificateDelayed": "Your certificate is not ready yet. Please try again later."
"certificatePreparing": "Assembling your certificate",
"certificateDelayed": "Your certificate is not ready yet. Please try again later.",
"certificateContent": "Attestation text",
"certificateDigest": "Digest",
"certificateAuthor": "Author",
"certificateTime": "Created",
"certificateDownload": "Download certificate"
},
"field": {
"viewFile": "View file"
+9 -2
View File
@@ -59,6 +59,8 @@
"contract": "合约地址",
"blockHeight": "区块高度",
"onchainAt": "上链时间",
"blockHash": "区块哈希",
"proofTitle": "存证详情",
"explorer": "查看存证",
"copy": "复制",
"copied": "已复制",
@@ -71,8 +73,13 @@
"certificateUnavailable": "暂时无法查询证书,请稍后重试。",
"certificateRetrieving": "正在调取存证数据",
"certificateAcquiring": "获取存证证书",
"certificateRedirecting": "正在打开存证证书",
"certificateDelayed": "证书暂未就绪,请稍后再试。"
"certificatePreparing": "正在整理存证证书",
"certificateDelayed": "证书暂未就绪,请稍后再试。",
"certificateContent": "存证内容",
"certificateDigest": "摘要",
"certificateAuthor": "作者",
"certificateTime": "生成时间",
"certificateDownload": "下载证书"
},
"field": {
"viewFile": "查看文件"
+73
View File
@@ -0,0 +1,73 @@
export interface CertificateImageInput {
title: string;
rows: Array<{ label: string; value: string }>;
filename: string;
}
const FONT = '"Microsoft YaHei", "PingFang SC", sans-serif';
function wrap(ctx: CanvasRenderingContext2D, value: string, maxWidth: number): string[] {
const lines: string[] = [];
let line = '';
for (const char of Array.from(value || '-')) {
const next = line + char;
if (line && ctx.measureText(next).width > maxWidth) {
lines.push(line);
line = char;
} else {
line = next;
}
}
lines.push(line || '-');
return lines;
}
export function downloadCertificatePng(input: CertificateImageInput): void {
const width = 750;
const pad = 48;
const maxWidth = width - pad * 2;
const measure = document.createElement('canvas').getContext('2d');
if (!measure) return;
const lines: Array<{ text: string; font: string; gap: number; color: string }> = [];
measure.font = `bold 28px ${FONT}`;
for (const text of wrap(measure, input.title, maxWidth)) {
lines.push({ text, font: measure.font, gap: 36, color: '#143028' });
}
for (const row of input.rows) {
measure.font = `bold 14px ${FONT}`;
lines.push({ text: row.label, font: measure.font, gap: 22, color: '#5c6b66' });
measure.font = `16px ${FONT}`;
const wrapped = wrap(measure, row.value, maxWidth);
wrapped.forEach((text, index) => {
lines.push({ text, font: measure.font, gap: index === wrapped.length - 1 ? 28 : 22, color: '#1c1c1c' });
});
}
const height = pad * 2 + lines.reduce((sum, line) => sum + line.gap, 0);
const canvas = document.createElement('canvas');
canvas.width = width * 2;
canvas.height = height * 2;
const ctx = canvas.getContext('2d');
if (!ctx) return;
ctx.scale(2, 2);
ctx.fillStyle = '#f7f3ea';
ctx.fillRect(0, 0, width, height);
ctx.strokeStyle = '#0f766e';
ctx.lineWidth = 3;
ctx.strokeRect(18, 18, width - 36, height - 36);
let y = pad + 8;
for (const line of lines) {
ctx.font = line.font;
ctx.fillStyle = line.color;
ctx.fillText(line.text, pad, y);
y += line.gap;
}
canvas.toBlob((blob) => {
if (!blob) return;
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = input.filename;
link.click();
URL.revokeObjectURL(url);
}, 'image/png');
}