You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
3.2 KiB

  1. <template>
  2. <view class="countdown">
  3. <van-circle v-model:current-rate="currentRate" :rate="rate" :speed="100" :color="gradientColor" size="600rpx">
  4. <template #default>
  5. <view class="countdown-main">
  6. <van-count-down :time="countTime" @change="calculateCountdownPercentage(countTime,startTime)">
  7. <template #default="timeData">
  8. <view class="countdown-title">剩余服务时间</view>
  9. <view class="a1">
  10. <span class="block">{{ addZero(timeData.hours) }}</span>
  11. <span class="colon">:</span>
  12. <span class="block">{{ addZero(timeData.minutes) }}</span>
  13. <span class="colon">:</span>
  14. <span class="block">{{ addZero(timeData.seconds) }}</span>
  15. </view>
  16. </template>
  17. </van-count-down>
  18. </view>
  19. </template>
  20. </van-circle>
  21. </view>
  22. </template>
  23. <script>
  24. export default {
  25. data() {
  26. return {
  27. currentRate: 0,
  28. rate : 0,
  29. gradientColor: {
  30. '0%': '#4899a6',
  31. '100%': '#6fc6ad',
  32. },
  33. startTime : 0,
  34. endTime: 0,
  35. countTime : 0,
  36. audio: new Audio(),
  37. worker: null
  38. }
  39. },
  40. onShow() {
  41. //初始化开始、结束时间
  42. this.startTime = new Date(this.$route.query.startTime).valueOf();
  43. this.endTime = this.startTime + (this.$route.query.time ? this.$route.query.time * 1000 * 60 : 0)
  44. this.countTime = this.endTime - Date.now() //倒计时时间戳(用于倒计时组件)
  45. let self = this;
  46. this.audio.src = '/static/music/server-time.mp3'
  47. this.worker = new Worker('/static/work.js')
  48. this.worker.postMessage({
  49. startTime : this.startTime,
  50. endTime : this.endTime
  51. });
  52. this.worker.onmessage = function(event) {
  53. self.play()
  54. }
  55. },
  56. onUnload(){
  57. this.worker.terminate()
  58. this.audio.pause()
  59. this.audio = null
  60. },
  61. methods: {
  62. //播放音频
  63. play() {
  64. this.audio.play()
  65. this.worker.terminate()
  66. },
  67. // 计算时间百分比
  68. calculateCountdownPercentage() {
  69. let totalTime = this.endTime - this.startTime;
  70. let elapsedTime = Date.now() - this.startTime;
  71. // 计算进度百分比
  72. let percentage = (elapsedTime / totalTime) * 100;
  73. // 如果百分比超过100%,则将其设置为100%
  74. this.rate = Math.min(percentage, 100);
  75. },
  76. //添加0
  77. addZero(time){
  78. if(time < 10){
  79. return '0' + time
  80. }
  81. return time
  82. }
  83. }
  84. }
  85. </script>
  86. <style lang="scss" scoped>
  87. .countdown {
  88. min-height: 100vh;
  89. display: flex;
  90. align-items: center;
  91. justify-content: center;
  92. background: #f7f7f7;
  93. width: 750rpx;
  94. margin: 0rpx auto;
  95. .countdown-main {
  96. display: flex;
  97. width: 600rpx;
  98. height: 600rpx;
  99. justify-content: center;
  100. align-items: center;
  101. .countdown-title {
  102. text-align: center;
  103. margin-bottom: 30rpx;
  104. font-size: 35rpx;
  105. color: #8f8f8f;
  106. }
  107. .a1 {
  108. display: flex;
  109. align-items: center;
  110. height: 80rpx;
  111. .colon {
  112. margin: 0 8rpx;
  113. color: black;
  114. }
  115. .block {
  116. display: flex;
  117. align-items: center;
  118. justify-content: center;
  119. width: 80rpx;
  120. height: 80rpx;
  121. color: #fff;
  122. font-size: 28rpx;
  123. font-weight: bold;
  124. background-color: #4899a6;
  125. border-radius: 10rpx;
  126. margin: 0rpx 15rpx;
  127. }
  128. }
  129. }
  130. }
  131. </style>