app-goods-timer.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 + '-color'" v-if="timerStr">&nbsp;&nbsp;&nbsp;{{timerStr}}</view>
  8. </template>
  9. <template v-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-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. import {mapState} from "vuex";
  28. export default {
  29. name: "app-goods-timer",
  30. props: {
  31. startDateTime: String,
  32. endDateTime: String,
  33. listStyle: {
  34. type: Number,
  35. default() {
  36. return -1;
  37. }
  38. },
  39. sign: String,
  40. pageHide: Boolean,
  41. },
  42. data() {
  43. return {
  44. timeInterval: null,
  45. timer: null,
  46. timerStr: null
  47. };
  48. },
  49. computed: {
  50. ...mapState({
  51. theme: state => state.mallConfig.theme,
  52. }),
  53. time() {
  54. return {
  55. startDateTime: this.startDateTime,
  56. endDateTime: this.endDateTime,
  57. pageHide: this.pageHide,
  58. };
  59. },
  60. listClass() {
  61. if (this.listStyle === 1) {
  62. return `background`
  63. } else if (this.listStyle === 2) {
  64. return `background main-center`;
  65. } else {
  66. return ``;
  67. }
  68. },
  69. signName() {
  70. let name = '';
  71. if (this.sign === 'miaosha') {
  72. name = '秒杀';
  73. } else if (this.sign === 'bargain') {
  74. name = '砍价';
  75. } else if (this.sign === 'lottery') {
  76. name = '抽奖';
  77. } else if (this.sign === 'advance') {
  78. name = '预售';
  79. }
  80. return name;
  81. }
  82. },
  83. watch: {
  84. time: {
  85. handler(v) {
  86. if (this.pageHide) {
  87. clearInterval(this.timeInterval);
  88. return ;
  89. }
  90. let startDateTime = this.startDateTime;
  91. let endDateTime = this.endDateTime;
  92. startDateTime = startDateTime.replace(/-/g, '/');
  93. endDateTime = endDateTime.replace(/-/g, '/');
  94. let timerStr = null;
  95. let startTime = this.$utils.timeDifference(new Date().getTime(), new Date(startDateTime).getTime());
  96. if (startTime) {
  97. this.timer = '距开始 ';
  98. timerStr = (startTime['d'] > 0 ? startTime['d'] + '天' : '') + startTime['h'] + ':' + startTime['m'] + ':' + startTime['s'];
  99. }
  100. let endTime = null;
  101. if (!timerStr) {
  102. endTime = this.$utils.timeDifference(new Date().getTime(), new Date(endDateTime).getTime());
  103. if (endTime) {
  104. this.timer = '距结束 ';
  105. timerStr = (endTime['d'] > 0 ? endTime['d'] + '天' : '') + endTime['h'] + ':' + endTime['m'] + ':' + endTime['s'];
  106. }
  107. }
  108. if (!timerStr) {
  109. this.timer = '活动已结束';
  110. }
  111. this.timerStr = timerStr;
  112. this.timeInterval = setInterval(() => {
  113. let timerStr = null;
  114. let startTime = this.$utils.timeDifference(new Date().getTime(), new Date(startDateTime).getTime());
  115. if (startTime) {
  116. this.timer = '距开始 ';
  117. timerStr = (startTime['d'] > 0 ? startTime['d'] + '天' : '') + startTime['h'] + ':' + startTime['m'] + ':' + startTime['s'];
  118. }
  119. let endTime = null;
  120. if (!timerStr) {
  121. endTime = this.$utils.timeDifference(new Date().getTime(), new Date(endDateTime).getTime());
  122. if (endTime) {
  123. this.timer = '距结束 ';
  124. timerStr = (endTime['d'] > 0 ? endTime['d'] + '天' : '') + endTime['h'] + ':' + endTime['m'] + ':' + endTime['s'];
  125. }
  126. }
  127. if (!timerStr) {
  128. this.timer = '活动已结束';
  129. clearInterval(this.timeInterval);
  130. }
  131. this.timerStr = timerStr;
  132. }, 1000);
  133. },
  134. immediate: true
  135. }
  136. },
  137. beforeDestroy() {
  138. clearInterval(this.timeInterval);
  139. }
  140. }
  141. </script>
  142. <style scoped lang="scss">
  143. .app-goods-timer {
  144. font-size: $uni-font-size-weak-one;
  145. color: #ffffff;
  146. .img {
  147. width: #{24rpx};
  148. height: #{24rpx};
  149. display: block;
  150. margin-right: #{12rpx};
  151. }
  152. .timer-1 {
  153. color: $uni-general-color-two;
  154. }
  155. &.background {
  156. background: linear-gradient(left, #ff4544, #ff8b8b);
  157. }
  158. .app-column-1 {
  159. padding: 0 #{24rpx};
  160. height: #{80rpx};
  161. line-height: #{80rpx};
  162. width: 100%;
  163. .timer-1 {
  164. color: #ffffff;
  165. }
  166. .sign-name {
  167. font-size: $uni-font-size-import-two;
  168. }
  169. }
  170. .app-column-2 {
  171. height: #{44rpx};
  172. line-height: #{44rpx};
  173. .timer-1 {
  174. color: #ffffff;
  175. }
  176. }
  177. }
  178. </style>