diff --git a/pages/shop/pay.vue b/pages/shop/pay.vue index 5b22bed..ae0f6af 100644 --- a/pages/shop/pay.vue +++ b/pages/shop/pay.vue @@ -10,9 +10,12 @@ ¥{{fullPrice | formatPrice}} - + 支付剩余时间 - {{ countdown }} + {{ countdown }} + + + 订单加载中··· @@ -42,7 +45,7 @@ return { order_sn: '', //订单号 fullPrice: 0, //总价格 包括附加费用 - order_time: 0, //订单创建时间 + order_time: -1, //订单创建时间 countdown: '', // 用于显示倒计时 timer: null, // 存储计时器 } @@ -51,51 +54,58 @@ this.$store.dispatch('fetchOrderList') this.order_sn = options.order_sn // 从提交订单页面传递过来的订单号 this.fullPrice = Number(options.fullPrice) // 从提交订单页面传递过来的订单号 - //倒计时 - this.timer = setInterval(() => { - if (this.order_time && this.order_time > 0) { - let tempTime = this.order_time + 100 - Math.floor(Date.now() / 1000) - if (tempTime <= 0) { - this.countdown = '00:00' - clearInterval(this.timer) // 清除定时器 - // 倒计时结束后跳转到首页 - uni.navigateTo({ - url: '/pages/index/index' // 非 Tab 页的路径 - }) - } else { - this.countdown = this.formatSeconds(tempTime) - } - } else { - this.countdown = '00:00' - clearInterval(this.timer) // 清除定时器 - // 如果没有有效的 order_time,也跳转到首页 - uni.switchTab({ - url: '/pages/index/index' // 首页的路径 - }) - } - }, 1000) + if (this.orderShow) { + this.order_time = Number(this.orderShow.order_time) + } }, computed: { - //订单详情 + // 订单详情 orderShow() { if (this.$store.state.orderList && this.$store.state.orderList.length > 0) { - // 使用 find 查找匹配的订单,若没有找到返回 null - return this.$store.state.orderList.find((item) => item.order_sn === this.order_sn) || null; + // 使用 find 查找匹配的订单 + return this.$store.state.orderList.find(item => item.order_sn === this.order_sn) || null } - return null; // 如果 orderList 为空或未定义,返回 null - }, + return null // 如果 orderList 为空或未定义,返回 null + } }, filters: { formatPrice, //格式化价格 }, watch: { - 'orderShow': { - handler(newVal, oldVal) { - if (newVal) { - this.order_time = Number(newVal.order_time) + orderShow: { + handler(val) { + // 先判断数据有效性 + if (val && val.order_time) { + this.order_time = Number(val.order_time) + } else { + this.order_time = -1 // 重置为无效值 } }, - deep: true + deep: true, + immediate: true, + }, + order_time: { + handler(val) { + if (this.timer) { + clearInterval(this.timer) // 清除已有定时器 + } + // 如果 order_time 无效,则不启动倒计时 + if (!val || val < 0) return + this.timer = setInterval(() => { + const tempTime = this.order_time + 900 - Math.floor(Date.now() / 1000) + if (tempTime <= 0) { + this.countdown = '00:00' + clearInterval(this.timer) // 清除定时器 + // 倒计时结束后跳转到首页 + uni.navigateTo({ + url: '/pages/index/index', // 非 Tab 页的路径 + }) + } else { + this.countdown = this.formatSeconds(tempTime) + } + }, 1000) + }, + immediate: true, // 确保初次赋值时立即触发 } }, methods: { @@ -113,12 +123,12 @@ }).then((res) => { if (res.data.status === 1) { // 调用微信支付接口 - this.requestPayment(res.data.payMsg); + this.requestPayment(res.data.payMsg) } else { uni.showToast({ title: '支付失败', icon: 'error' - }); + }) console.error(res.data.msg) } }) @@ -143,10 +153,10 @@ uni.showToast({ title: '支付失败', icon: 'error' - }); + }) console.error(err) } - }); + }) },