Files
mp-pilates/packages/app/src/components/FlashSaleSection.vue
2026-04-09 11:31:12 +08:00

439 lines
9.9 KiB
Vue

<template>
<view v-if="flashSales.length" class="flash-sale-section">
<!-- Section header -->
<view class="section-header">
<view class="header-left">
<view class="flash-icon-wrap">
<view class="flash-icon-clock" />
</view>
<text class="section-title">限时秒杀</text>
<view v-if="hasOngoing" class="live-dot" />
</view>
</view>
<!-- Horizontal scroll cards -->
<scroll-view
scroll-x
:show-scrollbar="false"
class="flash-scroll"
>
<view class="flash-card-list">
<view
v-for="sale in flashSales"
:key="sale.id"
class="flash-card"
:class="cardPhaseClass(sale.phase)"
@tap="goToDetail(sale.id)"
>
<!-- Top gradient band -->
<view class="card-top">
<!-- Phase badge -->
<view class="phase-badge" :class="badgeClass(sale.phase)">
<text class="phase-badge-text">{{ phaseLabel(sale.phase) }}</text>
</view>
<!-- Countdown / status text -->
<view class="countdown-row">
<text v-if="sale.phase === FlashSalePhase.UPCOMING" class="countdown-label">距开始</text>
<text v-else-if="sale.phase === FlashSalePhase.ONGOING" class="countdown-label">剩余</text>
<view v-if="sale.phase === FlashSalePhase.UPCOMING || sale.phase === FlashSalePhase.ONGOING" class="countdown-blocks">
<text class="cd-block">{{ getSaleCountdown(sale).h }}</text>
<text class="cd-sep">:</text>
<text class="cd-block">{{ getSaleCountdown(sale).m }}</text>
<text class="cd-sep">:</text>
<text class="cd-block">{{ getSaleCountdown(sale).s }}</text>
</view>
</view>
</view>
<!-- Card body -->
<view class="card-body">
<text class="card-title">{{ sale.title }}</text>
<text class="card-type-name">{{ sale.cardType.name }}</text>
<!-- Price area -->
<view class="price-area">
<view class="flash-price-row">
<text class="flash-currency">¥</text>
<text class="flash-price">{{ formatPrice(sale.flashPrice) }}</text>
</view>
<text class="original-price">¥{{ formatPrice(sale.originalPrice) }}</text>
</view>
<!-- Stock progress -->
<view class="stock-area">
<view class="stock-bar">
<view
class="stock-fill"
:class="{ 'stock-fill--hot': getStockRatio(sale.soldCount, sale.totalStock) > 0.6 }"
:style="{ width: stockPercent(sale) }"
/>
</view>
<text class="stock-text">
{{ sale.phase === FlashSalePhase.SOLD_OUT ? '已售罄' : `${sale.remainingStock}/${sale.totalStock}` }}
</text>
</view>
</view>
</view>
</view>
</scroll-view>
</view>
</template>
<script setup lang="ts">
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { FlashSalePhase } from '@mp-pilates/shared'
import type { FlashSaleListItem } from '@mp-pilates/shared'
import { formatPrice, getFlashSalePhaseLabel, getCountdownParts, getStockRatio, getStockPercent } from '../utils/format'
import { get } from '../utils/request'
const flashSales = ref<FlashSaleListItem[]>([])
const tick = ref(0)
let timer: ReturnType<typeof setInterval> | null = null
const hasOngoing = computed(() =>
flashSales.value.some((s) => s.phase === FlashSalePhase.ONGOING),
)
async function fetchFlashSales() {
try {
const data = await get<FlashSaleListItem[]>('/flash-sales')
flashSales.value = [...data]
} catch {
flashSales.value = []
}
}
// Expose for parent page refresh
defineExpose({ fetchFlashSales })
function phaseLabel(phase: FlashSalePhase): string {
return getFlashSalePhaseLabel(phase)
}
function cardPhaseClass(phase: FlashSalePhase): string {
if (phase === FlashSalePhase.ONGOING) return 'card--ongoing'
if (phase === FlashSalePhase.UPCOMING) return 'card--upcoming'
if (phase === FlashSalePhase.SOLD_OUT) return 'card--soldout'
return 'card--ended'
}
function badgeClass(phase: FlashSalePhase): string {
if (phase === FlashSalePhase.ONGOING) return 'badge--ongoing'
if (phase === FlashSalePhase.UPCOMING) return 'badge--upcoming'
return 'badge--inactive'
}
function stockPercent(sale: FlashSaleListItem): string {
return getStockPercent(sale.soldCount, sale.totalStock)
}
function getSaleCountdown(sale: FlashSaleListItem) {
void tick.value
const target = sale.phase === FlashSalePhase.UPCOMING ? sale.startTime : sale.endTime
return getCountdownParts(target)
}
function goToDetail(id: string) {
uni.navigateTo({ url: `/pages/flash-sale/detail?id=${id}` })
}
onMounted(() => {
fetchFlashSales()
timer = setInterval(() => {
tick.value++
}, 1000)
})
onUnmounted(() => {
if (timer) {
clearInterval(timer)
timer = null
}
})
</script>
<style lang="scss" scoped>
.flash-sale-section {
background: #fff;
margin: 16rpx 24rpx 0;
padding-bottom: 24rpx;
border-radius: 20rpx;
overflow: hidden;
box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
}
/* ── Section header ── */
.section-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 28rpx 32rpx 16rpx;
}
.header-left {
display: flex;
align-items: center;
gap: 12rpx;
}
.flash-icon-wrap {
width: 44rpx;
height: 44rpx;
border-radius: 12rpx;
background: linear-gradient(135deg, #D4A59A, #C08B7E);
display: flex;
align-items: center;
justify-content: center;
}
/* CSS-drawn clock icon */
.flash-icon-clock {
width: 24rpx;
height: 24rpx;
border: 3rpx solid #fff;
border-radius: 50%;
position: relative;
&::before {
content: '';
position: absolute;
top: 50%;
left: 50%;
width: 2rpx;
height: 9rpx;
background: #fff;
transform-origin: bottom center;
transform: translate(-50%, -100%) rotate(0deg);
border-radius: 1rpx;
}
&::after {
content: '';
position: absolute;
top: 50%;
left: 50%;
width: 2rpx;
height: 7rpx;
background: #fff;
transform-origin: bottom center;
transform: translate(-50%, -100%) rotate(90deg);
border-radius: 1rpx;
}
}
.section-title {
font-size: 36rpx;
font-weight: 700;
color: #222;
}
.live-dot {
width: 12rpx;
height: 12rpx;
border-radius: 50%;
background: #C08B7E;
animation: pulse 1.5s ease infinite;
}
@keyframes pulse {
0%, 100% { opacity: 1; transform: scale(1); }
50% { opacity: 0.4; transform: scale(0.8); }
}
/* ── Horizontal scroll ── */
.flash-scroll {
padding-left: 32rpx;
white-space: nowrap;
}
.flash-card-list {
display: inline-flex;
gap: 20rpx;
padding-right: 32rpx;
}
/* ── Flash card ── */
.flash-card {
width: 340rpx;
border-radius: 20rpx;
overflow: hidden;
flex-shrink: 0;
display: inline-flex;
flex-direction: column;
box-shadow: 0 4rpx 20rpx rgba(192, 139, 126, 0.18);
white-space: normal;
}
.card--ongoing { box-shadow: 0 6rpx 28rpx rgba(192, 139, 126, 0.28); }
.card--upcoming { opacity: 0.95; }
.card--soldout { opacity: 0.7; }
.card--ended { opacity: 0.5; }
/* Card top gradient — warm blush tones */
.card-top {
padding: 20rpx 20rpx 16rpx;
background: linear-gradient(135deg, #D4A59A 0%, #C9948A 40%, #B5836E 100%);
display: flex;
flex-direction: column;
gap: 10rpx;
}
.card--upcoming .card-top {
background: linear-gradient(135deg, #8FA89A 0%, #7BA5A0 100%);
}
.card--soldout .card-top,
.card--ended .card-top {
background: linear-gradient(135deg, #C4BAB0, #AEA49A);
}
/* Phase badge */
.phase-badge {
align-self: flex-start;
padding: 4rpx 14rpx;
border-radius: 12rpx;
}
.badge--ongoing { background: rgba(255, 255, 255, 0.3); }
.badge--upcoming { background: rgba(255, 255, 255, 0.25); }
.badge--inactive { background: rgba(0, 0, 0, 0.1); }
.phase-badge-text {
font-size: 20rpx;
font-weight: 600;
color: #fff;
}
/* Countdown */
.countdown-row {
display: flex;
align-items: center;
gap: 8rpx;
}
.countdown-label {
font-size: 20rpx;
color: rgba(255, 255, 255, 0.85);
}
.countdown-blocks {
display: flex;
align-items: center;
gap: 4rpx;
}
.cd-block {
background: rgba(255, 255, 255, 0.25);
color: #fff;
font-size: 22rpx;
font-weight: 700;
padding: 4rpx 8rpx;
border-radius: 6rpx;
font-family: 'DIN Alternate', monospace;
min-width: 36rpx;
text-align: center;
backdrop-filter: blur(4px);
}
.cd-sep {
color: rgba(255, 255, 255, 0.75);
font-size: 20rpx;
font-weight: 700;
}
/* Card body */
.card-body {
padding: 20rpx;
background: #fff;
display: flex;
flex-direction: column;
gap: 8rpx;
}
.card-title {
font-size: 28rpx;
font-weight: 700;
color: $text-primary;
line-height: 1.3;
display: -webkit-box;
-webkit-line-clamp: 1;
-webkit-box-orient: vertical;
overflow: hidden;
}
.card-type-name {
font-size: 22rpx;
color: $text-hint;
}
/* Price area */
.price-area {
display: flex;
align-items: baseline;
gap: 12rpx;
margin-top: 4rpx;
}
.flash-price-row {
display: flex;
align-items: baseline;
}
.flash-currency {
font-size: 22rpx;
font-weight: 700;
color: #B5725E;
}
.flash-price {
font-size: 40rpx;
font-weight: 800;
color: #B5725E;
line-height: 1;
}
.original-price {
font-size: 22rpx;
color: #ccc;
text-decoration: line-through;
}
/* Stock area */
.stock-area {
display: flex;
flex-direction: column;
gap: 6rpx;
margin-top: 8rpx;
}
.stock-bar {
height: 10rpx;
background: #f5f0ed;
border-radius: 5rpx;
overflow: hidden;
}
.stock-fill {
height: 100%;
background: linear-gradient(90deg, #D4A59A, #C08B7E);
border-radius: 5rpx;
transition: width 0.3s;
&--hot {
animation: stockPulse 2s ease infinite;
}
}
@keyframes stockPulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.7; }
}
.stock-text {
font-size: 20rpx;
color: $text-hint;
}
</style>