From 9b7ca6de0609bf200ba1eb2b21153625f7b571f1 Mon Sep 17 00:00:00 2001 From: sszdot Date: Tue, 17 Dec 2024 11:35:17 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E7=B1=BB=20=20=E5=9E=8B=E3=80=91?= =?UTF-8?q?=EF=BC=9Afix=20=E3=80=90=E5=8E=9F=20=20=E5=9B=A0=E3=80=91?= =?UTF-8?q?=EF=BC=9A=E5=BC=82=E6=AD=A5=E5=8A=A0=E8=BD=BD=E8=AE=A2=E5=8D=95?= =?UTF-8?q?=20=E7=9A=84=E5=BB=B6=E6=97=B6=20=20=E9=80=A0=E6=88=90=20?= =?UTF-8?q?=E6=94=AF=E4=BB=98=E5=80=92=E8=AE=A1=E6=97=B6=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E9=94=99=E8=AF=AF=20=E3=80=90=E8=BF=87=20=20=E7=A8=8B=E3=80=91?= =?UTF-8?q?=EF=BC=9Awatch=E7=9B=91=E5=90=ACorder=5Ftime=20=E7=9A=84?= =?UTF-8?q?=E5=8F=98=E5=8C=96=20=E6=9C=89=E5=80=92=E8=AE=A1=E6=97=B6?= =?UTF-8?q?=E7=9A=84=E4=BF=A1=E6=81=AF=E4=B9=8B=E5=90=8E=20=E5=86=8D?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=20=E5=80=92=E8=AE=A1=E6=97=B6=E7=9A=84?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=20=E4=BE=8B=E5=A6=82=E8=A7=86=E5=9B=BE?= =?UTF-8?q?=E6=B8=B2=E6=9F=93=20=E4=BB=A5=E5=85=8D=E6=8A=A5=E9=94=99=20?= =?UTF-8?q?=E3=80=90=E5=BD=B1=20=20=E5=93=8D=E3=80=91=EF=BC=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # 类型 包含: # feat:新功能(feature) # fix:修补bug # docs:文档(documentation) # style: 格式(不影响代码运行的变动) # refactor:重构(即不是新增功能,也不是修改bug的代码变动) # test:增加测试 # chore:构建过程或辅助工具的变动 --- pages/shop/pay.vue | 90 +++++++++++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 40 deletions(-) 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) } - }); + }) },