order.js 2.79 KB
'use strict';

const api = global.yoho.API;
const camelCase = global.yoho.camelCase;
const moment = require('moment');

const pageSize = 10;

const typeActiveIndexMap = {
    1: 0,
    2: 1,
    3: 2
};

const _getUserOrder = (type, page) => {
    const convertUnitTime = (src) => {
        return moment.unix(src).format('YYYY-MM-DD hh:mm:ss');
    };

    return api.get('', {
        method: 'app.SpaceOrders.get',
        uid: '10931021',
        type: type,
        page: page,
        limit: pageSize
    }).then(result => {
        let orderList = [];
        let total = false;
        let curPage = 1;

        if (result && result.data) {
            orderList = camelCase(result.data.order_list);
            total = result.data.total;
            curPage = result.data.page;
        }

        orderList.forEach(function(item) {
            const ot = parseInt(item.orderType, 10);

            // 转换订单创建时间
            item.createTime = convertUnitTime(item.createTime);

            // 隐藏为了的剩余时间
            if (parseInt(item.payLefttime, 10) === 0) {
                item.payLefttime = false;
            }

            // 判断是否是手机订单,3, 4, 6对应 iOS, Android, H5
            // 具体订单类型见 http://git.yoho.cn/yoho-documents/api-interfaces/blob/master/%E8%AE%A2%E5%8D%95/order.md
            item.showMobile = ot === 3 || ot === 4 || ot === 6;
        });

        return {
            orderList: orderList,
            total: total,
            curPage: curPage
        };
    });
};

const getOrderData = (type, page) => {
    const navBar = {
        tabs: [
            {
                text: '全部订单',
                typeStr: 'all'
            },
            {
                text: '待付款',
                typeStr: 'paying'
            },
            {
                text: '待发货',
                typeStr: 'delivering'
            }
        ]
    };

    type = parseInt(type, 10);

    type = type < 4 ? type : 1;
    page = page || 1;


    return _getUserOrder(type, page).then(result => {
        const basicData = {
            title: '我的订单',
            showSearch: type === 1,
            total: result.total,
            type: type
        };

        navBar.tabs[typeActiveIndexMap[type]].isActive = true;

        const order = Object.assign(basicData, {
            orderList: result.orderList.length && result.orderList || false
        }, navBar);

        const paginationOpts = result.total > pageSize ? {
            paginationOpts: {
                total: result.total,
                page: result.curPage,
                limit: pageSize
            }
        } : false;

        return {
            order: Object.assign(order, paginationOpts)
        };
    });
};

module.exports = {
    getOrderData: getOrderData
};