- Add tailwindcss v4 + @tailwindcss/vite to mall, shop-admin, admin - Add @vmall/shared/theme.css tokens with html[data-accent] presets - Add @vmall/ui kit (VBtn/VBadge/VField/VInput/VCard/VPanel/VTable/VPage, VAccentSwatch, useAccent) as a Nuxt module - Convert all three apps to kit + utilities; delete ui.css/mall.css and every <style scoped>; consoles get accent presets, mall locked to red - Fix VCard boolean prop default (padding) and PDP/store stale useAsyncData keys on param navigation - Archive adopt-tailwind-design-system; new frontend-ui capability spec
120 lines
5.7 KiB
Vue
120 lines
5.7 KiB
Vue
<script setup lang="ts">
|
|
import { t as pick } from "@vmall/shared";
|
|
import type { PublicFlashSaleItem, PublicFlashSaleSession } from "@vmall/shared";
|
|
|
|
const { locale, t } = useI18n();
|
|
const { $api } = useNuxtApp();
|
|
const cart = useCartStore();
|
|
const sessions = ref<PublicFlashSaleSession[]>([]);
|
|
const activeSessionIndex = ref(0);
|
|
const remainingSeconds = ref(0);
|
|
const loading = ref(true);
|
|
const addingSkuId = ref("");
|
|
const error = ref("");
|
|
let countdownTimer: ReturnType<typeof setInterval> | undefined;
|
|
|
|
const breadcrumb = computed(() => [
|
|
{ label: t("stores.breadcrumbHome"), to: "/" },
|
|
{ label: t("marketing.seckillBreadcrumb") },
|
|
]);
|
|
|
|
const activeSession = computed<PublicFlashSaleSession | null>(
|
|
() => sessions.value[activeSessionIndex.value] ?? sessions.value[0] ?? null,
|
|
);
|
|
|
|
function updateCountdown(): void {
|
|
const session = activeSession.value;
|
|
if (!session) {
|
|
remainingSeconds.value = 0;
|
|
return;
|
|
}
|
|
remainingSeconds.value = Math.max(
|
|
0,
|
|
Math.ceil((new Date(session.ends_at).getTime() - Date.now()) / 1000),
|
|
);
|
|
}
|
|
|
|
function formatCountdown(seconds: number): string {
|
|
const h = Math.floor(seconds / 3600);
|
|
const m = Math.floor((seconds % 3600) / 60);
|
|
const s = seconds % 60;
|
|
const p = (n: number): string => String(n).padStart(2, "0");
|
|
return `${p(h)}:${p(m)}:${p(s)}`;
|
|
}
|
|
|
|
function soldPct(item: PublicFlashSaleItem): number {
|
|
const total = item.sold_count + item.reserved_stock;
|
|
if (total <= 0) return 100;
|
|
return Math.round((item.sold_count / total) * 100);
|
|
}
|
|
|
|
// The listing price is display only: the shopper adds the SKU to the cart and
|
|
// checkout resolves the authoritative price server-side.
|
|
async function addToCart(item: PublicFlashSaleItem): Promise<void> {
|
|
addingSkuId.value = item.sku_id;
|
|
error.value = "";
|
|
try {
|
|
await $api.addCartItem(item.sku_id, 1);
|
|
await cart.refresh();
|
|
} catch {
|
|
error.value = t("marketing.grabFailed");
|
|
} finally {
|
|
addingSkuId.value = "";
|
|
}
|
|
}
|
|
|
|
async function load(): Promise<void> {
|
|
loading.value = true;
|
|
try {
|
|
sessions.value = await $api.listFlashSales();
|
|
activeSessionIndex.value = 0;
|
|
updateCountdown();
|
|
} catch {
|
|
sessions.value = [];
|
|
} finally {
|
|
loading.value = false;
|
|
}
|
|
}
|
|
|
|
onMounted(async () => {
|
|
await load();
|
|
countdownTimer = setInterval(updateCountdown, 1000);
|
|
});
|
|
|
|
onBeforeUnmount(() => {
|
|
if (countdownTimer) clearInterval(countdownTimer);
|
|
});
|
|
</script>
|
|
|
|
<template>
|
|
<div class="min-h-screen bg-bg pb-14 font-sans text-text">
|
|
<UiBreadcrumb :items="breadcrumb" />
|
|
<section class="mx-auto max-w-mall bg-surface px-4">
|
|
<header class="border border-border border-b-0 bg-bg px-4 py-3.5"><h1 class="m-0 border-l-[3px] border-primary pl-2.5 text-[17px] font-medium">{{ t("marketing.seckillTitle") }}</h1></header>
|
|
<div class="grid grid-cols-2 border border-border border-b-0 sm:grid-cols-4" role="tablist">
|
|
<button v-for="(session, index) in sessions" :key="session.id" type="button" class="flex min-h-[70px] cursor-pointer flex-col items-center justify-center border-0 border-r border-border bg-surface px-2 text-sm last:border-r-0" :class="index === activeSessionIndex ? 'bg-primary-soft text-primary' : ''" role="tab" :aria-selected="index === activeSessionIndex" @click="activeSessionIndex = index; updateCountdown()">
|
|
<strong>{{ pick(session.label, locale) }}</strong><span class="text-xs text-muted">{{ index === activeSessionIndex ? t("marketing.currentSession") : t("marketing.upcoming") }}</span>
|
|
</button>
|
|
</div>
|
|
<p v-if="activeSession" class="m-0 border border-border bg-bg px-4 py-3 text-center text-lg font-semibold text-primary" aria-live="polite">{{ t("marketing.countdown", { time: formatCountdown(remainingSeconds) }) }}</p>
|
|
<p v-if="error" class="my-4 border border-danger/30 bg-danger/10 px-3.5 py-2.5 text-danger" role="alert">{{ error }}</p>
|
|
<div v-if="loading" class="py-6 text-muted">{{ t("common.loading") }}</div>
|
|
<div v-else-if="activeSession && activeSession.items.length" class="grid gap-4 py-5 [grid-template-columns:repeat(auto-fill,minmax(220px,1fr))]">
|
|
<VCard v-for="item in activeSession.items" :key="item.id" :padded="false" class="min-w-0 overflow-hidden">
|
|
<NuxtLink :to="`/goods/${item.product_slug}`" class="flex h-52 items-center justify-center bg-bg"><img class="h-full w-full object-contain" :src="item.image || '/mock/product-1.svg'" :alt="pick(item.product_name, locale)" loading="lazy" /></NuxtLink>
|
|
<div class="p-3.5">
|
|
<NuxtLink :to="`/goods/${item.product_slug}`" class="line-clamp-2 font-medium hover:text-primary">{{ pick(item.product_name, locale) }}</NuxtLink>
|
|
<p class="my-2 flex items-center gap-2"><span class="text-lg font-semibold text-primary"><PriceText :amount-minor="item.sale_price_minor" :currency="item.currency" /></span><s class="text-sm text-muted"><PriceText :amount-minor="item.original_price_minor" :currency="item.original_currency" /></s></p>
|
|
<div class="flex items-center gap-2 text-xs text-muted"><div class="h-2 flex-1 overflow-hidden rounded-full bg-bg"><span class="block h-full bg-primary" :style="{ width: `${soldPct(item)}%` }"></span></div><span>{{ t("marketing.progress", { n: soldPct(item) }) }}</span></div>
|
|
<p class="my-2 text-sm text-muted">{{ t("marketing.stock", { n: item.reserved_stock }) }}</p>
|
|
<VBtn class="w-full" variant="primary" type="button" :disabled="item.reserved_stock <= 0 || addingSkuId === item.sku_id" @click="addToCart(item)">{{ addingSkuId === item.sku_id ? t("common.loading") : t("marketing.buyNow") }}</VBtn>
|
|
</div>
|
|
</VCard>
|
|
</div>
|
|
<UiEmptyState v-else :text="t('marketing.noSeckillProducts')" />
|
|
</section>
|
|
</div>
|
|
</template>
|
|
|
|
|