uni-countdown.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <template>
  2. <view class="uni-countdown">
  3. <text v-if="showDay" :style="{ borderColor: borderColor, color: color, backgroundColor: backgroundColor, width: borderWidth }" class="uni-countdown__number">{{ d }}</text>
  4. <text v-if="showDay" :style="{ color: splitorColor }" class="uni-countdown__splitor">天</text>
  5. <text :style="{ borderColor: borderColor, color: color, backgroundColor: backgroundColor, width: borderWidth }" class="uni-countdown__number">{{ h }}</text>
  6. <text :style="{ color: splitorColor }" class="uni-countdown__splitor">{{ showColon ? ':' : '时' }}</text>
  7. <text :style="{ borderColor: borderColor, color: color, backgroundColor: backgroundColor, width: borderWidth }" class="uni-countdown__number">{{ i }}</text>
  8. <text :style="{ color: splitorColor }" class="uni-countdown__splitor">{{ showColon ? ':' : '分' }}</text>
  9. <text :style="{ borderColor: borderColor, color: color, backgroundColor: backgroundColor, width: borderWidth }" class="uni-countdown__number">{{ s }}</text>
  10. <text v-if="!showColon" :style="{ color: splitorColor }" class="uni-countdown__splitor">秒</text>
  11. </view>
  12. </template>
  13. <script>
  14. export default {
  15. name: 'UniCountdown',
  16. props: {
  17. showDay: {
  18. type: Boolean,
  19. default: true
  20. },
  21. showColon: {
  22. type: Boolean,
  23. default: true
  24. },
  25. backgroundColor: {
  26. type: String,
  27. default: '#FFFFFF'
  28. },
  29. borderColor: {
  30. type: String,
  31. default: '#000000'
  32. },
  33. color: {
  34. type: String,
  35. default: '#000000'
  36. },
  37. splitorColor: {
  38. type: String,
  39. default: '#000000'
  40. },
  41. day: {
  42. type: Number,
  43. default: 0
  44. },
  45. hour: {
  46. type: Number,
  47. default: 0
  48. },
  49. minute: {
  50. type: Number,
  51. default: 0
  52. },
  53. second: {
  54. type: Number,
  55. default: 0
  56. },
  57. borderWidth: {
  58. type: String,
  59. default: "52rpx"
  60. }
  61. },
  62. data() {
  63. return {
  64. timer: null,
  65. syncFlag: false,
  66. d: '00',
  67. h: '00',
  68. i: '00',
  69. s: '00',
  70. leftTime: 0,
  71. seconds: 0
  72. }
  73. },
  74. watch: {
  75. day(val) {
  76. this.changeFlag()
  77. },
  78. hour(val) {
  79. this.changeFlag()
  80. },
  81. minute(val) {
  82. this.changeFlag()
  83. },
  84. second(val) {
  85. this.changeFlag()
  86. }
  87. },
  88. created: function(e) {
  89. this.startData();
  90. },
  91. beforeDestroy() {
  92. clearInterval(this.timer)
  93. },
  94. methods: {
  95. toSeconds(day, hours, minutes, seconds) {
  96. return day * 60 * 60 * 24 + hours * 60 * 60 + minutes * 60 + seconds
  97. },
  98. timeUp() {
  99. clearInterval(this.timer)
  100. this.timer = 0;
  101. // 优化建议:提前1秒去请求
  102. this.$emit('timeup', this)
  103. },
  104. countDown() {
  105. let seconds = this.seconds
  106. let [day, hour, minute, second] = [0, 0, 0, 0]
  107. if (seconds > 0) {
  108. day = Math.floor(seconds / (60 * 60 * 24))
  109. hour = Math.floor(seconds / (60 * 60)) - (day * 24)
  110. minute = Math.floor(seconds / 60) - (day * 24 * 60) - (hour * 60)
  111. second = Math.floor(seconds) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60)
  112. } else {
  113. this.timeUp()
  114. }
  115. if (day < 10) {
  116. day = '0' + day
  117. }
  118. if (hour < 10) {
  119. hour = '0' + hour
  120. }
  121. if (minute < 10) {
  122. minute = '0' + minute
  123. }
  124. if (second < 10) {
  125. second = '0' + second
  126. }
  127. this.d = day
  128. this.h = hour
  129. this.i = minute
  130. this.s = second
  131. },
  132. startData() {
  133. this.seconds = this.toSeconds(this.day, this.hour, this.minute, this.second)
  134. if (this.seconds <= 0) {
  135. this.timeUp()
  136. return
  137. }
  138. if (this.timer > 0){
  139. return;
  140. }
  141. this.countDown()
  142. this.timer = setInterval(() => {
  143. this.seconds--
  144. if (this.seconds < 0) {
  145. this.timeUp()
  146. return
  147. }
  148. this.countDown()
  149. }, 1000)
  150. //console.log('timer:' + this.timer);
  151. },
  152. changeFlag() {
  153. if (!this.syncFlag) {
  154. this.seconds = this.toSeconds(this.day, this.hour, this.minute, this.second)
  155. this.startData();
  156. this.syncFlag = true;
  157. }
  158. }
  159. }
  160. }
  161. </script>
  162. <style lang="scss" scoped>
  163. @import '~@/uni.scss';
  164. $countdown-height: 48rpx;
  165. $countdown-width: 52rpx;
  166. .uni-countdown {
  167. /* #ifndef APP-NVUE */
  168. display: flex;
  169. /* #endif */
  170. flex-direction: row;
  171. justify-content: flex-start;
  172. padding: 2rpx 0;
  173. }
  174. .uni-countdown__splitor {
  175. /* #ifndef APP-NVUE */
  176. display: flex;
  177. /* #endif */
  178. justify-content: center;
  179. line-height: $countdown-height;
  180. padding: 5rpx;
  181. font-size: $uni-font-size-sm;
  182. }
  183. .uni-countdown__number {
  184. /* #ifndef APP-NVUE */
  185. display: flex;
  186. /* #endif */
  187. justify-content: center;
  188. align-items: center;
  189. width: $countdown-width;
  190. height: $countdown-height;
  191. line-height: $countdown-height;
  192. margin: 5rpx;
  193. text-align: center;
  194. font-size: $uni-font-size-sm;
  195. }
  196. </style>