app-diy-timer.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <template>
  2. <view class="app-timer">
  3. <app-jump-button form :url="link.url" :open_type="link.openType" arrangement="column">
  4. <view v-if="picUrl">
  5. <app-image :img-src="picUrl" mode="widthFix" width="750rpx" height="auto"></app-image>
  6. </view>
  7. <view :style="{'background-image': `url(${bgPicUrl ? bgPicUrl : '../../../static/image/icon/icon-timer-bg.png'})`}" class="timer dir-top-nowrap main-center"
  8. v-if="timerStr">
  9. <view v-if="startTime">距离活动开始还有</view>
  10. <view v-if="startTime === null && endTime">距离活动结束还有</view>
  11. <view v-if="startTime === null && endTime === null">活动已结束</view>
  12. <view>{{timerStr}}</view>
  13. </view>
  14. </app-jump-button>
  15. </view>
  16. </template>
  17. <script>
  18. export default {
  19. name: "app-timer",
  20. props: {
  21. picUrl: String,
  22. link: Object,
  23. startDateTime: {
  24. type: String,
  25. default() {
  26. return '2019-8-30 10:00:00';
  27. }
  28. },
  29. endDateTime: {
  30. type: String,
  31. default() {
  32. return '2019-8-30 10:00:00';
  33. }
  34. },
  35. pageHide: Boolean,
  36. bgPicUrl: String,
  37. },
  38. data() {
  39. return {
  40. timeInterval: null,
  41. startTime: null,
  42. endTime: null,
  43. timerStr: null
  44. };
  45. },
  46. computed: {
  47. time() {
  48. return {
  49. startDateTime: this.startDateTime,
  50. endDateTime: this.endDateTime,
  51. pageHide: this.pageHide,
  52. };
  53. }
  54. },
  55. beforeDestroy() {
  56. clearInterval(this.timeInterval);
  57. },
  58. watch: {
  59. time: {
  60. handler() {
  61. if (this.pageHide) {
  62. clearInterval(this.timeInterval);
  63. return ;
  64. }
  65. let startDateTime = this.startDateTime;
  66. let endDateTime = this.endDateTime;
  67. this.timeInterval = setInterval(() => {
  68. let startTime = null, endTime = null, timerStr = null;
  69. if (startDateTime) {
  70. startDateTime = startDateTime.replace(/-/g, '/');
  71. startTime = this.$utils.timeDifference(new Date().getTime(), new Date(startDateTime).getTime());
  72. if (startTime) {
  73. timerStr = (startTime['d'] > 0 ? startTime['d'] + '天' : '') + startTime['h'] + '小时' + startTime['m'] + '分' + startTime['s'] + '秒';
  74. }
  75. }
  76. if (endDateTime && !timerStr) {
  77. endDateTime = endDateTime.replace(/-/g, '/');
  78. endTime = this.$utils.timeDifference(new Date().getTime(), new Date(endDateTime).getTime());
  79. if (endTime) {
  80. timerStr = (endTime['d'] > 0 ? endTime['d'] + '天' : '') + endTime['h'] + '小时' + endTime['m'] + '分' + endTime['s'] + '秒';
  81. }
  82. }
  83. this.startTime = startTime;
  84. this.endTime = endTime;
  85. this.timerStr = timerStr;
  86. }, 1000);
  87. },
  88. immediate: true
  89. }
  90. }
  91. }
  92. </script>
  93. <style scoped lang="scss">
  94. .timer {
  95. width: 100%;
  96. height: #{140rpx};
  97. padding: #{24rpx};
  98. font-size: $uni-font-size-general-one;
  99. color: #ffffff;
  100. background-repeat: no-repeat;
  101. background-size: 100% 100%;
  102. background-position: center;
  103. }
  104. </style>