orderaction.js 2.49 KB
// src/components/order/orderaction.js
import { formatTimeByMin } from '../../utils/index.js';
import { showDialog } from './orderActionUtil.js';
import { prePay } from '../../pages/order/wxpay.js'
import router from '../../router/index.js'
const BUY_AGAIN = 'buy_again';
const NOW_BUY = 'now_buy';
const SHOW_DETAIL = 'show_detail';
const SHOW_EXPRESS = 'show_express';
import event from '../../utils/event'

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    buttons: Array,
    //默认值:-1(不显示),0:订单列表,1:订单详情
    // fromPage: Number,
    timer: String,
    realPrice: String,
    orderCode:String,
    productId:String,
    isStore:{
      type:Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    lefttime:"",
    lastIndex: 0,
    interval:Object,
  },

  lifetimes: {

  },

attached: function () {

  let that = this;
  if (that.data.buttons){
    let lastIndex = that.data.buttons.length - 1;
    console.log("lastIndex===" + lastIndex);
    that.setData({ lastIndex: lastIndex });
  }

  if (that.data.timer) {
    let leftTime = that.data.lefttime;
    let timer = that.data.timer;

    if(timer > 0){
      let interval = setInterval(() => {
        timer = timer - 1;
        leftTime = formatTimeByMin(timer, 'm:s');
        if (timer <= 0) {
          leftTime = '00:00'
          event.emit('refresh-order');
        }
        that.setData({
          lefttime: leftTime,
          timer: timer
        });
      }, 1000);
      that.setData({ interval: interval});
    }

  }
},

detached: function () {
  let interval = this.interval;
  if (interval){
    clearInterval(interval);
  }
},



  /**
   * 组件的方法列表
   */
  methods: {

    onButtonClick: function(e){
      let actionCode = e.currentTarget.dataset.buttonCode;
      let orderCode = e.currentTarget.dataset.orderCode;
      let productId = e.currentTarget.dataset.productId;

      if (actionCode == BUY_AGAIN) {
        let params = {
          id: productId
        }
        router.go('productDetail', params);
      } else if (actionCode == NOW_BUY){
        prePay(productId, orderCode, 1);
      } else if (actionCode == SHOW_DETAIL) {
        let params = {
          orderCode
        }
        router.go('orderDetail', params);
      } else if (actionCode == SHOW_EXPRESS){
        let params = {
          orderCode
        }
        router.go('logistics', params);
      } else {
        showDialog(orderCode, actionCode, 0);
      }

    },

  }


})