detail.js 3.32 KB
import orderModel from '../../../models/home/order';
import orderHandle from './order-handle';
import Yas from '../../../common/yas';

const router = global.router;

let app = getApp();
const yas = new Yas(app);

Page({
    data: {
        receiveAddress: {}
    },
    onLoad(options) {
        if (!options.orderCode || !app.getUid()) {
            return router.go('home', {}, 'switchTab');
        }

        this.loadOrderDetail(options.orderCode);

        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));
                }

                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,
                    links: data.links
                });
            }
        })
    },
    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)}`;
    },
    cancelOrder() {
        orderHandle.cancelOrder(this.orderCode);
    },
    payNow() {
        orderHandle.payNow({
            order_code: this.orderCode,
            order_amount: this.data.paymentAmount
        });
    },
    deleteOrder() {
        orderHandle.deleteOrder(this.orderCode);
    },
    viewExpress() {
        orderHandle.expressDetail(this.orderCode);
    },
    confirmOrder() {
        orderHandle.confirmReceive(this.orderCode);
    },
    refundNow() {
        orderHandle.confirmReceive(this.orderCode);
    }
});