gui-count-down.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <template>
  2. <view
  3. class="gui-flex gui-row gui-nowrap gui-align-items-center"
  4. v-if="show && timer != ''">
  5. <text
  6. :class="customClass"
  7. class="gui-countdown-numbers gui-border gui-block"
  8. :style="{
  9. borderColor:borderColor+' !important',
  10. width:size*lineHeight+'rpx',
  11. marginRight:spacing,
  12. height:size*lineHeight+'rpx',
  13. lineHeight:size*lineHeight+'rpx',
  14. fontSize:size+'rpx',
  15. }"
  16. v-if="d > 0">{{d}}</text>
  17. <text
  18. class="gui-countdown-splitor gui-block"
  19. :class="splitorClass"
  20. :style="{
  21. height:size*lineHeight+'rpx',
  22. lineHeight:size*lineHeight+'rpx',
  23. fontSize:size+'rpx',
  24. marginRight:spacing}"
  25. v-if="d > 0">{{splitorText[0]}}</text>
  26. <text
  27. :class="customClass"
  28. class="gui-countdown-numbers gui-border gui-block"
  29. :style="{
  30. borderColor:borderColor+' !important',
  31. width:size*lineHeight+'rpx',
  32. marginRight:spacing,
  33. height:size*lineHeight+'rpx',
  34. lineHeight:size*lineHeight+'rpx',
  35. fontSize:size+'rpx'
  36. }"
  37. v-if="(h != '00' || zeroShow)">{{h}}</text>
  38. <text
  39. :class="splitorClass"
  40. class="gui-countdown-splitor gui-block"
  41. :style="{
  42. fontSize:size+'rpx',
  43. height:size*lineHeight+'rpx',
  44. lineHeight:size*lineHeight+'rpx',
  45. marginRight:spacing
  46. }"
  47. v-if="(h != '00' || zeroShow)">{{splitorText[1]}}</text>
  48. <text
  49. :class="customClass"
  50. class="gui-countdown-numbers gui-border gui-block"
  51. :style="{
  52. borderColor:borderColor+' !important',
  53. width:size*lineHeight+'rpx',
  54. marginRight:spacing,
  55. height:size*lineHeight+'rpx',
  56. lineHeight:size*lineHeight+'rpx',
  57. fontSize:size+'rpx'
  58. }">{{i}}</text>
  59. <text
  60. :class="splitorClass"
  61. class="gui-countdown-splitor gui-block"
  62. :style="{
  63. fontSize:size+'rpx',
  64. marginRight:spacing,
  65. height:size*lineHeight+'rpx',
  66. lineHeight:size*lineHeight+'rpx',
  67. }">{{splitorText[2]}}</text>
  68. <text
  69. :class="customClass"
  70. class="gui-countdown-numbers gui-border gui-block"
  71. :style="{
  72. borderColor:borderColor+' !important',
  73. width:size*lineHeight+'rpx',
  74. marginRight:spacing,
  75. height:size*lineHeight+'rpx',
  76. lineHeight:size*lineHeight+'rpx',
  77. fontSize:size+'rpx'
  78. }">{{s}}</text>
  79. <text
  80. :class="splitorClass"
  81. class="gui-countdown-splitor gui-block"
  82. :style="{
  83. fontSize:size+'rpx',
  84. height:size*lineHeight+'rpx',
  85. lineHeight:size*lineHeight+'rpx',
  86. }">{{splitorText[3]}}</text>
  87. </view>
  88. </template>
  89. <script>
  90. export default {
  91. name : "gui-count-down",
  92. props : {
  93. size : { type: Number, default : 26},
  94. lineHeight : { type: Number, default : 1.8},
  95. timer : { type:String, default : "" },
  96. splitorText : { type : Array,
  97. default : function () {
  98. return ['天', '时', '分', '秒']
  99. }
  100. },
  101. show : {type:Boolean, default:true},
  102. zeroShow : {type:Boolean, default:true},
  103. spacing : {type:String, default:'0rpx'},
  104. customClass : {type:Array, default:function(){
  105. return ['gui-primary-text'];
  106. }},
  107. splitorClass : {type:Array, default:function(){
  108. return ['gui-primary-text'];
  109. }},
  110. borderColor : {type:String, default:'rgba(255,255,255,0)'}
  111. },
  112. data() {
  113. return {
  114. d : 0,
  115. h : "",
  116. i : "",
  117. s : "",
  118. leftTime : 0,
  119. outTimer : null,
  120. timerIn : '',
  121. leftTimeNum : 0
  122. }
  123. },
  124. created : function(){
  125. this.timerIn = this.timer;
  126. this.runbase();
  127. },
  128. watch : {
  129. timer : function(){
  130. this.timerIn = this.timer;
  131. this.runbase();
  132. }
  133. },
  134. methods : {
  135. runbase : function(){
  136. var reg = /^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})$/;
  137. var res = this.timerIn.match(reg);
  138. if (res == null){ return false;}
  139. var year = parseInt(res[1]);
  140. if (year < 1000) { return false; }
  141. var month = parseInt(res[2]);
  142. var day = parseInt(res[3]);
  143. var h = parseInt(res[4]);
  144. if (h < 0 || h > 24) { return false; }
  145. var i = parseInt(res[5]);
  146. if (i < 0 || i > 60) { return false; }
  147. var s = parseInt(res[6]);
  148. if (s < 0 || s > 60) { return false; }
  149. var leftTime = new Date(year, month - 1, day, h, i, s);
  150. this.leftTime = leftTime;
  151. clearTimeout(this.outTimer);
  152. this.countDown();
  153. },
  154. countDown: function (){
  155. var leftTime = this.leftTime - new Date();
  156. this.leftTimeNum = leftTime;
  157. if (leftTime > 0) {
  158. var day = parseInt(leftTime / (1000 * 60 * 60 * 24));
  159. var hours = parseInt((leftTime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  160. var minutes = parseInt((leftTime % (1000 * 60 * 60)) / (1000 * 60));
  161. var seconds = parseInt((leftTime % (1000 * 60)) / 1000);
  162. if (hours < 10) { hours = '0' + hours;}
  163. if (minutes < 10) { minutes = '0' + minutes; }
  164. if (seconds < 10) { seconds = '0' + seconds; }
  165. this.h = hours;
  166. this.i = minutes;
  167. this.s = seconds;
  168. this.d = day;
  169. this.outTimer = setTimeout(()=>{this.countDown();}, 1000);
  170. }else{
  171. clearTimeout(this.outTimer);
  172. this.h = '00';
  173. this.i = '00';
  174. this.s = '00';
  175. this.d = 0;
  176. this.$emit('endDo');
  177. }
  178. },
  179. reSetTimer : function(timer){
  180. clearTimeout(this.outTimer);
  181. this.timerIn = timer;
  182. this.runbase();
  183. },
  184. getTimeRemaining : function(){
  185. if(this.leftTimeNum < 0){return 0;}
  186. return parseInt(this.leftTimeNum / 1000);
  187. }
  188. },
  189. emits : ['endDo']
  190. }
  191. </script>
  192. <style scoped>
  193. .gui-countdown-splitor{padding:0 5rpx;}
  194. .gui-countdown-numbers{border-radius:8rpx; text-align:center;}
  195. </style>