You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
shop-app/pages/goods/detail/components/SeckillPrice.vue

210 lines
4.8 KiB

3 years ago
<!--
* @Author: ch
* @Date: 2022-04-12 17:33:42
* @LastEditors: ch
3 years ago
* @LastEditTime: 2022-04-24 19:01:45
3 years ago
* @Description: file content
-->
<template>
<view>
<view class="seckill" :class="activityStatus === 'startActivity' && 'seckill__start'">
<view class="seckill--price-box" v-if="activityStatus === 'noStartActivity'">
<text class="seckill--price-icon"></text>
<text class="seckill--price">{{data.originalPrice}}</text>
</view>
<view class="seckill--price-box " v-else>
<text class="seckill--price-icon"></text>
<text class="seckill--price">{{data.activityPrice}}</text>
<text class="seckill--price-org">{{data.originalPrice}}</text>
</view>
<view>
<view class="seckill--timer-title">
{{activityStatus === 'startActivity' ? '距结束仅剩' : '即将开始秒杀' }}
</view>
3 years ago
<view v-if="isDay" class="seckill--date">{{startDate}}</view>
<view v-else class="seckill--timer">
<view>{{hours}}</view>
<text>:</text>
<view>{{minute}}</view>
<text>:</text>
<view>{{second}}</view>
</view>
</view>
3 years ago
</view>
<view class="title">{{title}}</view>
3 years ago
</view>
</template>
<script>
import {formatDate} from '@/common/utils';
3 years ago
export default {
props : {
data : {
type : Object,
default : () => ({})
},
activityStatus : {
type : String,
default : 'noStartActivity'
},
title : {
type : String,
default : ''
3 years ago
}
},
data(){
return {
secondNum : 0,
timerStop : null
3 years ago
}
},
watch:{
data(){
this.calcStartSecond();
}
},
mounted(){
this.calcStartSecond();
},
3 years ago
computed:{
hours(){
let hours = parseInt(this.secondNum / 3600);
return hours > 9 ? hours : `0${hours}`;
},
minute(){
let minute = parseInt((this.secondNum - this.hours * 3600) / 60);
return minute > 9 ? minute : `0${minute}`;
3 years ago
},
second(){
let second = parseInt(this.secondNum % 60);
return second > 9 ? second : `0${second}`;
},
isDay(){
const currentTime = new Date(this.data.currentTime);
const activityTime = new Date(this.data.activityStartTime);
return currentTime.getDate() === activityTime.getDate();
},
startDate(){
return formatDate(this.data.activityStartTime, 'm月d日hh:ii')
3 years ago
}
},
methods:{
/**
* 计算倒计时开始秒数
*/
calcStartSecond(){
const {isStartActivity, activityStartTime, activityEndTime , currentTime} = this.data;
let curTime = (new Date(currentTime)).getTime(),
expireTime = 0;
if(isStartActivity){
// 已开始秒杀 显示距结束倒计时
expireTime = (new Date(activityEndTime)).getTime();
}else{
// 即将开抢 显示距开始倒计时
expireTime = (new Date(activityStartTime)).getTime();
}
const second = Math.floor((expireTime - curTime) / 1000);
this.secondNum = second > 0 ? second : 0;
this.timer();
},
/**
* 倒计时
*/
timer(){
if(this.secondNum < 2){
clearTimeout(this.timerStop);
this.$emit('change');
// 倒计时到0 后如果是已开始则触发已结束秒杀事件
// if(this.data.isStartActivity){
// this.$emit('change');
// // this.$emit('update:data',{...this.data, isActivity : false});
// }else{
// this.$emit('change');
// // 未开始则变为已开始,并重新开始倒计时
// // this.$emit('update:data',{...this.data, isStartActivity : true});
// // this.calcStartSecond();
// }
return false;
}
this.secondNum--;
this.timerStop = setTimeout(this.timer, 1000)
}
3 years ago
}
}
</script>
<style lang="scss" scoped>
.seckill{
height: 110rpx;
3 years ago
width: 690rpx;
margin: 20rpx auto 0;
3 years ago
position: relative;
display: flex;
justify-content: space-between;
padding: 0 20rpx 0 260rpx;
background-image: url('@/static/goods/seckill.png');
background-repeat : no-repeat;
background-size: 100%;
&__start{
padding: 0 20rpx 0 108rpx;
background-image: url('@/static/goods/seckill_start.png');
3 years ago
}
&--price-box{
padding-top: 30rpx;
}
&--price-icon{
color: $color-grey0;
font-weight: bold;
font-size: 32rpx;
3 years ago
}
&--price{
color: $color-grey0;
font-weight: bold;
font-size: 50rpx;
3 years ago
}
&--price-org{
color: $color-grey0;
3 years ago
text-decoration: line-through;
margin-left: 15rpx;
}
&--timer-title{
margin: 20rpx 0 10rpx 0;
color: $color-grey0;
text-align: right;
font-size: $font-size-sm;
3 years ago
}
3 years ago
&--date{
color: $color-grey0;
text-align: right;
font-size: $font-size-sm;
padding-top: 6rpx;
}
3 years ago
&--timer{
display: flex;
align-items: center;
view{
background: #E83710;
width: 40rpx;
height: 40rpx;
line-height: 40rpx;
3 years ago
color: #fff;
text-align: center;
border-radius: 8rpx;
font-size: $font-size-sm;
3 years ago
}
text{
margin: 0 5rpx;
color: #fff;
3 years ago
}
}
}
.title{
margin: 0 30rpx ;
background: $color-grey0;
border-radius: 16rpx;
padding: 30rpx;
font-size: $font-size-lg;
line-height: 48rpx;
}
3 years ago
</style>