巴青农资商城

OrderStatusBar.vue 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <template>
  2. <view class="order-status-bar">
  3. <text class="order-status-bar__text">{{ statusText }}</text>
  4. <text v-if="showCountdown" class="order-status-bar__countdown">
  5. 剩余 {{ countdownText }}
  6. </text>
  7. <text v-if="subText" class="order-status-bar__sub">{{ subText }}</text>
  8. </view>
  9. </template>
  10. <script setup>
  11. import { ref, computed, watch, onUnmounted } from 'vue'
  12. import { formatPayCountdown } from '@/utils/orderDisplay'
  13. const props = defineProps({
  14. statusText: {
  15. type: String,
  16. default: ''
  17. },
  18. payRemainSeconds: {
  19. type: Number,
  20. default: null
  21. },
  22. subText: {
  23. type: String,
  24. default: ''
  25. }
  26. })
  27. const remain = ref(props.payRemainSeconds)
  28. let timer = null
  29. const showCountdown = computed(() => remain.value != null && remain.value > 0)
  30. const countdownText = computed(() => formatPayCountdown(remain.value))
  31. function clearTimer() {
  32. if (timer) {
  33. clearInterval(timer)
  34. timer = null
  35. }
  36. }
  37. function startTimer() {
  38. clearTimer()
  39. if (props.payRemainSeconds == null || props.payRemainSeconds <= 0) {
  40. remain.value = props.payRemainSeconds
  41. return
  42. }
  43. remain.value = props.payRemainSeconds
  44. timer = setInterval(() => {
  45. if (remain.value > 0) {
  46. remain.value -= 1
  47. } else {
  48. clearTimer()
  49. }
  50. }, 1000)
  51. }
  52. watch(
  53. () => props.payRemainSeconds,
  54. () => startTimer(),
  55. { immediate: true }
  56. )
  57. onUnmounted(() => clearTimer())
  58. </script>
  59. <style lang="scss" scoped>
  60. .order-status-bar {
  61. padding: 32rpx 24rpx;
  62. background: linear-gradient(135deg, #43a047 0%, #2e7d32 100%);
  63. color: #fff;
  64. }
  65. .order-status-bar__text {
  66. display: block;
  67. font-size: 34rpx;
  68. font-weight: 600;
  69. }
  70. .order-status-bar__countdown {
  71. display: block;
  72. margin-top: 12rpx;
  73. font-size: 26rpx;
  74. opacity: 0.92;
  75. }
  76. .order-status-bar__sub {
  77. display: block;
  78. margin-top: 8rpx;
  79. font-size: 24rpx;
  80. opacity: 0.85;
  81. }
  82. </style>