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
| <template>
| <!--倒计时-->
| <view class="countdown" :style="'color:'+color">
| <template v-if="status==2">活动已结束</template>
| <template v-else>
| {{title}} <text class="mr10">{{parseInt(day)+'天'}}</text>
| <text class="f28" :style="'font-size:'+timeSize+';'">{{ parseInt(hour) }}:{{ minute }}:{{ second }}</text>
| </template>
| </view>
| </template>
|
| <script>
| export default {
| data() {
| return {
| /*状态*/
| status: 0,
| /*天*/
| day: '0',
| /*时*/
| hour: '0',
| /*分*/
| minute: '0',
| /*秒*/
| second: '0',
| /*定时器*/
| timer: null,
| /*剩余秒*/
| totalSeconds: 0,
| /*标题*/
| title: '还剩'
| };
| },
| props: {
| config: {
| type: Object,
| default: function() {
| return {
| type: 'all',
| };
| }
| },
| color: {
| type: String,
| default: function() {
| return '#ffffff';
| }
| },
| timeSize: {
| type: String,
| default: function() {
| return '28rpx';
| }
| },
| },
| created() {},
| watch: {
| config: {
| deep: true,
| handler: function(n, o) {
| if (n != o && n.endstamp != 0) {
| if (n.title && typeof n.title != 'undefined') {
| this.title = n.title;
| }
| this.setTime();
| }
| },
| immediate: true
| }
| },
| methods: {
| /*轮循*/
| setTime() {
| let self = this;
| self.timer = setInterval(function() {
| self.init();
| }, 1000);
| },
|
| /*初始化*/
| init() {
| let nowseconds = Date.now() / 1000;
| if (nowseconds < this.config.startstamp) {
| //活动时间还没到
| this.status = 1;
| this.title = '距开始还剩';
| } else if (nowseconds > this.config.endstamp) {
| //活动已过期
| this.status = 2;
| } else {
| //活动进行中
| this.totalSeconds = parseInt(this.config.endstamp - nowseconds);
| this.status = 0;
| this.title = '距截止还剩';
| this.countDown();
| }
| this.$emit('returnVal', this.status);
| },
|
| /*计算时分秒*/
| countDown() {
| let totalSeconds = this.totalSeconds;
| //天数
| let day = Math.floor(totalSeconds / (60 * 60 * 24));
| //取模(余数)
| let modulo = totalSeconds % (60 * 60 * 24);
| //小时数
| let hour = Math.floor(modulo / (60 * 60));
| modulo = modulo % (60 * 60);
| //分钟
| let minute = Math.floor(modulo / 60);
| //秒
| let second = modulo % 60;
| this.day = this.convertTwo(day);
| this.hour = this.convertTwo(hour);
| this.minute = this.convertTwo(minute);
| this.second = this.convertTwo(second);
| this.totalSeconds--;
| },
|
| /*转两位数*/
| convertTwo(n) {
| let str = '';
| if (n < 10) {
| str = '0' + n;
| } else {
| str = n;
| }
| return str;
| },
|
| /*把时间戳转普通时间*/
| getLocalTime(nS) {
| return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/, ' ');
| },
| clear() {
| console.log(1)
| clearInterval(this.timer);
| this.timer = null;
| },
| },
|
| destroyed() {
| clearInterval(this.timer);
| }
| };
| </script>
|
| <style lang="scss">
| .countdown{
| color: #FFFFFF;
| font-size: 22rpx;
| }
| </style>
|
|