orderaction.js 2.54 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';

let interval;

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){
      interval = setInterval(() => {
        timer = timer - 1;
        leftTime = formatTimeByMin(timer, 'm:s'); 
        if (timer <= 0) {
          leftTime = '00:00';
          setTimeout(() => {
            event.emit('refresh-order');
          }, 2000);
          clearInterval(interval);
        }
        that.setData({
          lefttime: leftTime,
          timer: timer
        });
      }, 1000);
    }

  }
},

detached: function () {
  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 ,this.properties.isStore);
      } 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);
      }

    },

  }


})