order.js 6.62 KB
import wx from '../../../utils/wx';
import Yas from '../../../common/yas';
import orderHandle from './order-handle';
import commonModel from '../../../models/common';
import orderModel from '../../../models/home/order';
import formatImage from '../../../utils/formatImage';

// 获取应用实例
let app = getApp();
let yas;
let timer;

Page({
  data: {
    windowHeight: 0,
    orderType: 1,
    ordersList: [],
    reason: [],
    orderCode: ''
  },
  onLoad: function(options) {
    let type = options.type || 1;

    this.setData({
      orderType: type
    });

    if (!app.getUid()) {
      wx.switchTab({
        url: '/pages/home/home',
      });
    }

    yas = new Yas(app);
    yas.pageOpenReport();
  },
  onShow: function() {
    this.getOrders(this.data.orderType);
  },
  onReady: function() {
    wx.getSystemInfo().then(({windowHeight}) => {
      this.setData({
        windowHeight
      });
    });
  },
  startTimer: function() {
    let that = this;

    timer = setInterval(function() {
      let orders = that.data.ordersList;

      for (let i = 0; i < orders.length; i++) {
        let item = orders[i];
        let pay_lefttime = parseInt(item.pay_lefttime);

        if (item.counter_flag === 'Y' && item.is_cancel !== 'Y' && pay_lefttime > 0) {
          item.pay_lefttime = pay_lefttime - 1;
          item.time_left = that.formatDate(pay_lefttime - 1);
          if (item.pay_lefttime === 0) {
            that.getOrders(this.data.orderType);
          }
        } else {
          item.pay_lefttime = 0;
        }
      }
      that.setData({
        ordersList: orders,
      });
    }, 1000);
  },

  stopTimer: function() {
    clearInterval(timer);
  },
  add0: function(m) {
    return m < 10 ? '0' + m : m;
  },
  formatDate: function(second) {
    var dateStr = '';
    var hr = Math.floor(second / 3600);
    var min = Math.floor((second - hr * 3600) / 60);
    var sec = (second - hr * 3600 - min * 60);

    dateStr = this.add0(hr) + ':' + this.add0(min) + ':' + this.add0(sec);
    return dateStr;
  },
  formatImgUrl: function(res) {
    res.data.order_list && res.data.order_list.map((item) => {
      item.order_goods && item.order_goods.map(good => {
        good.goods_image = formatImage.image(good.goods_image, 100, 134);
      });
    });
  },
  formatOrders: function(orders) {
    let _orders = [];
    let needTimer = false;

    orders && orders.map((item)=>{
      item.pay_lefttime = parseInt(item.pay_lefttime);
      item.time_left = this.formatDate(parseInt(item.pay_lefttime));

      if (item.counter_flag === 'Y' && item.is_cancel !== 'Y' && parseInt(item.pay_lefttime) > 0) {
        needTimer = true;
      }
      for (let i = 0; i < item.order_goods.length; i++) {
                if (Number(item.order_goods[i].real_pay_price).toFixed(2) === Number(item.order_goods[i].sales_price).toFixed(2) || !item.order_goods[i].real_pay_price) { // eslint-disable-line
                    item.order_goods[i].real_pay_price = item.order_goods[i].goods_price ? item.order_goods[i].goods_price : item.order_goods[i].sales_price; // eslint-disable-line
          item.order_goods[i].sales_price = '';
        }
        if (item.order_goods[i].sales_price) {
          item.order_goods[i].sales_price = Number(item.order_goods[i].sales_price).toFixed(2);
        }
      }
      _orders.push(item);
    });

    if (needTimer) {
      this.stopTimer();
      this.startTimer();
    } else {
      this.stopTimer();
    }

    return _orders;
  },
  getOrders: function(type) {
    let params = {
      type,
      uid: app.getUid(),
      page: 1,
      limit: 50
    };

    wx.showLoading({title: '加载中'});
    orderModel.getOrders(params)
      .then(res => {
        if (res.code === 200) {
          this.formatImgUrl(res);
          let orders = res.data.order_list;

          orders = this.formatOrders(orders);

          this.setData({
            ordersList: orders,
            showEmptyContainer: res.data.order_list.length === 0
          });
          wx.hideLoading();
        }
      }).catch(() => {
        wx.hideLoading();
      });
  },
  loadRefundReason() {
    if (this.hasSetReason) {
      return;
    }

    orderHandle.getRefundReason(res => {
      this.setData({reason: res});
      this.hasSetReason = true;
    });
  },
  navToOrderDetail(e) {
    let code = e.currentTarget.dataset.order_code;

    global.router.go('orderDetail', {orderCode: code});
  },
  delOrder: function(e) {
    const code = e.target.dataset.code;

    orderHandle.deleteOrder(code, res => {
      if (res && res.code === 200) {
        this.getOrders(this.data.orderType);
      }
    });
  },
  confirmReceive: function(e) {
    let code = e.currentTarget.dataset.code;

    orderHandle.confirmReceive(code, res => {
      if (res && res.code === 200) {
        this.getOrders(this.data.orderType);
      }
    });
  },
  cancelOrder: function(e) {
    let code = e.currentTarget.dataset.code;

    orderHandle.cancelOrder(code, res => {
      if (res && res.code === 200) {
        this.getOrders(this.data.orderType);
      }
    });
  },
  expressDetail: function(e) {
    let code = e.currentTarget.dataset.code;

    orderHandle.expressDetail(code);
  },
  payNow: function(e) {
    let code = e.currentTarget.dataset.code;
    let amount = e.currentTarget.dataset.amount;

    orderHandle.payNow({ order_code: code, order_amount: amount});
  },
  goShopping: function() {
    wx.switchTab({url: '/pages/index/index'});
  },
  changeType: function(e) {
    const { type } = e.currentTarget.dataset;

    this.setData({
      orderType: type
    });
    this.getOrders(type);
  },
  refundOrder(e) {
    this.refundOrderCode = e.currentTarget.dataset.code;
    wx.showModal({
      content: '申请退款后,本单享有的优惠可能会一并取消,确定申请吗?',
      cancelText: '取消',
      confirmText: '确定',
      confirmColor: '#FF0000'
    }).then(res => {
      if (res.confirm) {
        this.loadRefundReason();
        this.setData({showReason: true});
      }
    });
  },
  hidePicker(e) {
    if (e.target.id === 'reason-picker') {
      this.setData({showReason: false});
    }
  },
  reasonChange(e) {
    this.reasonIndex = e.detail.value[0];
  },
  reasonSure() {
    orderHandle.refundNow(this.refundOrderCode,
      this.data.reason[this.reasonIndex || 0], res => {
        if (res && res.code === 200) {
          this.getOrders(this.data.orderType);
        }
      });

    this.setData({showReason: false});
  },
  submitFormId: function(e) {
    // 上报formID
    commonModel.addWechatFormId({
      order_code: e.detail.value.orderCode,
      openId: app.getOpenID(),
      miniapp_type: app.getMiniappType(),
      formId: e.detail.formId
    });
  }
});