支付过后,后端不能立即返回支付结果的前端处理

使用倒计时,在倒计时的过程中,多次向后端接口轮询支付结果,如果倒计时结束,返回的结果还是未支付,者进入未支付状态,如果倒计时过程中,有结果为已支付,则停止倒计时,并返回成功状态;
代码如下

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",
          });
        },
      });
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容