web/buyer/src/pages/promotion/seckill.vue

280 lines
7.5 KiB
Vue
Raw Normal View History

2021-05-13 10:56:04 +08:00
<template>
<div class="seckill-list">
<BaseHeader></BaseHeader>
<Search />
<!-- 秒杀时间段 -->
<div class="promotion-decorate">限时秒杀</div>
<ul class="time-line">
<template v-for="(time, index) in list">
2022-10-09 16:04:25 +08:00
<li
v-if="index < 5"
@click="currIndex = index"
:key="index"
:class="{ currTimeline: currIndex === index }"
>
<div>{{ time.timeLine + ":00" }}</div>
2021-05-13 10:56:04 +08:00
<div v-if="currIndex === index">
2022-10-09 16:04:25 +08:00
<p>{{ nowHour >= time.timeLine ? "秒杀中" : "即将开始" }}</p>
<p>
{{ nowHour >= time.timeLine ? "距结束" : "距开始" }}&nbsp;{{
currTime
}}
</p>
2021-05-13 10:56:04 +08:00
</div>
<div v-else class="not-curr">
2022-10-09 16:04:25 +08:00
{{ nowHour >= time.timeLine ? "秒杀中" : "即将开始" }}
2021-05-13 10:56:04 +08:00
</div>
</li>
</template>
</ul>
<!-- 秒杀商品列表 -->
<div class="goods-list">
<empty v-if="goodsList.length === 0" />
<div
v-else
2022-10-09 16:04:25 +08:00
class="goods-show-info1"
2021-05-13 10:56:04 +08:00
v-for="(item, index) in goodsList"
:key="index"
@click="goGoodsDetail(item.skuId, item.goodsId)"
>
<div class="goods-show-img">
2022-10-09 16:04:25 +08:00
<img width="200" height="200" :src="item.goodsImage" />
2021-05-13 10:56:04 +08:00
</div>
<div class="goods-show-price">
<span>
<span class="seckill-price text-danger">{{
item.price | unitPrice("¥")
}}</span>
2022-10-09 16:04:25 +08:00
<span style="color: #999; text-decoration: line-through">{{
item.originalPrice | unitPrice("¥")
}}</span>
2021-05-13 10:56:04 +08:00
</span>
</div>
<div class="goods-show-detail">
<span>{{ item.goodsName }}</span>
</div>
2022-10-09 16:04:25 +08:00
<div
class="goods-seckill-btn"
:class="{
'goods-seckill-btn-gray': nowHour < list[currIndex].timeLine,
}"
>
{{ nowHour >= list[currIndex].timeLine ? "立即抢购" : "即将开始" }}
</div>
<div
v-if="nowHour >= list[currIndex].timeLine"
class="goods-seckill-btn goods-seckill-btn-gray"
>
已售罄
</div>
<div v-if="nowHour >= list[currIndex].timeLine" class="goods-show-num1">
<span
>已售{{
(item.quantity && item.quantity > 0
? Math.ceil(
(item.salesNum / (item.quantity + item.salesNum)) * 100
)
: 100) + "%"
}}</span
><Progress
hide-info
stroke-color="#df0021"
style="width: 110px"
class="ml_10"
:percent="
item.quantity && item.quantity > 0
? Math.ceil(
(item.salesNum / (item.quantity + item.salesNum)) * 100
)
: 100
"
/>
2021-05-13 10:56:04 +08:00
</div>
<div class="goods-show-seller">
<span>{{ item.storeName }}</span>
</div>
</div>
</div>
<BaseFooter />
</div>
</template>
<script>
2022-10-09 16:04:25 +08:00
import { seckillByDay } from "@/api/promotion";
2021-05-13 10:56:04 +08:00
export default {
2022-10-09 16:04:25 +08:00
data() {
2021-05-13 10:56:04 +08:00
return {
2021-05-13 11:35:57 +08:00
list: [], // 秒杀时段列表
goodsList: [], // 商品列表
interval: null, // 定时器
2021-05-13 10:56:04 +08:00
currIndex: 0, // 当前时间段的下标
currTime: 0, // 当前显示的倒计时
diffSeconds: 0, // 倒计时时间戳
2022-10-09 16:04:25 +08:00
nowHour: new Date().getHours(), // 当前小时数
};
2021-05-13 10:56:04 +08:00
},
2022-10-09 16:04:25 +08:00
beforeDestroy() {
// 销毁前清除定时器
clearInterval(this.interval);
},
2021-05-13 10:56:04 +08:00
watch: {
2022-10-09 16:04:25 +08:00
currIndex(val) {
clearInterval(this.interval);
this.interval = null;
this.nowHour = new Date().getHours();
this.countDown(val);
this.goodsList = this.list[val].seckillGoodsList;
2021-05-13 10:56:04 +08:00
},
2022-10-09 16:04:25 +08:00
diffSeconds(val) {
2021-05-13 10:56:04 +08:00
const hours = Math.floor(val / 3600);
// 当前秒数 / 60向下取整
// 获取到所有分钟数 3600 / 60 = 60分钟
// 对60取模超过小时数的分钟数
const minutes = Math.floor(val / 60) % 60;
// 当前的秒数 % 60获取到 超过小时数、分钟数的秒数(秒数)
const seconds = val % 60;
2022-10-09 16:04:25 +08:00
this.currTime =
filteTime(hours) + ":" + filteTime(minutes) + ":" + filteTime(seconds);
2021-05-13 10:56:04 +08:00
if (val <= 0) {
2022-10-09 16:04:25 +08:00
clearInterval(this.interval);
this.interval = null;
2021-05-13 10:56:04 +08:00
}
2022-10-09 16:04:25 +08:00
function filteTime(time) {
2021-05-13 10:56:04 +08:00
if (time < 10) {
2022-10-09 16:04:25 +08:00
return "0" + time;
2021-05-13 10:56:04 +08:00
} else {
2022-10-09 16:04:25 +08:00
return time;
2021-05-13 10:56:04 +08:00
}
}
2022-10-09 16:04:25 +08:00
},
2021-05-13 10:56:04 +08:00
},
methods: {
2022-10-09 16:04:25 +08:00
getListByDay() {
// 当天秒杀活动
seckillByDay().then((res) => {
2021-05-13 10:56:04 +08:00
if (res.success) {
2022-10-09 16:04:25 +08:00
this.list = res.result;
this.goodsList = this.list[0].seckillGoodsList;
this.countDown(this.currIndex);
2021-05-13 10:56:04 +08:00
}
2022-10-09 16:04:25 +08:00
});
2021-05-13 10:56:04 +08:00
},
2022-10-09 16:04:25 +08:00
goGoodsDetail(skuId, goodsId) {
2021-05-13 10:56:04 +08:00
// 跳转商品详情
let routeUrl = this.$router.resolve({
2022-10-09 16:04:25 +08:00
path: "/goodsDetail",
query: { skuId, goodsId },
2021-05-13 10:56:04 +08:00
});
2022-10-09 16:04:25 +08:00
window.open(routeUrl.href, "_blank");
2021-05-13 10:56:04 +08:00
},
2022-10-09 16:04:25 +08:00
countDown(currIndex) {
// 倒计时
2021-05-13 10:56:04 +08:00
// 0点时间戳
let zeroTime = new Date(new Date().toLocaleDateString()).getTime();
2022-10-09 16:04:25 +08:00
let currTime = new Date().getTime();
2021-05-13 10:56:04 +08:00
let actTime = 0;
let nowHour = new Date().getHours(); // 当前小时数
2022-10-09 16:04:25 +08:00
if (this.list[currIndex].timeLine > nowHour) {
// 活动未开始
actTime = zeroTime + this.list[currIndex].timeLine * 3600 * 1000;
} else if (this.list[currIndex].timeLine <= nowHour) {
// 活动进行中
if (currIndex === this.list.length - 1) {
// 如果是最后一个活动直到24点结束
actTime = zeroTime + 24 * 3600 * 1000;
2021-05-13 10:56:04 +08:00
} else {
2022-10-09 16:04:25 +08:00
actTime = zeroTime + this.list[currIndex + 1].timeLine * 3600 * 1000;
2021-05-13 10:56:04 +08:00
}
}
2022-10-09 16:04:25 +08:00
this.diffSeconds = Math.floor((actTime - currTime) / 1000);
2021-05-13 10:56:04 +08:00
this.interval = setInterval(() => {
2022-10-09 16:04:25 +08:00
this.diffSeconds--;
}, 1000);
},
2021-05-13 10:56:04 +08:00
},
2022-10-09 16:04:25 +08:00
mounted() {
this.getListByDay();
},
};
2021-05-13 10:56:04 +08:00
</script>
<style lang="scss" scoped>
2022-10-09 16:04:25 +08:00
@import "../../assets/styles/goodsList.scss";
2021-05-13 10:56:04 +08:00
.goods-seckill-btn {
position: absolute;
right: 0;
bottom: 0;
width: 80px;
color: #fff;
height: 35px;
text-align: center;
line-height: 35px;
font-size: 14px;
background-color: $theme_color;
}
.goods-seckill-btn-gray {
background-color: #666;
}
2022-10-09 16:04:25 +08:00
.promotion-decorate::before,
.promotion-decorate::after {
background-image: url("/src/assets/images/sprite@2x.png");
2021-05-13 10:56:04 +08:00
}
2022-10-09 16:04:25 +08:00
.time-line {
2021-05-13 10:56:04 +08:00
width: 1200px;
height: 60px;
margin: 0 auto;
background-color: #fff;
display: flex;
2022-10-09 16:04:25 +08:00
li {
2021-05-13 10:56:04 +08:00
padding: 0 30px;
font-size: 16px;
font-weight: bold;
width: 240px;
height: 100%;
display: flex;
align-items: center;
2022-10-09 16:04:25 +08:00
&:hover {
2021-05-13 10:56:04 +08:00
cursor: pointer;
}
.not-curr {
border: 1px solid #999;
border-radius: 20px;
padding: 3px 10px;
margin-left: 10px;
font-size: 12px;
font-weight: normal;
}
}
2022-10-09 16:04:25 +08:00
.currTimeline {
2021-05-13 10:56:04 +08:00
background-color: $theme_color;
color: #fff;
2022-10-09 16:04:25 +08:00
> div:nth-child(1) {
2021-05-13 10:56:04 +08:00
font-size: 20px;
}
2022-10-09 16:04:25 +08:00
> div:nth-child(2) {
2021-05-13 10:56:04 +08:00
font-size: 14px;
margin-left: 10px;
}
}
}
2022-10-09 16:04:25 +08:00
.goods-show-info1 {
width: 275px;
padding: 6px;
margin: 10px 0px;
margin-left: 5px;
position: relative;
border: 1px solid #fff;
cursor: pointer;
background-color: #fff;
}
.goods-show-info1:hover {
border: 1px solid #ccc;
box-shadow: 0px 0px 15px #ccc;
}
.goods-show-img {
display: flex;
justify-content: center;
}
.ivu-progress-success .ivu-progress-bg {
background-color: #111fff;
}
2021-05-13 10:56:04 +08:00
</style>