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
| <template> <div class="count-down-wrap" :class="type"> <div v-if="showDesc" class="count-down-text">{{lableText}}</div> <template v-if="notSimple"> <div class="count-down-time"> <div class="count-down-day">{{countDown.day}}</div> <div class="count-down-dot">天</div> <div class="count-down-hour">{{countDown.hour}}</div> <div class="count-down-dot">:</div> <div class="count-down-minute">{{countDown.minute}}</div> <div class="count-down-dot">:</div> <div class="count-down-second">{{countDown.second}}</div> </div> </template> <template v-else-if="showSimple"> <div class="count-down-time"> <div class="count-down-minute">{{countDown.minute}}</div> <div class="count-down-dot">:</div> <div class="count-down-second">{{countDown.second}}</div> </div> </template> <div v-if="postLable" class="post-count-down-text">{{postLable}}</div> </div> </template> <script lang="ts"> import { defineComponent, ref, computed, watch, onBeforeUnmount } from 'vue' const TIMESTAMPOFDAY = 24 * 60 * 60 export default defineComponent({ name: 'DriverSupportCountDown', props: { type: { type: String }, lable: { type: String }, timeGap: { type: Number, default: 0 }, postLable: { type: String } }, setup(props, ctx) { let countDownGap = props.timeGap || 0 const countDown = ref({ day: '0', hour: '00', minute: '00', second: '00' }) let countDownHandler = 0 const lableText = computed(() => props.lable || '') const showDesc = computed(() => props.type === 'full' && lableText) const notSimple = computed(() => props.type !== 'simple') const showSimple = computed(() => props.type === 'simple') const prefixTime = (time) => { if (time < 10) { return `0${time}` } return `${time}` } const updateCountDown = () => { if (countDownGap < 0) { ctx.emit('update') stopCountDown() return } const dayNum = parseInt(countDownGap / TIMESTAMPOFDAY + '', 10) const hourNum = parseInt((countDownGap / 3600) % 24 + '', 10) const minuteNum = parseInt((countDownGap / 60) % 60 + '', 10) const secondNum = parseInt(countDownGap % 60 + '', 10) countDown.value = { day: dayNum + '', hour: prefixTime(hourNum), minute: prefixTime(minuteNum), second: prefixTime(secondNum) } countDownGap -= 1 } const startCountDown = () => { if (!countDownGap || countDownGap <= 0) return if (countDownHandler) { stopCountDown() } updateCountDown() countDownHandler = window.setInterval(() => { updateCountDown() }, 1000) } startCountDown() const stopCountDown = () => { if (countDownHandler) { clearInterval(countDownHandler) countDownHandler = 0 } } watch(() => props.timeGap, () => { countDownGap = props.timeGap startCountDown() }) onBeforeUnmount(() => { stopCountDown() }) return { showDesc, lableText, notSimple, showSimple, countDown } } }) </script> <style lang="stylus"> .count-down-wrap display: flex align-items: center justify-content: center margin-bottom: 10px !important color: black font-weight: 400; font-size: 13px font-family: PingFangSC-Regular line-height: 17px letter-spacing: 0 .count-down-text, .count-down-time color: black font-weight: 400 font-family: PingFangSC-Regular letter-spacing: 0 white-space: nowrap .count-down-time display: flex align-items: center justify-content: flex-start font-weight: 400; font-family: PingFangSC-Regular letter-spacing: 0 .full .count-down-text, .count-down-dot font-weight: 400 font-family: PingFangSC-Regular line-height: 17px; letter-spacing: 0 .count-down-day, .count-down-hour, .count-down-minute, .count-down-second margin: 0 4px color: black font-weight: 400 font-family: PingFangSC-Regular // font-weight bold line-height: 1 letter-spacing: 0 text-align: center border-radius: 2px </style>
|