app-goods-timer.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <template>
  2. <view class="dir-left-nowrap app-goods-timer cross-center" :class="listClass">
  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" :class="theme + '-m-text ' + theme" 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: String,
  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. switch (this.listStyle) {
  60. case 1:
  61. str = `${this.theme}-m-back ${this.theme}`;
  62. break;
  63. case 2:
  64. str = `${this.theme}-m-back ${this.theme} main-center`;
  65. break;
  66. default:
  67. str = '';
  68. break;
  69. }
  70. return str;
  71. },
  72. signName() {
  73. let name = '';
  74. switch (this.sign) {
  75. case 'miaosha':
  76. name = '秒杀';
  77. break;
  78. case 'bargain':
  79. name = '砍价';
  80. break;
  81. case 'lottery':
  82. name = '抽奖';
  83. break;
  84. case 'advance':
  85. name = '预售';
  86. break;
  87. case 'flash-sale':
  88. name = '限时抢购';
  89. break;
  90. }
  91. return name;
  92. },
  93. },
  94. methods: {
  95. timing(startDateTime, endDateTime) {
  96. let timerStr = null;
  97. let startTime = this.$utils.timeDifference(new Date().getTime(), new Date(startDateTime).getTime());
  98. if (startTime) {
  99. this.timer = '距开始 ';
  100. timerStr = (startTime['d'] > 0 ? startTime['d'] + '天' : '') + startTime['h'] + ':' + startTime['m'] + ':' + startTime['s'];
  101. }
  102. let endTime = null;
  103. if (!timerStr) {
  104. endTime = this.$utils.timeDifference(new Date().getTime(), new Date(endDateTime).getTime());
  105. if (endTime) {
  106. this.timer = '距结束 ';
  107. timerStr = (endTime['d'] > 0 ? endTime['d'] + '天' : '') + endTime['h'] + ':' + endTime['m'] + ':' + endTime['s'];
  108. }
  109. }
  110. if (!timerStr) {
  111. this.timer = '活动已结束';
  112. clearInterval(this.timeInterval);
  113. }
  114. this.timerStr = timerStr;
  115. }
  116. },
  117. watch: {
  118. time: {
  119. handler() {
  120. if (this.pageHide) {
  121. clearInterval(this.timeInterval);
  122. return ;
  123. }
  124. let startDateTime = this.startDateTime;
  125. let endDateTime = this.endDateTime;
  126. startDateTime = startDateTime.replace(/-/g, '/');
  127. endDateTime = endDateTime.replace(/-/g, '/');
  128. this.timing(startDateTime, endDateTime);
  129. this.timeInterval = setInterval(() => {
  130. this.timing(startDateTime, endDateTime);
  131. }, 1000);
  132. },
  133. immediate: true
  134. }
  135. },
  136. beforeDestroy() {
  137. clearInterval(this.timeInterval);
  138. }
  139. }
  140. </script>
  141. <style scoped lang="scss">
  142. .app-goods-timer {
  143. font-size: #{24upx};
  144. color: #ffffff;
  145. .img {
  146. width: #{24rpx};
  147. height: #{24rpx};
  148. display: block;
  149. margin-right: #{12rpx};
  150. }
  151. .timer-1 {
  152. color: $uni-general-color-two;
  153. }
  154. .app-column-1 {
  155. padding: 0 #{24rpx};
  156. height: #{80rpx};
  157. line-height: #{80rpx};
  158. width: 100%;
  159. .timer-1 {
  160. color: #ffffff;
  161. }
  162. .sign-name {
  163. text-align: left;
  164. font-size: $uni-font-size-import-two;
  165. }
  166. }
  167. .app-column-2 {
  168. height: #{44rpx};
  169. line-height: #{44rpx};
  170. .timer-1 {
  171. color: #ffffff;
  172. }
  173. }
  174. }
  175. </style>