app-goods-timer.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <template>
  2. <view class="dir-left-nowrap app-goods-timer cross-center" :class="listClass" :style="{'background-color': listStyle == 1 || listStyle == 2 ?theme.background :''}">
  3. <template v-if="listStyle === -1">
  4. <!-- 列表模式 -->
  5. <image class="box-grow-0 img" src="../../../static/image/icon/time.png"></image>
  6. <view class="box-grow-0 timer-1">{{timer}}</view>
  7. <view class="box-grow-1" :style="{'color': theme.color}" v-if="timerStr">&nbsp;&nbsp;&nbsp;{{timerStr}}</view>
  8. </template>
  9. <template v-else-if="listStyle === 1">
  10. <!-- 一行一个 -->
  11. <view class="dir-left-nowrap app-column-1">
  12. <view class="box-grow-1 sign-name">{{signName}}</view>
  13. <view class="box-grow-0 timer-1">{{timer}}</view>
  14. <view class="box-grow-0" v-if="timerStr">&nbsp;&nbsp;&nbsp;{{timerStr}}</view>
  15. </view>
  16. </template>
  17. <template v-else-if="listStyle === 2">
  18. <!-- 一行一个 -->
  19. <view class="box-grow-0 dir-left-nowrap app-column-2">
  20. <view class="box-grow-0 timer-1">{{timer}}</view>
  21. <view class="box-grow-1" v-if="timerStr">&nbsp;&nbsp;&nbsp;{{timerStr}}</view>
  22. </view>
  23. </template>
  24. </view>
  25. </template>
  26. <script>
  27. export default {
  28. name: "app-goods-timer",
  29. props: {
  30. startDateTime: String,
  31. endDateTime: String,
  32. listStyle: {
  33. type: Number,
  34. default() {
  35. return -1;
  36. }
  37. },
  38. sign: String,
  39. pageHide: Boolean,
  40. theme: Object,
  41. },
  42. data() {
  43. return {
  44. timeInterval: null,
  45. timer: null,
  46. timerStr: null
  47. };
  48. },
  49. computed: {
  50. time() {
  51. return {
  52. startDateTime: this.startDateTime,
  53. endDateTime: this.endDateTime,
  54. pageHide: this.pageHide,
  55. };
  56. },
  57. listClass() {
  58. let str = ``;
  59. if(this.listStyle == 2) {
  60. str = `main-center`
  61. }
  62. return str;
  63. },
  64. signName() {
  65. let name = '';
  66. switch (this.sign) {
  67. case 'miaosha':
  68. name = '秒杀';
  69. break;
  70. case 'bargain':
  71. name = '砍价';
  72. break;
  73. case 'lottery':
  74. name = '抽奖';
  75. break;
  76. case 'advance':
  77. name = '预售';
  78. break;
  79. case 'flash-sale':
  80. name = '限时抢购';
  81. break;
  82. }
  83. return name;
  84. },
  85. },
  86. methods: {
  87. timing(startDateTime, endDateTime) {
  88. let timerStr = null;
  89. let startTime = this.$utils.timeDifference(new Date().getTime(), new Date(startDateTime).getTime());
  90. if (startTime) {
  91. this.timer = '距开始 ';
  92. timerStr = (startTime['d'] > 0 ? startTime['d'] + '天' : '') + startTime['h'] + ':' + startTime['m'] + ':' + startTime['s'];
  93. }
  94. let endTime = null;
  95. if (!timerStr) {
  96. endTime = this.$utils.timeDifference(new Date().getTime(), new Date(endDateTime).getTime());
  97. if (endTime) {
  98. this.timer = '距结束 ';
  99. timerStr = (endTime['d'] > 0 ? endTime['d'] + '天' : '') + endTime['h'] + ':' + endTime['m'] + ':' + endTime['s'];
  100. }
  101. }
  102. if (!timerStr) {
  103. this.timer = '活动已结束';
  104. clearInterval(this.timeInterval);
  105. }
  106. this.timerStr = timerStr;
  107. }
  108. },
  109. watch: {
  110. time: {
  111. handler() {
  112. if (this.pageHide) {
  113. clearInterval(this.timeInterval);
  114. return ;
  115. }
  116. let startDateTime = this.startDateTime;
  117. let endDateTime = this.endDateTime;
  118. startDateTime = startDateTime.replace(/-/g, '/');
  119. endDateTime = endDateTime.replace(/-/g, '/');
  120. this.timing(startDateTime, endDateTime);
  121. this.timeInterval = setInterval(() => {
  122. this.timing(startDateTime, endDateTime);
  123. }, 1000);
  124. },
  125. immediate: true
  126. }
  127. },
  128. beforeDestroy() {
  129. clearInterval(this.timeInterval);
  130. }
  131. }
  132. </script>
  133. <style scoped lang="scss">
  134. .app-goods-timer {
  135. font-size: #{24upx};
  136. color: #ffffff;
  137. .img {
  138. width: #{24rpx};
  139. height: #{24rpx};
  140. display: block;
  141. margin-right: #{12rpx};
  142. }
  143. .timer-1 {
  144. color: $uni-general-color-two;
  145. }
  146. .app-column-1 {
  147. padding: 0 #{24rpx};
  148. height: #{80rpx};
  149. line-height: #{80rpx};
  150. width: 100%;
  151. .timer-1 {
  152. color: #ffffff;
  153. }
  154. .sign-name {
  155. text-align: left;
  156. font-size: $uni-font-size-import-two;
  157. }
  158. }
  159. .app-column-2 {
  160. height: #{44rpx};
  161. line-height: #{44rpx};
  162. .timer-1 {
  163. color: #ffffff;
  164. }
  165. }
  166. }
  167. </style>