app-diy-timer.vue 4.3 KB

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