CountDown.vue
4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<template>
<div class="count-down">
<div class="bg">
<img src="https://img02.glsx.com.cn/weapp/resource/scrm-mobile/crowdfunding/timer-bg.png" />
</div>
<div :class="['timer-title', { 'timer-not-started': detail.activityStatus == 1 }]">
<img :src="`https://img02.glsx.com.cn/weapp/resource/scrm-mobile/crowdfunding/timer-${detail.activityStatus == 1 ? 1 : 2}.png`" />
</div>
<div :class="['time-box text-center', { 'not-started': detail.activityStatus == 1 }, { finished: finished }]">
<template v-if="timeFlag">
<template v-if="finished">{{ finished }}</template>
<template v-else>
<template v-if="detail.activityStatus == 1">{{ new Date(iosTimeFormat(detail.startTime)).format('M月d') }}日开始,还差</template>
<template v-if="activityTime.day">
<div class="time flex">
<span class="text-center flex">{{ activityTime.day }}</span>
<div class="unit">天</div>
</div>
</template>
<template v-for="(i, index) in activityTime.dayList">
<div class="time flex" :key="index">
<span class="text-center">{{ i.text }}</span>
<div class="unit">{{ i.unit }}</div>
</div>
</template>
</template>
</template>
</div>
</div>
</template>
<script type="text/ecmascript-6">
export default {
name: 'CountDown',
props: {
detail: {
type: Object,
default: () => { }
},
},
data () {
return {
activityTime: {},
timeFlag: false,
finished: ''
}
},
mounted () {
this.init()
},
methods: {
init () {
this.timeFlag = false
let { detail } = this
this.countDown([detail.startTime, detail.endTime])
this.hideLoading()
},
countDown (params) {
let {
activityStatus
} = this.detail
// activityStatus 1:未开始,2进行中,3已结束
const addZero = i => i < 10 ? `0${i}` : `${i}`
let [activityStartTime, activityEndTime] = params
const start = this.iosTimeFormat(activityStartTime)
const end = this.iosTimeFormat(activityEndTime)
if ((new Date().getTime() > new Date(end).getTime()) || activityStatus == 3) {
clearTimeout(this.time)
this.timeFlag = true
this.finished = '活动已结束'
if (activityStatus != 3) this.$emit('refresh', 3)
return
}
if (new Date().getTime() >= new Date(start).getTime() && activityStatus != 2) {
this.$emit('refresh', 2)
return
}
let starTime = new Date()
let endTime = new Date(activityStatus == 2 ? end : start)
let lefttime = parseInt((endTime.getTime() - starTime.getTime()) / 1000)
let d = parseInt(lefttime / (24 * 60 * 60))
let h = parseInt(lefttime / (60 * 60) % 24)
let m = parseInt(lefttime / 60 % 60)
let s = parseInt(lefttime % 60)
h = addZero(h)
m = addZero(m)
s = addZero(s)
let type = {
1: '距开始',
2: '距结束'
}[activityStatus]
if (this.time) clearTimeout(this.time)
this.time = setTimeout(() => {
let dayList = [{
text: h,
unit: '时'
}, {
text: m,
unit: '分'
}, {
text: s,
unit: '秒'
}]
let activityTime = {
dayList,
type
}
if (d > 0) activityTime.day = addZero(d)
this.activityTime = activityTime
this.timeFlag = true
this.countDown(params)
}, 1 * 1000)
}
}
}
</script>
<style scoped lang="scss">
@import '@/assets/css/crowdfunding.scss';
.count-down {
position: relative;
.bg {
position: absolute;
width: calc(100% + 32px);
left: -16px;
top: 0;
z-index: -1;
img {
width: 100%;
height: 200px;
}
}
}
.timer-title {
padding-top: 14px;
height: 19px;
img {
width: 105px;
}
}
.timer-not-started {
img {
width: 128px;
}
}
.time-box {
height: 56px;
border-radius: 4px;
margin: 11.5px 0;
.time {
span {
width: 34px;
height: 32px;
background: #fff;
border-radius: 4px;
font-size: 20px;
color: #fa2c18;
font-weight: 600;
}
.unit {
font-weight: 500;
font-size: 20px;
color: #fff;
margin: 0 5px 0 3px;
}
}
}
.not-started {
font-size: 14px;
font-weight: 600;
color: #fff;
border: 1px dashed #fff;
box-sizing: border-box;
.time {
margin-left: 2px;
span {
width: 26px;
height: 26px;
background: #ae0404;
box-shadow: inset 1px 1px 2px 0px rgba(111, 0, 0, 0.5);
border-radius: 3px;
color: #fff;
font-size: 18px;
}
.unit {
font-size: 14px;
}
}
}
.finished {
font-size: 20px;
font-weight: 600;
color: #fff;
border: 1px dashed #ffffff;
box-sizing: border-box;
}
</style>