vue移动端使用-订单超时,时间计算

在这里插入图片描述
vue的html代码,在此处显示时间

 data() {
    
    
    return {
    
    
      overTime: "",//超时时间
      orderUuid: this.$route.query.orderUuid,
      orderInfos: {
    
    },
      expectFinishTime: "",//预定到达时间
    };
  },
  mounted() {
    
    
    this.getOrderDetail(this.orderUuid);//获取订单信息

    if (this.timer) {
    
    
      clearInterval(this.timer);
    } else {
    
    
      let that = this;
      this.timer = setInterval(() => {
    
    
        that.getoverTime();//每秒调一次超时时间,
      }, 1000);
    }
    //   console.log(expectFinishTime)
  },
  destroyed() {
    
    
    clearInterval(this.timer);//要记得销毁他哦
  },
  methods: {
    
    
    //获取订单详情
    getOrderDetail(orderUuid) {
    
    
      takeoutDetailApi({
    
    
        orderUuid: orderUuid
      }).then(res => {
    
    
        if (res.data.result != 0) {
    
    
          Toast(res.data.msg);
        } else {
    
    
          this.orderInfos = res.data.data;
          this.expectFinishTime = res.data.data.expectFinishTime;//拿到返回的预订到达时间
          //   console.log(res.data.data.expectFinishTime)
        }
      });
    },
    // 获取超时时间
    getoverTime() {
    
    
      //   console.log(this.getDate(this.expectFinishTime));
      this.getDate(this.expectFinishTime);//对拿到的预定时间做处理
    },
    // 处理送达时间
    getDate(date) {
    
    
      if (date) {
    
    
        let time = new Date(Date.parse(date.replace(/-/g, "/")));
        let curDate = new Date();
        if (time >= curDate) {
    
    //预定时间和现在时间作对比
          //   console.log(time, curDate);
          return "预计" + date.substr(0, date.length - 3) + "送达";
        } else {
    
    
          let total = (curDate.getTime() - time.getTime()) / 1000;
          let day = parseInt(total / (24 * 60 * 60)); //计算整数天数
          let afterDay = total - day * 24 * 60 * 60; //取得算出天数后剩余的秒数
          let hour = parseInt(afterDay / (60 * 60)); //计算整数小时数
          let afterHour = total - day * 24 * 60 * 60 - hour * 60 * 60; //取得算出小时数后剩余的秒数
          let min = parseInt(afterHour / 60); //计算整数分
          let afterMin =
            String(total - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60) < 10
              ? "0" +
                String(
                  total - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60
                ).substr(0, 1)
              : String(
                  total - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60
                ).substr(0, 2); //取得算出分后剩余的秒数
          this.overTime = "";//先置空在渲染
          this.overTime =
            "超时" + hour + "时" + min + "分" + afterMin + "秒" + "";
          //   console.log(afterMin);
          return "超时" + hour + "时" + min + "分" + afterMin + "秒" + "";
        }
      }
    },

猜你喜欢

转载自blog.csdn.net/yxgd_1442962728/article/details/105410885
今日推荐