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

const router = global.router;

let app = getApp();
let yas;

Page({
  data: {
    receiveAddress: {},
    reason: []
  },
  onLoad(options) {
    let orderCode = options.orderCode || options.order_code;

    if (!orderCode || !app.getUid()) {
      return router.go('userCenter', {}, 'switchTab');
    }

    this.loadOrderDetail(orderCode);

    yas = new Yas(app);
    yas.pageOpenReport();
  },
  loadOrderDetail(orderCode) {
    this.orderCode = orderCode;

    orderModel.getOrderDetail(orderCode).then(res => {
      if (res.code === 200) {
        let data = res.data;

        if (data.counter_flag === 'Y' &&
                    data.is_cancel !== 'Y' && data.pay_lefttime > 0) {
          this.timeCountDown(parseInt(data.pay_lefttime));
        }

        let links = data.links || [];

        this.setData({
          pageLoaded: true,
          receiveAddress: {
            consignee: data.user_name,
            address: data.address,
            area: data.area,
            mobile: data.mobile,
            hideRightIcon: true
          },
          orderCode: data.order_code,
          orderStatus: data.status_str,
          createTime: this.formatDate(data.create_time),
          goodsList: data.order_goods || [],
          promotionList: data.promotion_formulas || [],
          paymentAmount: data.payment_amount,
          attribute: data.attribute,
          mobile: data.mobile,
          links: links,
          invoice: data.invoice
        });

        if (links.indexOf('refundApply') > -1) {
          this.loadRefundReason(); // 获取退款理由
        }
      }
    });
  },
  loadRefundReason() {
    if (this.hasSetReason) {
      return;
    }

    orderHandle.getRefundReason(res => {
      this.setData({reason: res});
      this.hasSetReason = true;
    });
  },
  timeCountDown(second) {
    const that = this;

    this.setData({payLeftTime: this.formatSecond(second)});

    this.timer = setInterval(function() {
      let payLeftTime = '';

      second--;

      if (second > 0) {
        payLeftTime = that.formatSecond(second);
      } else {
        clearInterval(that.timer);
        that.loadOrderDetail(that.orderCode);
      }

      that.setData({payLeftTime});
    }, 1000);
  },
  fill2(m) {
    m = m || 0;

    return m < 10 ? `0${m}` : m;
  },
  formatDate(time) {
    time = new Date(time * 1000);

    return `${time.getFullYear()}-${this.fill2(time.getMonth() + 1)}-${this.fill2(time.getDate())}` +
            ` ${this.fill2(time.getHours())}:${this.fill2(time.getMinutes())}:${this.fill2(time.getSeconds())}`;
  },
  formatSecond(second) {
    let hr = Math.floor(second / 3600);
    let min = Math.floor((second - hr * 3600) / 60);
    let sec = second % 60;

    return `${this.fill2(hr)}:${this.fill2(min)}:${this.fill2(sec)}`;
  },
  submitFormId(e) {
    // 上报formID
    commonModel.addWechatFormId({
      order_code: this.orderCode,
      openId: app.getOpenID(),
      miniapp_type: app.getMiniappType(),
      formId: e.detail.formId
    });
  },
  cancelOrder() {
    orderHandle.cancelOrder(this.orderCode, res => {
      if (res && res.code === 200) {
        wx.navigateBack({delta: 1});
      }
    });
  },
  payNow() {
    orderHandle.payNow({
      order_code: this.orderCode,
      order_amount: this.data.paymentAmount
    });
  },
  deleteOrder() {
    orderHandle.deleteOrder(this.orderCode, res => {
      if (res && res.code === 200) {
        if (getCurrentPages().length > 1) {
          wx.navigateBack({delta: 1});
        } else {
          router.go('orderList', {type: 1});
        }
      }
    });
  },
  viewExpress() {
    orderHandle.expressDetail(this.orderCode);
  },
  confirmOrder() {
    orderHandle.confirmReceive(this.orderCode, res => {
      if (res && res.code === 200) {
        wx.navigateBack({delta: 1});
      }
    });
  },
  refundOrder() {
    let that = this;

    wx.showModal({
      content: '申请退款后,本单享有的优惠可能会一并取消,确定申请吗?',
      cancelText: '取消',
      confirmText: '确定',
      confirmColor: '#FF0000',
      success(res) {
        if (res.confirm) {
          that.loadRefundReason();
          that.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.orderCode, this.data.reason[this.reasonIndex || 0], res => {
      if (res && res.code === 200) {
        wx.navigateBack({delta: 1});
      }
    });

    this.setData({showReason: false});
  },
  showToast(title) {
    return wx.showToast({
      title: title,
      icon: 'none',
      duration: 2000
    });
  },
  viewInvoice() {
    let invoice = this.data.invoice;

    if (!invoice || !invoice.pdfUrl) {
      return;
    }

    let url = invoice.pdfUrl;

    if (url.indexOf('dev.') < 0) {
      url = url.replace('http:', 'https:');
    }

    wx.downloadFile({url}).then(res => {
      if (wx.openDocument) {
        return wx.openDocument({filePath: res.tempFilePath}).catch(() => {
          return res;
        });
      }

      return res;
    }).then(res => {
      if (res && res.tempFilePath) {
        return wx.saveFile(res).then(subRes => {
          this.showToast('发票已保存至' + subRes.savedFilePath);
        });
      }
    }).catch((e) => {
      console.error(e);
      this.showToast('发票下载失败,请稍后重试');
    });
  }
});