countdown.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <template>
  2. <!--倒计时-->
  3. <view class="countdown">
  4. <template v-if="config.type == null">
  5. <text v-if="status == 0">{{title}}</text>
  6. <text v-if="status == 1">活动具体时间:</text>
  7. <text v-if="status == 2">活动结束时间:</text>
  8. <text class="box" :style="'background-color: '+back_color+';color:'+cut_color">{{ day }}</text>
  9. <text class="p-0-10" :style="'color:'+color">天</text>
  10. <text class="box" :style="'background-color: '+back_color+';color:'+cut_color">{{ hour }}</text>
  11. <text class="p-0-10" :style="'color:'+color">:</text>
  12. <text class="box" :style="'background-color: '+back_color+';color:'+cut_color">{{ minute }}</text>
  13. <text class="p-0-10" :style="'color:'+color">:</text>
  14. <text class="box" :style="'background-color: '+back_color+';color:'+cut_color">{{ second }}</text>
  15. <text class="p-0-10" :style="'color:'+color"></text>
  16. </template>
  17. <template v-if="config.type === 'text'">
  18. {{title}}{{ parseInt(day * 24) + parseInt(hour) }}:{{ minute }}:{{ second }}
  19. </template>
  20. </view>
  21. </template>
  22. <script>
  23. export default {
  24. data() {
  25. return {
  26. /*状态*/
  27. status: 0,
  28. /*天*/
  29. day: '0',
  30. /*时*/
  31. hour: '0',
  32. /*分*/
  33. minute: '0',
  34. /*秒*/
  35. second: '0',
  36. /*定时器*/
  37. timer: null,
  38. /*剩余秒*/
  39. totalSeconds: 0,
  40. /*标题*/
  41. title: '活动剩余:'
  42. };
  43. },
  44. props: {
  45. config: {
  46. type: Object,
  47. default: function() {
  48. return {
  49. type: 'all',
  50. };
  51. }
  52. },
  53. back_color: {
  54. type: String,
  55. default: function() {
  56. return {
  57. type: 'all',
  58. };
  59. }
  60. },
  61. cut_color: {
  62. type: String,
  63. default: function() {
  64. return {
  65. type: 'all',
  66. };
  67. }
  68. },
  69. color: {
  70. type: String,
  71. default: function() {
  72. return {
  73. type: 'all',
  74. };
  75. }
  76. }
  77. },
  78. created() {},
  79. watch: {
  80. config: {
  81. deep: true,
  82. handler: function(n, o) {
  83. if (n != o && n.endstamp != 0) {
  84. if (n.title && typeof n.title != 'undefined') {
  85. this.title = n.title;
  86. }
  87. this.setTime();
  88. }
  89. },
  90. immediate: true
  91. }
  92. },
  93. methods: {
  94. /*轮循*/
  95. setTime() {
  96. let self = this;
  97. self.timer = setInterval(function() {
  98. self.init();
  99. }, 1000);
  100. },
  101. /*初始化*/
  102. init() {
  103. let nowseconds = Date.now() / 1000;
  104. if (nowseconds < this.config.startstamp) {
  105. //活动时间还没到
  106. this.status = 1;
  107. } else if (nowseconds > this.config.endstamp) {
  108. //活动已过期
  109. this.status = 2;
  110. } else {
  111. //活动进行中
  112. this.totalSeconds = parseInt(this.config.endstamp - nowseconds);
  113. this.status = 0;
  114. this.countDown();
  115. }
  116. this.$emit('returnVal', this.status);
  117. },
  118. /*计算时分秒*/
  119. countDown() {
  120. let totalSeconds = this.totalSeconds;
  121. //天数
  122. let day = Math.floor(totalSeconds / (60 * 60 * 24));
  123. //取模(余数)
  124. let modulo = totalSeconds % (60 * 60 * 24);
  125. //小时数
  126. let hour = Math.floor(modulo / (60 * 60));
  127. modulo = modulo % (60 * 60);
  128. //分钟
  129. let minute = Math.floor(modulo / 60);
  130. //秒
  131. let second = modulo % 60;
  132. this.day = this.convertTwo(day);
  133. this.hour = this.convertTwo(hour);
  134. this.minute = this.convertTwo(minute);
  135. this.second = this.convertTwo(second);
  136. this.totalSeconds--;
  137. },
  138. /*转两位数*/
  139. convertTwo(n) {
  140. let str = '';
  141. if (n < 10) {
  142. str = '0' + n;
  143. } else {
  144. str = n;
  145. }
  146. return str;
  147. },
  148. /*把时间戳转普通时间*/
  149. getLocalTime(nS) {
  150. return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/, ' ');
  151. },
  152. clear() {
  153. console.log(1)
  154. clearInterval(this.timer);
  155. this.timer = null;
  156. },
  157. },
  158. destroyed() {
  159. clearInterval(this.timer);
  160. }
  161. };
  162. </script>
  163. <style>
  164. .countdown .box {
  165. display: inline-block;
  166. padding: 4rpx;
  167. /* width: 34rpx; */
  168. border-radius: 8rpx;
  169. background: #000000;
  170. text-align: center;
  171. color: #ffffff;
  172. }
  173. </style>