| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- <template>
- <view class="container">
- <!-- 头部导航栏 (使用默认或者自定义) -->
-
- <!-- 商品列表 -->
- <view class="goods-list">
- <view class="goods-item" v-for="(item, index) in list" :key="item.id" @tap="showProduct(item)">
- <image class="goods-img" :src="item.productImage" mode="aspectFill"></image>
-
- <view class="goods-info">
- <view class="goods-name ellipsis2">{{ item.productName }}</view>
-
- <view class="countdown-box" v-if="item.activityStatus !== 'ended' && item.activityStatus !== 'sold_out'">
- <text class="status-text" style="margin-left: 0; margin-right: 10rpx;">{{ item.activityStatus === 'not_started' ? '即将开抢' : '抢购中' }}</text>
- <text class="time-text">{{ formatTime(item.countdown) }}</text>
- </view>
-
- <view class="goods-bottom">
- <view class="price-info">
- <view class="flash-price">¥<text class="num">{{ item.flashPrice }}</text></view>
- <view class="original-price">¥{{ item.originalPrice }}</view>
- </view>
-
- <view class="btn-box">
- <button
- class="grab-btn"
- :class="[
- item.activityStatus === 'not_started' ? 'not-started' : '',
- item.activityStatus === 'sold_out' ? 'sold-out' : '',
- item.activityStatus === 'ended' ? 'ended' : ''
- ]"
- >
- {{ getBtnText(item.activityStatus) }}
- </button>
- </view>
- </view>
- </view>
- </view>
- </view>
-
- <view class="empty-box" v-if="list.length === 0 && !loading">
- <text>暂无秒杀活动</text>
- </view>
- </view>
- </template>
- <script>
- import { getActiveFlashSaleList, getFlashSaleServerTime } from '@/api/flashSale.js';
- export default {
- data() {
- return {
- list: [],
- timer: null,
- loading: true,
- serverTimestamp: 0
- };
- },
- onLoad() {
- this.fetchData();
- },
- onUnload() {
- this.clearTimer();
- },
- methods: {
- async fetchData() {
- this.loading = true;
- try {
- const [listRes, timeRes] = await Promise.all([
- getActiveFlashSaleList(),
- getFlashSaleServerTime()
- ]);
-
- this.loading = false;
- let currentServerTime = Date.now();
- if (timeRes.code === 0 || timeRes.code === 200) {
- currentServerTime = timeRes.serverTimestamp || timeRes.data?.serverTimestamp || Date.now();
- } else if (listRes.serverTimestamp) {
- currentServerTime = listRes.serverTimestamp;
- }
-
- const serverTimeSec = Math.floor(currentServerTime / 1000);
- if (listRes.code === 0 || listRes.code === 200) {
- this.list = listRes.data || [];
-
- this.list.forEach(item => {
- let targetTime = 0;
- if (item.activityStatus === 'not_started') {
- targetTime = Math.floor(new Date(item.startTime.replace(/-/g, '/')).getTime() / 1000);
- } else if (item.activityStatus === 'ongoing') {
- targetTime = Math.floor(new Date(item.endTime.replace(/-/g, '/')).getTime() / 1000);
- }
-
- if (targetTime > 0) {
- let diff = targetTime - serverTimeSec;
- item.countdown = diff > 0 ? diff : 0;
- }
- });
-
- this.startTimer();
- } else {
- uni.showToast({
- title: listRes.msg || '获取数据失败',
- icon: 'none'
- });
- }
- } catch (error) {
- this.loading = false;
- console.error(error);
- }
- },
- startTimer() {
- this.clearTimer();
- this.timer = setInterval(() => {
- let hasCountdown = false;
- this.list.forEach(item => {
- if (item.countdown > 0) {
- item.countdown--;
- hasCountdown = true;
- } else if (item.countdown === 0 && item.activityStatus === 'not_started') {
- item.activityStatus = 'ongoing';
- let endTimeSec = Math.floor(new Date(item.endTime.replace(/-/g, '/')).getTime() / 1000);
- let nowSec = Math.floor(Date.now() / 1000);
- let diff = endTimeSec - nowSec;
- item.countdown = diff > 0 ? diff : 0;
- if (item.countdown > 0) hasCountdown = true;
- } else if (item.countdown === 0 && item.activityStatus === 'ongoing') {
- item.activityStatus = 'ended';
- }
- });
- if (!hasCountdown) {
- this.clearTimer();
- }
- }, 1000);
- },
- clearTimer() {
- if (this.timer) {
- clearInterval(this.timer);
- this.timer = null;
- }
- },
- formatTime(seconds) {
- if (!seconds || seconds <= 0) return '00:00:00';
- let h = Math.floor(seconds / 3600);
- let m = Math.floor((seconds % 3600) / 60);
- let s = seconds % 60;
- return `${h.toString().padStart(2, '0')}:${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}`;
- },
- getBtnText(status) {
- switch (status) {
- case 'not_started': return '即将开抢';
- case 'ongoing': return '立即抢购';
- case 'sold_out': return '已售罄';
- case 'ended': return '已结束';
- default: return '抢购';
- }
- },
- showProduct(item) {
- uni.navigateTo({
- url: '/pages_index/index/activityProductDetail?type=flash&id=' + item.id
- });
- }
- }
- };
- </script>
- <style lang="scss" scoped>
- .container {
- min-height: 100vh;
- background-color: #f5f5f5;
- padding: 20rpx;
- }
- .goods-list {
- .goods-item {
- display: flex;
- background-color: #fff;
- border-radius: 16rpx;
- padding: 20rpx;
- margin-bottom: 20rpx;
-
- .goods-img {
- width: 240rpx;
- height: 240rpx;
- border-radius: 12rpx;
- background-color: #f0f0f0;
- flex-shrink: 0;
- }
-
- .goods-info {
- flex: 1;
- margin-left: 20rpx;
- display: flex;
- flex-direction: column;
- justify-content: space-between;
-
- .goods-name {
- font-size: 28rpx;
- color: #333;
- font-weight: bold;
- line-height: 40rpx;
- }
-
- .ellipsis2 {
- display: -webkit-box;
- -webkit-box-orient: vertical;
- -webkit-line-clamp: 2;
- overflow: hidden;
- }
-
- .countdown-box {
- display: flex;
- align-items: center;
- margin-top: 10rpx;
-
- .time-text {
- font-size: 24rpx;
- color: #ff4a00;
- background-color: rgba(255, 74, 0, 0.1);
- padding: 2rpx 10rpx;
- border-radius: 6rpx;
- font-weight: bold;
- }
-
- .status-text {
- font-size: 22rpx;
- color: #999;
- margin-left: 10rpx;
- }
- }
-
- .goods-bottom {
- display: flex;
- justify-content: space-between;
- align-items: flex-end;
- margin-top: 10rpx;
-
- .price-info {
- .flash-price {
- color: #ff4a00;
- font-size: 24rpx;
- font-weight: bold;
-
- .num {
- font-size: 36rpx;
- }
- }
-
- .original-price {
- color: #999;
- font-size: 22rpx;
- text-decoration: line-through;
- margin-top: 4rpx;
- }
- }
-
- .btn-box {
- .grab-btn {
- margin: 0;
- padding: 0 24rpx;
- height: 56rpx;
- line-height: 56rpx;
- border-radius: 28rpx;
- font-size: 24rpx;
- color: #fff;
- background: linear-gradient(90deg, #ff6000, #ff4a00);
- border: none;
-
- &::after {
- display: none;
- }
-
- &.not-started {
- background: #cccccc;
- }
-
- &.sold-out {
- background: #cccccc;
- }
-
- &.ended {
- background: #cccccc;
- }
- }
- }
- }
- }
- }
- }
- .empty-box {
- padding: 100rpx 0;
- text-align: center;
- color: #999;
- font-size: 28rpx;
- }
- </style>
|