vr-shopxo-uniapp/components/countdown/countdown.vue

212 lines
6.8 KiB
Vue
Raw Normal View History

2023-08-31 06:26:22 +00:00
<template>
2023-11-06 11:43:27 +00:00
<view :class="theme_view">
2023-09-11 02:01:49 +00:00
<view class="countdown" v-if="is_show && !is_end">
<block v-if="propMsecShow">
2024-09-25 16:23:30 +00:00
<view class="fr time" :style="time_style">{{ msec }}</view>
<view class="fr ds" :style="ds_style">{{ propSecondDs }}</view>
2023-09-11 02:01:49 +00:00
</block>
2024-09-25 16:23:30 +00:00
<view class="fr time" :style="time_style">{{ second }}</view>
<view class="fr ds" :style="ds_style">{{ propMinuteDs }}</view>
<view class="fr time" :style="time_style">{{ minute }}</view>
<view class="fr ds" :style="ds_style">{{ propHourDs }}</view>
<view class="fr time" :style="time_style">{{ hour }}</view>
2023-09-11 02:01:49 +00:00
</view>
2024-01-15 10:39:38 +00:00
<view v-if="is_show && is_end" class="timer-title">{{ propMsg || this.$t('index.index.443683') }}</view>
2023-09-11 02:01:49 +00:00
</view>
2023-08-31 06:26:22 +00:00
</template>
<script>
2023-11-06 11:43:27 +00:00
const app = getApp();
2023-09-11 02:01:49 +00:00
export default {
data() {
return {
2023-11-06 11:43:27 +00:00
theme_view: app.globalData.get_theme_value_view(),
2023-09-11 02:01:49 +00:00
hour: '00',
minute: '00',
second: '00',
msec: 0,
is_show: true,
is_end: false,
timer: null,
timers: null,
};
},
components: {},
props: {
propHour: {
type: [String, Number],
2023-09-22 09:30:56 +00:00
default: '00',
2023-09-11 02:01:49 +00:00
},
propMinute: {
type: [String, Number],
2023-09-22 09:30:56 +00:00
default: '00',
2023-09-11 02:01:49 +00:00
},
propSecond: {
type: [String, Number],
2023-09-22 09:30:56 +00:00
default: '00',
2023-09-11 02:01:49 +00:00
},
propEndShow: {
type: Boolean,
2023-09-22 09:30:56 +00:00
default: false,
2023-09-11 02:01:49 +00:00
},
propMsecShow: {
type: Boolean,
2023-09-22 09:30:56 +00:00
default: false,
2023-09-11 02:01:49 +00:00
},
propMsg: {
type: String,
2024-01-15 10:39:38 +00:00
default: '',
2023-09-11 02:01:49 +00:00
},
propHourDs: {
type: String,
2023-09-22 09:30:56 +00:00
default: ':',
2023-09-11 02:01:49 +00:00
},
propMinuteDs: {
type: String,
2023-09-22 09:30:56 +00:00
default: ':',
2023-09-11 02:01:49 +00:00
},
propSecondDs: {
type: String,
2023-09-22 09:30:56 +00:00
default: ':',
2023-09-11 02:01:49 +00:00
},
propTimePadding: {
type: [Number, String],
2023-09-22 09:30:56 +00:00
default: 0,
2023-09-11 02:01:49 +00:00
},
propTimeSize: {
type: [Number, String],
2023-09-22 09:30:56 +00:00
default: 24,
2023-09-11 02:01:49 +00:00
},
propTimeBackgroundColor: {
type: String,
2023-09-22 09:30:56 +00:00
default: 'linear-gradient(180deg, #FF601B 0%, #FE1B33 100%);',
2023-09-11 02:01:49 +00:00
},
propTimeColor: {
type: String,
2023-09-22 09:30:56 +00:00
default: '#FFF',
2023-09-11 02:01:49 +00:00
},
propTimeWeight: {
type: [Number, String],
2023-09-22 09:30:56 +00:00
default: '400',
2023-09-11 02:01:49 +00:00
},
propDsColor: {
type: String,
2023-09-22 09:30:56 +00:00
default: '#4B5459',
2023-09-11 02:01:49 +00:00
},
propDsSize: {
type: [Number, String],
2023-09-22 09:30:56 +00:00
default: 24,
2023-09-11 02:01:49 +00:00
},
propDsWeight: {
type: [Number, String],
2023-09-22 09:30:56 +00:00
default: '400',
},
2023-09-11 02:01:49 +00:00
},
computed: {
time_style() {
2023-09-22 09:30:56 +00:00
return 'padding: 0 ' + this.propTimePadding + 'rpx;background:' + this.propTimeBackgroundColor + ';color:' + this.propTimeColor + ';font-size:' + this.propTimeSize + 'rpx;font-weight:' + this.propTimeWeight;
2023-09-11 02:01:49 +00:00
},
ds_style() {
2023-09-22 09:30:56 +00:00
return 'color:' + this.propDsColor + ';font-size:' + this.propDsSize + 'rpx;font-weight:' + this.propDsWeight;
},
2023-09-11 02:01:49 +00:00
},
2023-09-22 09:30:56 +00:00
created: function (e) {
2023-09-11 02:01:49 +00:00
// 参数处理
this.hour = this.propHour;
this.minute = this.propMinute;
this.second = this.propSecond;
2023-08-31 06:26:22 +00:00
2023-09-11 02:01:49 +00:00
// 定时处理
2023-09-22 09:30:56 +00:00
this.countdown();
2023-09-11 02:01:49 +00:00
},
// #ifndef VUE2
destroyed() {
clearInterval(this.timer);
clearInterval(this.timers);
},
// #endif
// #ifdef VUE3
unmounted() {
clearInterval(this.timer);
clearInterval(this.timers);
},
// #endif
methods: {
// 倒计时处理
countdown() {
// 销毁之前的任务
clearInterval(this.timer);
clearInterval(this.timers);
2023-08-31 06:26:22 +00:00
2023-09-11 02:01:49 +00:00
// 秒
var self = this;
var hour = parseInt(self.hour);
var minute = parseInt(self.minute);
var second = parseInt(self.second);
2023-09-22 09:30:56 +00:00
self.timer = setInterval(function () {
2023-09-11 02:01:49 +00:00
if (second <= 0) {
if (minute > 0) {
minute--;
second = 59;
} else if (hour > 0) {
hour--;
minute = 59;
second = 59;
}
} else {
second--;
}
2023-08-31 06:26:22 +00:00
2023-09-11 02:01:49 +00:00
self.hour = hour < 10 ? '0' + hour : hour;
self.minute = minute < 10 ? '0' + minute : minute;
self.second = second < 10 ? '0' + second : second;
if (self.propHour <= 0 && self.propMinute <= 0 && self.propSecond <= 0) {
// 停止时间
clearInterval(self.timer);
clearInterval(self.timers);
self.is_end = true;
2023-08-31 06:26:22 +00:00
2023-09-11 02:01:49 +00:00
// 活动已结束、是否结束还展示
if (!self.propEndShow) {
self.is_show = false;
}
}
}, 1000);
2023-08-31 06:26:22 +00:00
2023-09-11 02:01:49 +00:00
// 毫秒
var count = 0;
2023-09-22 09:30:56 +00:00
self.timers = setInterval(function () {
2023-09-11 02:01:49 +00:00
count++;
self.msec = count;
if (count > 9) {
count = 0;
}
if (!self.is_show) {
clearInterval(self.timers);
}
}, 100);
2023-09-22 09:30:56 +00:00
},
},
2023-09-11 02:01:49 +00:00
};
2023-08-31 06:26:22 +00:00
</script>
2023-09-22 09:30:56 +00:00
<style scoped>
2023-09-11 02:01:49 +00:00
.countdown {
line-height: 38rpx;
}
.countdown .timer-title {
color: #666;
margin-right: 10rpx;
}
.countdown .time {
padding: 0;
-moz-border-radius: 8rpx;
border-radius: 8rpx;
color: #fff;
min-width: 40rpx;
text-align: center;
}
.countdown .ds {
2023-09-22 09:30:56 +00:00
color: #4b5459;
2023-09-11 02:01:49 +00:00
padding: 0 8rpx;
}
2023-09-22 09:30:56 +00:00
</style>