order-handle.js 3.35 KB
import wx from '../../../utils/wx';
import orderModel from '../../../models/home/order';
import { wechatPay } from '../../../utils/wechat-pay';

const REASON_CACHE_KEY = 'refund_reason_cache';

let app = getApp();
let router = global.router;

export default {
    deleteOrder(orderCode, callback) {
        if (!orderCode) {
            return;
        }

        wx.showModal({
            title: '确认删除该订单吗?',
            content: '删除了的订单不可恢复!',
            cancelText: '取消',
            confirmText: '确定',
            confirmColor: '#FF0000'
        }).then(res => {
            if (res.confirm) {
                orderModel.delOrder({order_code: orderCode}).then(json => {
                    callback && callback(json);
                });
            }
        });
    },
    confirmReceive(orderCode, callback) {
        if (!orderCode) {
            return;
        }

        wx.showModal({
            title: '确认收货',
            content: '请您收到货后再点击“确认”!',
            cancelText: '取消',
            confirmText: '确定',
            confirmColor: '#FF0000'
        }).then(res => {
            if (res.confirm) {
                orderModel.confirmReceive({order_code: orderCode, miniapp_type: app.getMiniappType()}).then(json => {
                    callback && callback(json);
                });
            }
        });
    },
    cancelOrder(orderCode, callback) {
        if (!orderCode) {
            return;
        }

        wx.showModal({
            title: '取消订单',
            content: '确认取消该订单吗?',
            cancelText: '返回',
            confirmText: '取消订单',
            confirmColor: '#000000'
        }).then(res => {
            if (res.confirm) {
                orderModel.cancelOrder({order_code: orderCode}).then((json) => {
                    if (json.code !== 200) {
                        wx.showModal({title: json.message, showCancel: false});
                    }

                    callback && callback(json);
                });
            }
        });
    },
    expressDetail(orderCode) {
        if (!orderCode) {
            return;
        }
        router.go('orderExpress', {order_code: orderCode});
    },
    payNow(data, callback) {
        if (!data.order_code) {
            return;
        }

        wechatPay(data, callback);
    },
    refundNow(orderCode, reason, callback) {
        orderModel.refundOrder({
            order_code: orderCode,
            reason_id: reason.reason_id,
            reason: reason.reason,
            fromPage: 'aFP_MineOrderContent'
        }).then(json => {
            callback && callback(json);
        });
    },
    getRefundReason(callback) {
        let reason;

        try {
            reason = wx.getStorageSync(REASON_CACHE_KEY);

            if (reason) {
                reason = JSON.parse(reason);
            }
        } catch (e) {
            console.log(JSON.stringify(e));
        }

        if (reason) {
            return callback && callback(reason);
        }

        orderModel.getRefundSeason().then(res => {
            if (res.code === 200) {
                wx.setStorage({
                    key: REASON_CACHE_KEY,
                    data: JSON.stringify(res.data)
                });
                return callback && callback(res.data);
            }
        });
    },
};