gui-count-down.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <template name="gui-count-down">
  2. <view class="gui-flex gui-rows gui-nowrap gui-align-items-center"
  3. v-if="show && timer != ''">
  4. <text class="gui-countdown-numbers gui-border"
  5. :style="{
  6. borderColor:borderColor,
  7. width:size*lineHeight+'rpx', marginRight:spacing,
  8. height:size*lineHeight+'rpx', lineHeight:size*lineHeight+'rpx', fontSize:size+'rpx',
  9. color:fontColor, backgroundColor:bgColor}"
  10. v-if="d > 0">{{d}}</text>
  11. <text class="gui-countdown-splitor"
  12. :style="{color:splitorColor, fontSize:size+'rpx', marginRight:spacing}"
  13. v-if="d > 0">{{splitorText[0]}}</text>
  14. <text class="gui-countdown-numbers gui-border"
  15. :style="{
  16. borderColor:borderColor,
  17. width:size*lineHeight+'rpx', marginRight:spacing,
  18. height:size*lineHeight+'rpx', lineHeight:size*lineHeight+'rpx', fontSize:size+'rpx',
  19. color:fontColor, backgroundColor:bgColor}"
  20. v-if="(h != '00' || zeroShow)">{{h}}</text>
  21. <text class="gui-countdown-splitor"
  22. :style="{color:splitorColor, fontSize:size+'rpx', marginRight:spacing}"
  23. v-if="(h != '00' || zeroShow)">{{splitorText[1]}}</text>
  24. <text class="gui-countdown-numbers gui-border"
  25. :style="{
  26. borderColor:borderColor,
  27. width:size*lineHeight+'rpx', marginRight:spacing,
  28. height:size*lineHeight+'rpx', lineHeight:size*lineHeight+'rpx', fontSize:size+'rpx',
  29. color:fontColor, backgroundColor:bgColor}">{{i}}</text>
  30. <text class="gui-countdown-splitor"
  31. :style="{color:splitorColor, fontSize:size+'rpx', marginRight:spacing}">{{splitorText[2]}}</text>
  32. <text class="gui-countdown-numbers gui-border"
  33. :style="{
  34. borderColor:borderColor,
  35. width:size*lineHeight+'rpx', marginRight:spacing,
  36. height:size*lineHeight+'rpx', lineHeight:size*lineHeight+'rpx', fontSize:size+'rpx',
  37. color:fontColor, backgroundColor:bgColor}">{{s}}</text>
  38. <text class="gui-countdown-splitor"
  39. :style="{color:splitorColor, fontSize:size+'rpx'}">{{splitorText[3]}}</text>
  40. </view>
  41. </template>
  42. <script>
  43. export default {
  44. name : "gui-count-down",
  45. props : {
  46. bgColor : { type: String, default : "#FFFFFF" },
  47. borderColor : { type:String, default : "#FFFFFF"},
  48. fontColor : { type: String, default : "#2B2E3D" },
  49. size : { type: Number, default : 26},
  50. lineHeight : { type: Number, default : 1.8},
  51. splitorColor : { type: String, default : "rgba(69, 90, 100, 0.6)" },
  52. timer : { type:String, default : "" },
  53. splitorText : { type : Array,
  54. default : function () {
  55. return ['天', '时', '分', '秒']
  56. }
  57. },
  58. show : {type:Boolean, default:true},
  59. zeroShow : {type:Boolean, default:true},
  60. spacing : {type:String, default:'0rpx'}
  61. },
  62. data() {
  63. return {
  64. d : 0,
  65. h : "",
  66. i : "",
  67. s : "",
  68. leftTime : 0,
  69. outTimer : null,
  70. timerIn : '',
  71. leftTimeNum : 0
  72. }
  73. },
  74. created : function(){
  75. this.timerIn = this.timer;
  76. this.runbase();
  77. },
  78. watch : {
  79. timer : function(){
  80. this.timerIn = this.timer;
  81. this.runbase();
  82. }
  83. },
  84. methods : {
  85. runbase : function(){
  86. var reg = /^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})$/;
  87. var res = this.timerIn.match(reg);
  88. if (res == null){ return false;}
  89. var year = parseInt(res[1]);
  90. if (year < 1000) { return false; }
  91. var month = parseInt(res[2]);
  92. var day = parseInt(res[3]);
  93. var h = parseInt(res[4]);
  94. if (h < 0 || h > 24) { return false; }
  95. var i = parseInt(res[5]);
  96. if (i < 0 || i > 60) { return false; }
  97. var s = parseInt(res[6]);
  98. if (s < 0 || s > 60) { return false; }
  99. var leftTime = new Date(year, month - 1, day, h, i, s);
  100. this.leftTime = leftTime;
  101. clearTimeout(this.outTimer);
  102. this.countDown();
  103. },
  104. countDown: function (){
  105. var leftTime = this.leftTime - new Date();
  106. this.leftTimeNum = leftTime;
  107. if (leftTime > 0) {
  108. var day = parseInt(leftTime / (1000 * 60 * 60 * 24));
  109. var hours = parseInt((leftTime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  110. var minutes = parseInt((leftTime % (1000 * 60 * 60)) / (1000 * 60));
  111. var seconds = parseInt((leftTime % (1000 * 60)) / 1000);
  112. if (hours < 10) { hours = '0' + hours;}
  113. if (minutes < 10) { minutes = '0' + minutes; }
  114. if (seconds < 10) { seconds = '0' + seconds; }
  115. this.h = hours;
  116. this.i = minutes;
  117. this.s = seconds;
  118. this.d = day;
  119. this.outTimer = setTimeout(()=>{this.countDown();}, 1000);
  120. }else{
  121. clearTimeout(this.outTimer);
  122. this.h = '00';
  123. this.i = '00';
  124. this.s = '00';
  125. this.d = 0;
  126. this.$emit('endDo');
  127. }
  128. },
  129. reSetTimer : function(timer){
  130. clearTimeout(this.outTimer);
  131. this.timerIn = timer;
  132. this.runbase();
  133. },
  134. getTimeRemaining : function(){
  135. if(this.leftTimeNum < 0){return 0;}
  136. return parseInt(this.leftTimeNum / 1000);
  137. }
  138. }
  139. }
  140. </script>
  141. <style scoped>
  142. .gui-countdown-splitor{padding:0 5rpx;}
  143. .gui-countdown-numbers{border-radius:8rpx; text-align:center;}
  144. </style>