cancel-order-dialog.js 4.56 KB
import OrderListService from '../../pages/orderList/orderListService.js';
import OrderActionService from './orderActionService.js';
import event from '../../utils/event';

Component({
  properties: {
    showConfirm: {
      type: Boolean,
      value: false
    },
    orderCode: {
      type: String,
      value: '',
      observer: '_orderChange'
    },
    navBack: {
      type: Number,
      value: 0
    }
  },
  data: {
    slideLeft: 0,
    startX: 0,
    hideSlide: true,
    hideTip: false,
    containerWidth: 0,
    slideIconWidth: 0,
    status: '',

    orderPrice: '',
    orderPriceDesc: '',
    penaltyAmount: '',
    penaltyDesc: '',
    penaltyRate: '',
    refundAmount: '',
    refundDesc: ''
  },
  methods: {
    closeSlide: function() {
      this.setData({
        hideSlide: true
      });
    },
    touchStart: function (e) {
      this.data.startX = e.touches[0].clientX;
    },
    touchMove: function (e) {
      let currentX = e.touches[0].clientX;

      if (currentX - this.data.startX < 0 || this.data.complete) {
        return;
      }

      if (currentX - this.data.startX > (this.data.containerWidth - this.data.slideIconWidth - 4)) {
        this.data.complete = true;
        this.setData({
          slideLeft: this.data.containerWidth - this.data.slideIconWidth - 4
        });
        return this.slideComplete();
      }

      this.setData({
        hideTip: true,
        slideLeft: currentX - this.data.startX
      });
    },
    touchEnd: function (e) {
      if (!this.data.complete) {
        this.setData({
          slideLeft: 0,
          hideTip: false
        });
      }
    },
    confirm: function() {
      let ctx = this;

      if (!this.data.needPenalty) {
        let api = new OrderActionService();
        let params = {
          method: 'ufo.buyer.cancel',
          orderCode: this.data.orderCode
        };
        this.setData({
          showConfirm: false
        });
        api.actionOrder(params).then(() => {
          if (+this.data.navBack === 1){
            wx.navigateBack({
              delta: 1
            });
          }
          event.emit('refresh-order');
        });
        return;
      }

      this.setData({
        showConfirm: false,
        hideSlide: false
      }, () => {
        let query = this.createSelectorQuery();
        query.select('.slide-container').boundingClientRect();
        query.exec(function (res) {
          if (res && res[0] && res[0].width) {
            ctx.data.containerWidth = res[0].width;
          }
        });

        query = this.createSelectorQuery();
        query.select('.slide-icon').boundingClientRect();
        query.exec(function (res) {
          if (res && res[0] && res[0].width) {
            ctx.data.slideIconWidth = res[0].width;
          }
        });
      });
    },
    keep: function () {
      this.setData({
        showConfirm: false
      });
    },
    slideComplete: function() {
      this._reset();
      let api = new OrderActionService();
      let params = {
        method: 'ufo.buyer.cancel',
        orderCode: this.data.orderCode
      };

      api.actionOrder(params).then(() => {
        if (+this.data.navBack === 1){
          wx.navigateBack({
            delta: 1
          });
        }
        event.emit('refresh-order');
      });
    },
    _reset: function() {
      this.setData({
        hideSlide: true,
        slideLeft: 0,
        hideTip: false,
        complete: false
      });
    },
    _orderChange: function (val) {
      if (!val) {
        return;
      }

      let api = new OrderListService();
      this.data.complete = false;
      this.setData({
        hideSlide: true,
        slideLeft: 0
      });

      api.getCancelOrderInfo({orderCode: val}).then(data => {
        this.data.needPenalty = data && data.needPenalty || false;
        if (data && !data.needPenalty) {
          this.setData({
            ...data,
            status: '0',
            cancelText: data && data.confirmDesc || '确定取消订单?',
            cancelBtnText: '取消订单'
          });
        }

        if (data && data.needPenalty) {
          if (data && data.showPrice) {
            this.setData({
              ...data,
              status: '1',
              cancelText: data && data.confirmDesc || '确定取消订单?',
              cancelBtnText: '赔付并取消订单'
            });
          } else {
            this.setData({
              status: '2',
              cancelText: data && data.confirmDesc || '确定取消订单?',
              cancelBtnText: '赔付并取消订单'
            });
          }
        }
      });
    }
  }
});