242 lines
12 KiB
Vue
242 lines
12 KiB
Vue
<template>
|
|
<div class="subpage">
|
|
<header class="subpage-bar">
|
|
<button type="button" class="subpage-back" @click="back">{{ t('chain.txBack') }}</button>
|
|
<h1>{{ t('chain.certificateView') }}</h1>
|
|
<button v-if="certificateRecord" type="button" class="des-download" :disabled="saving" @click="save">
|
|
{{ t('chain.certificateDownload') }}
|
|
</button>
|
|
</header>
|
|
<div class="subpage-body">
|
|
<p v-if="preview" class="subpage-note">{{ t('chain.certificatePreview') }}</p>
|
|
<template v-else-if="certificateError">
|
|
<p class="subpage-note" role="alert">{{ certificateError }}</p>
|
|
<button v-if="retryable" type="button" class="subpage-action" @click="load(true)">
|
|
{{ t('chain.certificateCheckAgain') }}
|
|
</button>
|
|
</template>
|
|
<template v-else-if="certificateRecord">
|
|
<p v-if="saveFailed" class="subpage-note" role="alert">{{ t('chain.certificateDownloadFailed') }}</p>
|
|
<section class="prove-des">
|
|
<h2 class="des-title">
|
|
{{ t('chain.certificateChainId') }}
|
|
<button type="button" class="des-tip" :aria-label="t('chain.certificateChainId')" @click.stop="toggleTip('tx')">
|
|
<TipIcon />
|
|
<span class="des-pop" :class="{ open: tip === 'tx' }" role="tooltip">{{ t('chain.certificateChainIdTip') }}</span>
|
|
</button>
|
|
</h2>
|
|
<p class="des-hash">{{ certificateRecord.txHash }}</p>
|
|
<h2 class="des-title">
|
|
{{ t('chain.certificateDigestTitle') }}
|
|
<button type="button" class="des-tip" :aria-label="t('chain.certificateDigestTitle')" @click.stop="toggleTip('digest')">
|
|
<TipIcon />
|
|
<span class="des-pop" :class="{ open: tip === 'digest' }" role="tooltip">{{ t('chain.certificateDigestTip') }}</span>
|
|
</button>
|
|
</h2>
|
|
<p class="des-digest">{{ certificateRecord.digest }}</p>
|
|
</section>
|
|
<div ref="stage" class="cert-stage" :style="{ height: `${720 * scale}px` }">
|
|
<div class="cert-fit" :style="{ transform: `scale(${scale})` }">
|
|
<trace-certificate-sheet ref="certificateSheet" :record="certificateRecord" />
|
|
</div>
|
|
</div>
|
|
</template>
|
|
<div v-else class="subpage-wait">
|
|
<div class="certificate-spinner" aria-hidden="true" />
|
|
<ol 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>
|
|
</div>
|
|
<div v-if="savedImage" class="save-view" @click="savedImage = ''">
|
|
<img :src="savedImage" alt="" />
|
|
<p v-if="saveHint" class="save-hint"><span>{{ t('chain.certificateSaveHint') }}</span></p>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { defineComponent, h, nextTick, onMounted, onUnmounted, ref } from 'vue';
|
|
import { useRoute, useRouter } from 'vue-router';
|
|
import { useI18n } from 'vue-i18n';
|
|
import { fetchTraceCertificate, requestTraceCertificate, type TraceCertificateRecord, type TraceCertificateResult } from '@/api';
|
|
import TraceCertificateSheet from '@/components/trace/TraceCertificateSheet.vue';
|
|
|
|
const TipIcon = defineComponent({
|
|
name: 'TipIcon',
|
|
setup() {
|
|
return () => h('svg', { viewBox: '0 0 1024 1024', width: 14, height: 14, 'aria-hidden': 'true' }, [
|
|
h('path', { fill: 'currentColor', d: 'M512 64a448 448 0 1 0 0 896 448 448 0 0 0 0-896zm0 820.48A372.48 372.48 0 1 1 512 139.52a372.48 372.48 0 0 1 0 744.96zM480 304h64v320h-64zm0 384h64v64h-64z' }),
|
|
]);
|
|
},
|
|
});
|
|
|
|
const route = useRoute();
|
|
const router = useRouter();
|
|
const { t } = useI18n();
|
|
const preview = route.name === 'trace-preview-certificate';
|
|
const code = String(route.params.code || '');
|
|
const certificateError = ref('');
|
|
const certificatePhase = ref(0);
|
|
const certificateRecord = ref<TraceCertificateRecord | null>(null);
|
|
const certificateSheet = ref<{ render: () => Promise<HTMLCanvasElement> }>();
|
|
const retryable = ref(true);
|
|
const certificateSteps = ['chain.certificateRetrieving', 'chain.certificateAcquiring', 'chain.certificatePreparing'];
|
|
const certificatePollMs = 5000;
|
|
const certificateWaitMs = 60_000;
|
|
const stage = ref<HTMLElement>();
|
|
const scale = ref(1);
|
|
const tip = ref('');
|
|
const saving = ref(false);
|
|
const saveFailed = ref(false);
|
|
const savedImage = ref('');
|
|
const saveHint = ref(false);
|
|
let certificateTimer: ReturnType<typeof setTimeout> | undefined;
|
|
let hintTimer: ReturnType<typeof setTimeout> | undefined;
|
|
let certificateRun = 0;
|
|
let certificatePolls = 0;
|
|
|
|
function back() {
|
|
if (window.history.state?.back) router.back();
|
|
else router.replace({ name: preview ? 'trace-preview' : 'trace', params: { code }, query: route.query });
|
|
}
|
|
function failCertificate(messageKey: string, canRetry = true) {
|
|
certificateError.value = t(messageKey);
|
|
retryable.value = canRetry;
|
|
}
|
|
async function showCertificateResult(result: TraceCertificateResult, run: number) {
|
|
if (run !== certificateRun) return;
|
|
if (result.status === 'READY' && result.certificate) {
|
|
certificatePhase.value = 2;
|
|
certificateRecord.value = result.certificate;
|
|
await nextTick();
|
|
measure();
|
|
return;
|
|
}
|
|
if (result.status === 'FAILED') {
|
|
// 只有请求从未发出的失败才能重新申请,其余失败重试也不会有结果
|
|
return result.retryable
|
|
? failCertificate('chain.certificateUnreachable')
|
|
: failCertificate('chain.certificateFailed', false);
|
|
}
|
|
if (result.status === 'UNKNOWN') return failCertificate('chain.certificateDelayed');
|
|
if (result.status === 'NONE') return failCertificate('chain.certificateUnavailable');
|
|
certificatePhase.value = 1;
|
|
if (++certificatePolls > certificateWaitMs / certificatePollMs) return failCertificate('chain.certificateDelayed');
|
|
certificateTimer = setTimeout(async () => {
|
|
try { await showCertificateResult(await fetchTraceCertificate(code), run); }
|
|
catch { if (run === certificateRun) failCertificate('chain.certificateUnavailable'); }
|
|
}, certificatePollMs);
|
|
}
|
|
async function load(retry = false) {
|
|
if (preview) return;
|
|
certificateRun += 1;
|
|
if (certificateTimer) clearTimeout(certificateTimer);
|
|
const run = certificateRun;
|
|
certificateError.value = '';
|
|
certificateRecord.value = null;
|
|
certificatePhase.value = 0;
|
|
certificatePolls = 0;
|
|
retryable.value = true;
|
|
try {
|
|
const cached = await fetchTraceCertificate(code);
|
|
if (run !== certificateRun) return;
|
|
certificatePhase.value = 1;
|
|
const result = retry || cached.status === 'NONE' || cached.status === 'UNKNOWN'
|
|
? await requestTraceCertificate(code) : cached;
|
|
await showCertificateResult(result, run);
|
|
} catch {
|
|
if (run === certificateRun) failCertificate('chain.certificateRequestFailed');
|
|
}
|
|
}
|
|
async function save() {
|
|
if (saving.value || !certificateSheet.value) return;
|
|
saving.value = true;
|
|
saveFailed.value = false;
|
|
try {
|
|
const canvas = await certificateSheet.value.render();
|
|
// 与天河链证书页相同:手机上不能直接下载文件,改为显示图片由用户长按保存
|
|
if (document.body.clientWidth <= 768) {
|
|
savedImage.value = canvas.toDataURL('image/jpeg');
|
|
saveHint.value = true;
|
|
if (hintTimer) clearTimeout(hintTimer);
|
|
hintTimer = setTimeout(() => (saveHint.value = false), 2500);
|
|
return;
|
|
}
|
|
const link = document.createElement('a');
|
|
link.href = canvas.toDataURL('image/png');
|
|
link.download = `${Date.now()}.png`;
|
|
document.body.appendChild(link);
|
|
link.click();
|
|
link.remove();
|
|
} catch {
|
|
saveFailed.value = true;
|
|
} finally {
|
|
saving.value = false;
|
|
}
|
|
}
|
|
function toggleTip(name: string) {
|
|
if (window.matchMedia('(hover: hover) and (pointer: fine)').matches) return;
|
|
tip.value = tip.value === name ? '' : name;
|
|
}
|
|
function measure() {
|
|
const width = stage.value?.clientWidth || 0;
|
|
scale.value = width > 0 ? Math.min(1, width / 508) : 1;
|
|
}
|
|
onMounted(() => {
|
|
load();
|
|
window.addEventListener('resize', measure);
|
|
});
|
|
onUnmounted(() => {
|
|
certificateRun += 1;
|
|
if (certificateTimer) clearTimeout(certificateTimer);
|
|
if (hintTimer) clearTimeout(hintTimer);
|
|
window.removeEventListener('resize', measure);
|
|
});
|
|
</script>
|
|
|
|
<style scoped>
|
|
.subpage { max-width: 560px; margin: 0 auto; min-height: 100vh; background: var(--bg); }
|
|
.subpage-bar { position: sticky; top: 0; z-index: 8; display: flex; align-items: center; gap: 10px; min-height: 52px; padding: 8px 12px; background: var(--card-bg); border-bottom: 1px solid var(--border); }
|
|
.subpage-bar h1 { flex: 1; min-width: 0; margin: 0; font-size: 16px; font-weight: 600; text-align: left; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
.subpage-back, .subpage-action { border: 0; background: transparent; color: var(--brand-2); font: inherit; font-size: 14px; cursor: pointer; padding: 6px 0; }
|
|
.subpage-action { background: var(--brand); color: #fff; border-radius: 6px; padding: 8px 12px; }
|
|
.subpage-body { padding: 12px 12px 28px; }
|
|
.subpage-note { margin: 12px 0; font-size: 14px; line-height: 1.6; }
|
|
.prove-des { margin: 4px 0 16px; text-align: left; }
|
|
.des-title { display: flex; align-items: center; margin: 20px 0 12px; padding: 0 8px; border-left: 3px solid #2977f0; font-family: PingFang-SC-Bold, 'PingFang SC', 'Microsoft YaHei', sans-serif; font-size: 14px; font-weight: 700; line-height: 20px; color: var(--text); letter-spacing: 0.14px; }
|
|
.des-title:first-child { margin-top: 0; }
|
|
.des-tip { position: relative; display: inline-flex; margin-left: 4px; padding: 0; border: 0; background: transparent; color: inherit; cursor: pointer; }
|
|
.des-pop { display: none; position: absolute; left: calc(100% + 8px); top: 50%; z-index: 6; width: 200px; transform: translateY(-50%); box-sizing: border-box; padding: 12px; border: 1px solid #ebeef5; border-radius: 4px; background: #fff; color: #606266; font-family: PingFangSC-Regular, 'PingFang SC', 'Microsoft YaHei', sans-serif; font-size: 14px; font-weight: 400; line-height: 1.4; letter-spacing: 0; text-align: left; box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1); }
|
|
.des-pop.open { display: block; }
|
|
@media (hover: hover) and (pointer: fine) {
|
|
.des-tip:hover .des-pop, .des-tip:focus-visible .des-pop { display: block; }
|
|
}
|
|
.des-hash, .des-digest { margin: 0 0 20px; font-family: PingFangSC-Regular, 'PingFang SC', 'Microsoft YaHei', sans-serif; font-size: 14px; font-weight: 400; line-height: normal; letter-spacing: 0.14px; text-align: left; word-break: break-all; }
|
|
.des-hash { margin-bottom: 0; color: #2977f0; }
|
|
.des-digest { color: var(--muted); }
|
|
.des-download { flex: none; min-width: 112px; height: 36px; padding: 6px 16px; border: 1px solid #2977f0; border-radius: 4px; background: var(--card-bg); color: #2977f0; font-family: PingFangSC-Regular, 'PingFang SC', 'Microsoft YaHei', sans-serif; font-size: 14px; font-weight: 500; line-height: 14px; cursor: pointer; }
|
|
.des-download:hover { background: rgba(41, 119, 240, 0.06); }
|
|
.des-download:disabled { opacity: 0.6; cursor: default; }
|
|
@media (max-width: 420px) {
|
|
.des-pop { left: 0; top: calc(100% + 6px); transform: none; }
|
|
}
|
|
.cert-stage { position: relative; width: 100%; margin: 0 auto; }
|
|
.cert-fit { position: absolute; left: 50%; top: 0; width: 508px; margin-left: -254px; transform-origin: top center; }
|
|
.save-view { position: fixed; inset: 0; z-index: 2048; display: flex; overflow: hidden; background: #303030; }
|
|
.save-view img { display: block; max-width: 100%; max-height: 100%; margin: auto; }
|
|
.save-hint { position: absolute; left: 0; bottom: 120px; width: 100%; margin: 0; text-align: center; pointer-events: none; }
|
|
.save-hint span { padding: 4px 8px; border-radius: 4px; background: rgba(90, 90, 90, 0.4); color: #fff; font-size: 20px; }
|
|
.subpage-wait { padding: 12px 4px 24px; }
|
|
.certificate-spinner { width: 28px; height: 28px; border: 3px solid var(--border); border-top-color: var(--brand); border-radius: 50%; margin: 0 auto 24px; animation: certificate-spin .8s linear infinite; }
|
|
.certificate-steps { list-style: none; padding: 0; margin: 0; display: grid; gap: 18px; font-size: 14px; }
|
|
.certificate-steps li { display: flex; gap: 12px; align-items: center; color: var(--muted); }
|
|
.certificate-steps li span { border: 1px solid var(--border); border-radius: 50%; width: 24px; height: 24px; display: grid; place-items: center; flex-shrink: 0; }
|
|
.certificate-steps li.current { color: var(--text); font-weight: 600; }
|
|
.certificate-steps li.done { color: var(--brand); }
|
|
@keyframes certificate-spin { to { transform: rotate(360deg); } }
|
|
@media (prefers-reduced-motion: reduce) { .certificate-spinner { animation: none; } }
|
|
</style>
|