使用倒计时,在倒计时的过程中,多次向后端接口轮询支付结果,如果倒计时结束,返回的结果还是未支付,者进入未支付状态,如果倒计时过程中,有结果为已支付,则停止倒计时,并返回成功状态;
代码如下
data() {
return {
orderIds: "",
sts: 0, // 订单支付状态: 0失败; 1成功
time: 10, //
timer: "",
};
},
onLoad(options) {
console.log("options", options);
this.orderIds = decodeURIComponent(options.orderIds).split(",");
this.sts = options.sts;
if (this.orderIds) {
this.timer = setInterval(() => {
this.time--;
if(!(this.time%3)){
this.getOrderInfo(this.orderIds);
}
if (this.time <= 0) {
clearInterval(this.timer);
// this.timer = 0;
}
}, 1000);
}
},
methods: {
async getOrderInfo(orderId) {
http.request({
url: "/zanmall_payment/pay/is_pay/0/" + orderId + "/0",
method: "get",
callBack: (res) => {
if (res) {
this.sts = 1;
clearInterval(this.timer);
} else {
// this.sts = 2;
if(this.time == 0){
this.sts = 2;
}
}
},
errCallBack: (err) => {
uni.showToast({
title: err.msg,
icon: "none",
});
},
});
}
}
网友评论