feat: 优化消费者出证进度弹窗和证书文本

This commit is contained in:
Nixevol
2026-09-20 02:14:39 +08:00
parent c4864992e8
commit c5c0168e4c
9 changed files with 123 additions and 136 deletions
@@ -65,23 +65,37 @@
target="_blank"
rel="noopener noreferrer"
>
{{ t('chain.explorer') }} →
{{ 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 ref="certificateButton" v-if="canViewCertificate" class="chain-link certificate-link" :disabled="opening || preview" @click="viewCertificate">
{{ t('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>
<Teleport to="body">
<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">
<p v-if="certificateError" role="alert">{{ certificateError }}</p>
<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>
</dialog>
</Teleport>
</template>
<script setup lang="ts">
import { computed, ref } from 'vue';
import { useRouter } from 'vue-router';
import { fetchTraceCertificate, requestTraceCertificate } from '@/api';
import { computed, onUnmounted, ref, watch } from 'vue';
import { fetchTraceCertificate, requestTraceCertificate, type TraceCertificateResult } from '@/api';
import { useI18n } from 'vue-i18n';
import dayjs from 'dayjs';
import { safeTraceUrl } from './media';
@@ -114,33 +128,76 @@ const props = defineProps<{
}>();
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 === '天河链');
const canViewCertificate = computed(() => props.blockchain?.status === 'ONCHAIN' && Boolean(props.blockchain.txHash) && props.blockchain.chainName === '天河链');
const certificateDialog = ref<HTMLDialogElement>();
const certificateButton = ref<HTMLButtonElement>();
const certificatePhase = ref(0);
const retryable = ref(true);
const certificateSteps = ['chain.certificateRetrieving', 'chain.certificateAcquiring', 'chain.certificateRedirecting'];
let certificateTimer: ReturnType<typeof setTimeout> | undefined;
let certificateRun = 0;
let certificatePolls = 0;
function stopCertificateWait() {
certificateRun++;
if (certificateTimer) clearTimeout(certificateTimer);
opening.value = false;
}
function closeCertificateDialog() {
stopCertificateWait();
certificateDialog.value?.close();
certificateButton.value?.focus();
}
function failCertificate(messageKey: string, canRetry = true) {
certificateError.value = t(messageKey);
retryable.value = canRetry;
opening.value = false;
}
function showCertificateResult(result: TraceCertificateResult, run: number, code: string) {
if (run !== certificateRun) return;
if (result.status === 'READY' && result.url) {
certificatePhase.value = 2;
certificateTimer = setTimeout(() => {
if (run === certificateRun) window.location.assign(result.url!);
}, 350);
return;
}
if (result.status === 'FAILED') return 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 >= 60) return failCertificate('chain.certificateDelayed');
certificateTimer = setTimeout(async () => {
try { showCertificateResult(await fetchTraceCertificate(code), run, code); }
catch { if (run === certificateRun) failCertificate('chain.certificateUnavailable'); }
}, 5000);
}
async function viewCertificate() {
if (opening.value || props.preview) return;
if (opening.value || props.preview || !canViewCertificate.value) return;
stopCertificateWait();
const run = certificateRun;
const code = props.shortCode;
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;
certificatePhase.value = 0;
certificatePolls = 0;
if (!certificateDialog.value?.open) certificateDialog.value?.showModal();
opening.value = true;
try {
const cached = await fetchTraceCertificate(props.shortCode);
const cached = await fetchTraceCertificate(code);
if (run !== certificateRun) return;
certificatePhase.value = 1;
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);
}
? await requestTraceCertificate(code) : cached;
showCertificateResult(result, run, code);
} catch {
if (popup && !popup.closed) popup.close();
certificateError.value = t('chain.certificateRequestFailed');
} finally { opening.value = false; }
if (run === certificateRun) failCertificate('chain.certificateRequestFailed');
}
}
watch(() => props.shortCode, closeCertificateDialog);
onUnmounted(stopCertificateWait);
const status = computed<ProofStatus>(() => {
const value = props.blockchain?.status as ProofStatus | undefined;
@@ -253,6 +310,19 @@ function formatTime(value: string) {
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 { 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::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; }
.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); }
.certificate-retry { border: 0; background: var(--brand); color: white; padding: 9px 18px; border-radius: 6px; cursor: pointer; font: inherit; }
@keyframes certificate-spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .certificate-spinner { animation: none; } }
</style>