Files
Chengdong Zhang 0d0e10b97b feat(ui): adopt Tailwind v4 design system and archive change
- 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
2026-09-22 18:22:50 +08:00

328 lines
13 KiB
Vue

<script setup lang="ts">
import type {
Currency,
ShopFlashSaleItem,
ShopFlashSaleSession,
} from "@vmall/shared";
definePageMeta({ middleware: "auth" });
const { $api } = useNuxtApp();
const { locale, t: translate } = useI18n();
const { load: loadMoney, fmt } = useMoney();
const sessions = ref<ShopFlashSaleSession[]>([]);
const currencies = ref<Currency[]>([]);
const skuOptions = ref<{ id: string; label: string }[]>([]);
const loading = ref(true);
const saving = ref(false);
const actionId = ref("");
const error = ref("");
const message = ref("");
const editingSessionId = ref("");
const editingItemId = ref("");
const itemSessionId = ref("");
const sessionForm = reactive({
labelEn: "",
labelZh: "",
startsAt: "",
endsAt: "",
enabled: true,
});
const itemForm = reactive({
skuId: "",
salePriceMinor: 0,
currency: "USD",
reservedStock: 0,
perCustomerLimit: 1,
});
function toLocalInput(iso: string): string {
const d = new Date(iso);
const pad = (n: number) => String(n).padStart(2, "0");
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(d.getHours())}:${pad(d.getMinutes())}`;
}
function resetSessionForm(): void {
const now = new Date();
const later = new Date(now.getTime() + 7 * 24 * 3600 * 1000);
editingSessionId.value = "";
sessionForm.labelEn = "";
sessionForm.labelZh = "";
sessionForm.startsAt = toLocalInput(now.toISOString());
sessionForm.endsAt = toLocalInput(later.toISOString());
sessionForm.enabled = true;
}
function resetItemForm(): void {
editingItemId.value = "";
itemForm.skuId = skuOptions.value[0]?.id ?? "";
itemForm.salePriceMinor = 0;
itemForm.currency =
currencies.value.find((c) => c.is_base)?.code ?? currencies.value[0]?.code ?? "USD";
itemForm.reservedStock = 0;
itemForm.perCustomerLimit = 1;
}
async function load(): Promise<void> {
loading.value = true;
error.value = "";
try {
sessions.value = await $api.shop.listFlashSales();
} catch (err: unknown) {
error.value = err instanceof Error ? err.message : translate("common.error");
} finally {
loading.value = false;
}
}
async function loadCatalog(): Promise<void> {
try {
const page = await $api.shop.listMyProducts({ per_page: 100 });
skuOptions.value = page.items.flatMap((product) =>
(product.skus ?? []).map((sku) => ({
id: sku.id,
label: `${product.name[locale.value] ?? product.name.en ?? product.slug} · ${sku.sku_code}`,
})),
);
} catch {
skuOptions.value = [];
}
}
async function loadCurrencies(): Promise<void> {
try {
currencies.value = (await $api.listCurrencies()).filter((c) => c.enabled);
} catch {
currencies.value = [];
}
}
async function saveSession(): Promise<void> {
error.value = "";
message.value = "";
if (!sessionForm.labelEn.trim() || !sessionForm.labelZh.trim()) {
error.value = translate("shop.flashLabelRequired");
return;
}
if (new Date(sessionForm.endsAt) < new Date(sessionForm.startsAt)) {
error.value = translate("shop.flashWindowInvalid");
return;
}
const body = {
label: { en: sessionForm.labelEn.trim(), zh: sessionForm.labelZh.trim() },
starts_at: new Date(sessionForm.startsAt).toISOString(),
ends_at: new Date(sessionForm.endsAt).toISOString(),
enabled: sessionForm.enabled,
};
saving.value = true;
try {
if (editingSessionId.value) await $api.shop.updateFlashSale(editingSessionId.value, body);
else await $api.shop.createFlashSale(body);
message.value = translate("shop.flashSaleSaved");
resetSessionForm();
await load();
} catch (err: unknown) {
error.value = err instanceof Error ? err.message : translate("common.error");
} finally {
saving.value = false;
}
}
function editSession(session: ShopFlashSaleSession): void {
editingSessionId.value = session.id;
sessionForm.labelEn = session.label.en ?? "";
sessionForm.labelZh = session.label.zh ?? "";
sessionForm.startsAt = toLocalInput(session.starts_at);
sessionForm.endsAt = toLocalInput(session.ends_at);
sessionForm.enabled = session.enabled;
}
async function removeSession(session: ShopFlashSaleSession): Promise<void> {
actionId.value = session.id;
error.value = "";
try {
await $api.shop.deleteFlashSale(session.id);
await load();
} catch (err: unknown) {
error.value = err instanceof Error ? err.message : translate("common.error");
} finally {
actionId.value = "";
}
}
function startAddItem(session: ShopFlashSaleSession): void {
itemSessionId.value = session.id;
resetItemForm();
}
function editItem(session: ShopFlashSaleSession, item: ShopFlashSaleItem): void {
itemSessionId.value = session.id;
editingItemId.value = item.id;
itemForm.skuId = item.sku_id;
itemForm.salePriceMinor = item.sale_price_minor;
itemForm.currency = item.currency;
itemForm.reservedStock = item.reserved_stock;
itemForm.perCustomerLimit = item.per_customer_limit;
}
async function saveItem(session: ShopFlashSaleSession): Promise<void> {
error.value = "";
message.value = "";
if (!itemForm.skuId) {
error.value = translate("shop.flashSkuRequired");
return;
}
if (!(Number(itemForm.salePriceMinor) > 0)) {
error.value = translate("shop.flashPriceInvalid");
return;
}
if (Number(itemForm.reservedStock) < 0) {
error.value = translate("shop.flashStockInvalid");
return;
}
if (!(Number(itemForm.perCustomerLimit) > 0)) {
error.value = translate("shop.flashLimitInvalid");
return;
}
const body = {
sku_id: itemForm.skuId,
sale_price_minor: Number(itemForm.salePriceMinor),
currency: itemForm.currency,
reserved_stock: Number(itemForm.reservedStock),
per_customer_limit: Number(itemForm.perCustomerLimit),
};
saving.value = true;
try {
if (editingItemId.value) await $api.shop.updateFlashSaleItem(editingItemId.value, body);
else await $api.shop.addFlashSaleItem(session.id, body);
message.value = translate("shop.flashItemSaved");
resetItemForm();
itemSessionId.value = "";
await load();
} catch (err: unknown) {
error.value = err instanceof Error ? err.message : translate("common.error");
} finally {
saving.value = false;
}
}
async function removeItem(item: ShopFlashSaleItem): Promise<void> {
actionId.value = item.id;
error.value = "";
try {
await $api.shop.deleteFlashSaleItem(item.id);
await load();
} catch (err: unknown) {
error.value = err instanceof Error ? err.message : translate("common.error");
} finally {
actionId.value = "";
}
}
onMounted(async () => {
loadMoney();
await Promise.all([loadCurrencies(), loadCatalog()]);
resetSessionForm();
resetItemForm();
await load();
});
</script>
<template>
<VPage :title="$t('shop.flashSaleList')">
<div v-if="error" class="my-2 text-sm text-danger" role="alert">{{ error }}</div>
<div v-if="message" class="my-2 text-sm text-muted" role="status">{{ message }}</div>
<VCard class="mb-5">
<h2 class="mb-4 text-base font-semibold">{{ editingSessionId ? $t("shop.editFlashSale") : $t("shop.newFlashSale") }}</h2>
<div class="my-3 grid gap-3 [grid-template-columns:repeat(auto-fit,minmax(220px,1fr))]">
<VField :label="$t('shop.flashLabelEn')"><VInput v-model="sessionForm.labelEn" type="text" /></VField>
<VField :label="$t('shop.flashLabelZh')"><VInput v-model="sessionForm.labelZh" type="text" /></VField>
<VField :label="$t('shop.flashStartsAt')"><VInput v-model="sessionForm.startsAt" type="datetime-local" /></VField>
<VField :label="$t('shop.flashEndsAt')"><VInput v-model="sessionForm.endsAt" type="datetime-local" /></VField>
<label class="flex items-center gap-2 text-sm font-medium text-text"><input v-model="sessionForm.enabled" type="checkbox" class="h-4 w-4 accent-primary" /><span>{{ $t("shop.flashEnabled") }}</span></label>
</div>
<div class="flex gap-2">
<VBtn variant="primary" :disabled="saving" @click="saveSession">{{ editingSessionId ? $t("shop.updateFlashSale") : $t("shop.createFlashSale") }}</VBtn>
<VBtn v-if="editingSessionId" type="button" @click="resetSessionForm">{{ $t("common.cancel") }}</VBtn>
</div>
</VCard>
<p v-if="loading" class="text-muted">{{ $t("common.loading") }}</p>
<VCard v-else-if="!sessions.length" class="text-muted">{{ $t("common.empty") }}</VCard>
<template v-else>
<VCard v-for="session in sessions" :key="session.id" class="mb-4">
<header class="flex flex-wrap items-center justify-between gap-3">
<div class="flex flex-wrap items-center gap-2">
<strong>{{ session.label[locale] ?? session.label.en }}</strong>
<span class="text-sm text-muted">{{ session.starts_at.slice(0, 10) }} {{ session.ends_at.slice(0, 10) }}</span>
<VBadge :tone="session.enabled ? 'green' : 'red'">{{ session.enabled ? $t("shop.enabledOn") : $t("shop.enabledOff") }}</VBadge>
</div>
<div class="flex gap-1.5">
<VBtn size="sm" :disabled="actionId === session.id" @click="editSession(session)">{{ $t("shop.edit") }}</VBtn>
<VBtn size="sm" :disabled="actionId === session.id" @click="startAddItem(session)">{{ $t("shop.newFlashItem") }}</VBtn>
<VBtn size="sm" :disabled="actionId === session.id" @click="removeSession(session)">{{ $t("common.delete") }}</VBtn>
</div>
</header>
<h3 class="mb-2 mt-4 text-sm font-semibold">{{ $t("shop.flashItems") }}</h3>
<div v-if="!session.items.length" class="text-muted">{{ $t("shop.noFlashItems") }}</div>
<VTable v-else>
<thead>
<tr>
<th>{{ $t("shop.flashSku") }}</th>
<th>{{ $t("shop.flashSalePrice") }}</th>
<th>{{ $t("shop.flashOriginalPrice") }}</th>
<th>{{ $t("shop.flashRemaining") }}</th>
<th>{{ $t("shop.flashSold") }}</th>
<th>{{ $t("shop.flashPerCustomerLimit") }}</th>
<th>{{ $t("common.actions") }}</th>
</tr>
</thead>
<tbody>
<tr v-for="item in session.items" :key="item.id">
<td>{{ item.sku_code }}</td>
<td>{{ fmt(item.sale_price_minor, item.currency) }}</td>
<td>{{ fmt(item.original_price_minor, item.original_currency) }}</td>
<td>{{ item.reserved_stock }}</td>
<td>{{ item.sold_count }}</td>
<td>{{ item.per_customer_limit }}</td>
<td class="flex gap-1.5">
<VBtn size="sm" :disabled="actionId === item.id" @click="editItem(session, item)">{{ $t("shop.edit") }}</VBtn>
<VBtn size="sm" :disabled="actionId === item.id" @click="removeItem(item)">{{ $t("common.delete") }}</VBtn>
</td>
</tr>
</tbody>
</VTable>
<div v-if="itemSessionId === session.id" class="mt-4 border-t border-border pt-3">
<h3 class="mb-2 text-sm font-semibold">{{ editingItemId ? $t("shop.editFlashItem") : $t("shop.newFlashItem") }}</h3>
<div class="my-3 grid gap-3 [grid-template-columns:repeat(auto-fit,minmax(220px,1fr))]">
<VField :label="$t('shop.flashSku')">
<select v-model="itemForm.skuId" class="w-full rounded-md border border-border bg-surface px-3 py-2 text-sm text-text focus:border-primary focus:outline-2 focus:outline-primary/30">
<option value="">{{ $t("shop.flashSelectSku") }}</option>
<option v-for="option in skuOptions" :key="option.id" :value="option.id">{{ option.label }}</option>
</select>
</VField>
<VField :label="$t('shop.flashSalePrice')"><VInput v-model.number="itemForm.salePriceMinor" type="number" min="1" step="1" /></VField>
<VField :label="$t('shop.couponCurrency')">
<select v-model="itemForm.currency" class="w-full rounded-md border border-border bg-surface px-3 py-2 text-sm text-text focus:border-primary focus:outline-2 focus:outline-primary/30">
<option v-for="c in currencies" :key="c.code" :value="c.code">{{ c.code }}</option>
</select>
</VField>
<VField :label="$t('shop.flashReservedStock')"><VInput v-model.number="itemForm.reservedStock" type="number" min="0" step="1" /></VField>
<VField :label="$t('shop.flashPerCustomerLimit')"><VInput v-model.number="itemForm.perCustomerLimit" type="number" min="1" step="1" /></VField>
</div>
<div class="flex gap-2">
<VBtn variant="primary" :disabled="saving" @click="saveItem(session)">{{ $t("shop.saveFlashItem") }}</VBtn>
<VBtn type="button" @click="itemSessionId = ''; resetItemForm()">{{ $t("common.cancel") }}</VBtn>
</div>
</div>
</VCard>
</template>
</VPage>
</template>