order.js 10.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
'use strict';

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

const helpers = global.yoho.helpers;

const _ = require('lodash');

const pageSize = 10;

// 订单列表类型对应的导航下标
const typeActiveIndexMap = {
    1: 0,
    2: 1,
    3: 2
};

const paymentTypeStr = {
    1: '在线支付',
    2: '货到付款',
    3: '现金支付',
    4: '抵消支付'
};

const btnMap = {
    all: [
        {
            name: '立即付款',
            classStr: 'btn red pay-btn mr'
        },
        {
            name: '修改订单',
            classStr: 'btn white edit-btn mr'
        },
        {
            name: '取消订单',
            classStr: 'btn white cancel-btn'
        }
    ],
    cancel: [
        {
            name: '取消订单',
            classStr: 'btn white cancel-btn'
        }
    ],
    edit: [
        {
            name: '修改订单',
            classStr: 'btn white edit-btn'
        }
    ],
    complete: [
        {
            name: '确认收货',
            classStr: 'btn complete-btn'
        }
    ],
    special: [
        {
            name: '修改订单',
            classStr: 'btn white edit-btn mr'
        },
        {
            name: '取消订单',
            classStr: 'btn white cancel-btn'
        }
    ]
};

const invoiceText = {
    1: '纸质',
    2: '电子'
};


// 订单状态
const statusMap = {
    0: {
        value: 0,
        valueStr: '待付款',
        step: 1,
        btns: btnMap.all
    },
    1: {
        value: 1,
        valueStr: '备货中',
        step: 1,
        btns: btnMap.edit
    },
    2: {
        value: 1,
        valueStr: '备货中',
        step: 1,
        btns: btnMap.edit
    },
    3: {
        value: 1,
        valueStr: '备货中',
        step: 1,
        btns: btnMap.edit
    },
    4: {
        value: 2,
        valueStr: '待收货',
        step: 3,
        showGetBtn: true,
        btns: btnMap.complete
    },
    5: {
        value: 2,
        valueStr: '待收货',
        step: 3,
        showGetBtn: true,
        btns: btnMap.complete
    },
    6: {
        value: 3,
        valueStr: '交易完成',
        step: 4,
        showEditor: true
    }
};

// 订单类型为手机的订单
const isMobileOrder = {
    3: true,
    4: true,
    6: true
};

// 转换时间戳
const _convertUnixTime = src => {
    return moment.unix(src).format('YYYY-MM-DD hh:mm:ss');
};

const _convertAddress = (addressList, createTime) => {
    let addrList = [];

    addressList.forEach(addr => {
        let address = {
            time: '',
            city: '',
            action: ''
        };

        address.time = addr.acceptTime;
        addr = addr.acceptAddress.split(' ');

        if (addr.length > 1 && /市/.test(addr[0])) {
            address.city = addr[0].replace('市', '');
            addr.splice(0, 1);
            address.action = addr.join('');
        } else {
            address.action = addr.join('');
        }


        addrList.push(address);
    });

    // 增加一个默认初始状态
    addrList.push({
        time: createTime,
        action: '您的订单已提交,等待付款'
    });

    return _.reverse(addrList);
};

// 根据订单状态获取当前步骤
const _getStepByOrderStatus = st => {
    let steps = [
        {
            stepStr: '1.提交订单'
        },
        {
            stepStr: '2.商品出库'
        },
        {
            stepStr: '3.等待收货'
        },
        {
            stepStr: '4.交易完成'
        }
    ];
    let curStep = statusMap[st].step;

    for (let i = 0; i < curStep; i++) {
        steps[i].isActive = true;
    }

    // 最后一个激活步骤需要特殊处理样式
    steps[curStep - 1].isLastActive = true;

    return steps;
};

const _getDetail = (uid, code) => {
    return api.get('', {
        method: 'app.SpaceOrders.detail',
        uid: uid,
        order_code: code
    }, {
        cache: true
    });
};

const _getUserOrder = (uid, type, page) => {

    return api.get('', {
        method: 'app.SpaceOrders.get',
        uid: uid,
        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(item => {
            const ot = parseInt(item.orderType, 10);
            const st = parseInt(item.status, 10);

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

            // 没有取消订单并且状态为0的时候显示付款按钮
            if (item.isCancel === 'N' &&
                st === 0) {
                item.showPayButton = true;
            }

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

            // 判断是否展示编辑选项
            item.showEditOption = statusMap[st].showEditor;

            // 判断是否是待收货状态
            item.showGetBtn = statusMap[st].showGetBtn;

            // 在线支付和货到付款有按钮的差异
            item.isOnlinePaid = parseInt(item.paymentType, 10) === 1;


            if (item.isCancel === 'Y') {
                item.showBuyBtn = true;
                item.statusStr = '已取消';
            } else if (item.isOnlinePaid) {
                item.statusStr = statusMap[st].valueStr;
            }

            item.payUrl = helpers.urlFormat('/shopping/pay/online', {
                code: item.orderCode
            });

            item.refundUrl = helpers.urlFormat('/me/return/refund/' + item.orderCode);
            item.exchangeUrl = helpers.urlFormat('/me/return/exchange/' + item.orderCode);
        });

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


const deleteOrder = (uid, code) => {
    return api.get('', {
        method: 'app.SpaceOrders.delOrderByCode',
        uid: uid,
        order_code: code
    });
};

const cancelOrder = (uid, data) => {
    return api.get('', {
        method: 'app.SpaceOrders.close',
        uid: uid,
        order_code: data.orderCode,
        reason_id: data.reasonId,
        reason: data.reason
    });
};

const getCancelOrderReason = () => {
    return api.get('', {
        method: 'app.SpaceOrders.closeReasons'
    }, {
        cache: true
    });
};

const getExpressInfo = (uid, code) => {
    return api.get('', {
        method: 'app.express.get',
        uid: uid,
        order_code: code
    }, {
        cache: true
    });
};

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

    const emptyObject = {
        1: {
            emptyMsg: '暂时还没有订单哦~',
            showEmptyEn: true,
            btnText: '去首页看看',
            url: '/'
        },
        2: {
            emptyMsg: '没有待付款的订单哦~',
            btnText: '查看全部订单',
            url: '/me/'
        },
        3: {
            emptyMsg: '没有待收货的订单哦~',
            btnText: '查看全部订单',
            url: '/me/'
        }
    };

    type = parseInt(type, 10);

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


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

        const empty = emptyObject[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, empty)
        };
    });
};

const getOrderDetail = (uid, code) => {
    return api.all([_getDetail(uid, code), getExpressInfo(uid, code)]).then(result => {
        let detail = result[0] && camelCase(result[0].data);
        let express = result[1] && camelCase(result[1].data);

        const st = parseInt(detail.status, 10);

        const basicData = {
            title: '订单详情'
        };

        detail.createTime = _convertUnixTime(detail.createTime);

        if (detail.isCancel === 'N' &&
            st === 0) {
            detail.showLeftTime = true;
        }

        if (st !== 0 && detail.isCancel !== 'Y') {
            detail.showPaid = true;
        }

        if (detail.isCancel !== 'Y') {
            detail.steps = _getStepByOrderStatus(st);
            detail.btns = statusMap[st].btns;

            if (parseInt(detail.paymentType, 10) === 2 &&
                (statusMap[st].valueStr === '备货中' ||
                 detail.statusStr === '备货中')) {
                detail.btns = btnMap.special;
            }
        }

        if (detail.invoice) {
            detail.invoice.typeText = invoiceText[detail.invoice.type];
        }

        detail.paymentTypeStr = paymentTypeStr[detail.paymentType];

        detail.expressInfo = express;
        detail.expressInfo.addressList = _convertAddress(express.expressDetail, detail.createTime);

        return {
            orderDetail: Object.assign(basicData, detail)
        };

    });
};

const editOrder = data => {
    return api.get('', {
        method: 'app.SpaceOrders.updateDeliveryAddress',
        uid: data.uid,
        order_code: data.orderCode,
        user_name: data.userName,
        area_code: data.areaCode,
        address: data.address,
        mobile: data.mobile,
        phone: data.phone,
        app_type: 1
    });
};

const reAddCart = (uid, code) => {
    return api.get('', {
        method: 'app.Shopping.readd',
        uid: uid,
        order_code: code,
        app_type: 1
    });
};

const confirmReceive = (uid, code) => {
    return api.get('', {
        method: 'app.SpaceOrders.confirm',
        uid: uid,
        order_code: code,
        app_type: 1
    });
};

module.exports = {
    getOrderData,
    getOrderDetail,
    cancelOrder,
    getCancelOrderReason,
    deleteOrder,
    editOrder,
    getExpressInfo,
    reAddCart,
    confirmReceive
};