returns.js 21.8 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 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
/**
 * 退换货
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2016/7/19
 */
'use strict';

const api = global.yoho.API;
const camelCase = global.yoho.camelCase;

const _ = require('lodash');
const mcHandler = require('./menu-crumb-handler');
const returnsAPI = require('./returns-api');

// const logger = global.yoho.logger;

const helpers = global.yoho.helpers;

const pageSize = 10;

const returnAddress = {
    address: '江苏省南京市江宁区江宁经济技术开发区苏源大道87号YOHO!BUY(有货)物流中心东一楼',
    name: 'YOHO!',
    code: '210000',
    phone: '17714194699'
};


/**
 * 获取原图片路径
 * @function _getForceSourceUrl
 * @param { String } url 图片路径
 * @return { String } 原图片路径
 */
const _getForceSourceUrl = (url) => {
    const str = ['?imageView', '?imageMogr2'];

    _.forEach(str, value => {
        url = _.split(url, value, 1)[0];
    });
    url.replace('http:', '');

    return url;
};

/**
 * 获取用户退换货列表
 * @function getUserReturn
 * @param { string } uid 用户uid
 * @param { number } page 分页数量
 * @return { Object } 退换货列表
 */
const getUserReturn = (uid, page) => {

    return api.get('', {
        method: 'app.refund.getList',
        uid: uid,
        page: page || 1,
        limit: 10
    }).then(result => {
        const basicData = {
            title: '我的退/换货'
        };

        const refundStr = {
            1: '退货',
            2: '换货'
        };

        const urlIndex = {
            1: '/me/return/refund/detail/',
            2: '/me/return/exchange/detail/'
        };

        const typeIndex = {
            1: 'refund',
            2: 'exchange'
        };

        let data = {};
        let paginationOpts = false;

        if (result && result.data) {
            data = result.data;// camelCase(result.data);

            paginationOpts = data.total > pageSize ? {
                paginationOpts: {
                    total: data.total,
                    page: data.page,
                    limit: pageSize
                }
            } : false;

            data.list.forEach(item => {
                item.orderGoods = item.goods;
                item.createTime = item.order_create_time;
                item.hidePrice = true;
                item.showStatus = true;
                item.refundStr = refundStr[item.refund_type];
                item.detailUrl = helpers.urlFormat(urlIndex[item.refund_type], {
                    orderCode: item.id
                });
                item.type = typeIndex[item.refund_type];

                item.orderGoods.forEach(good => {
                    let cnAlphabet = good.cn_alphabet ? good.cn_alphabet : '';

                    good.hidePrice = true;
                    good.goodUrl = helpers.urlFormat(`/product/pro_${good.product_id}_${good.goods_id}/${cnAlphabet}.html`); // eslint-disable-line
                });
            });
        }

        return {
            returnsList: Object.assign(data, paginationOpts, basicData)
        };
    });
};

/**
 * 设置侧边栏列表
 * @function _setSideMenu
 * @param { string } type 当前所在模块类型
 * @return { Object } 侧边栏列表
 */
const _setSideMenu = (type) => {
    return {
        nav: mcHandler.getMeCrumb(type),
        navigation: mcHandler.getSideMenu(type)
    };
};

/**
 * 计算进度条进度
 * @function _calcStatusRate
 * @param { number } num 当前数量
 * @param { number } total 总共数量
 * @return { string } 进度条进度百分比
 */
const _calcStatusRate = (num, total) => {
    let rate = 0;

    if (total) {
        rate = `${_.toInteger(num / total * 100)}%`;
    }

    return rate;
};

/**
 * 提交换货申请
 * @function submitChange
 * @param { Object } data 换货数据
 * @param { number } uid 用户uid
 * @return { Object } 换货申请结果
 */
const submitChange = (data, uid) => {
    return returnsAPI.changeSubmitAsync(data, uid).then(result => {
        return result;
    });
};

/**
 * 设置默认退换货状态进度信息
 * @function _setDefaultStatus
 * @param { string } type 退换货类型(退货/换货)
 * @return { Object } 退换货状态进度信息
 */
const _setDefaultStatus = (type) => {
    const list = ['提交申请', '审核通过', '商品寄回', '商品入库', `${type}完成`];
    let statusList = [];
    let resData = {};

    _.forEach(list, (value, key) => {
        statusList.push({
            step: _.toInteger(key) + 1,
            name: value
        });
    });
    statusList[0].act = true;

    resData.returnStstus = {
        rate: _calcStatusRate(1, statusList.length),
        statusList: statusList
    };

    return resData;
};

/**
 * 设置退换货状态进度信息
 * @function _setReturnStatus
 * @param { number } list 退换货进度列表
 * @param { boolean } half 是否比正常进度多半格
 * @return { Object } 退换货状态进度信息
 */
const _setReturnStatus = (list, half) => {
    let resData = {};

    if (list && list.length) {
        let statusList = [],
            step = 0;
        let rate;

        // 遍历进度列表
        _.forEach(list, (value, key) => {
            let state = {
                act: value.act === 'Y',
                step: _.toInteger(key) + 1,
                name: value.name
            };

            if (state.act) {
                step++;
            }
            statusList.push(state);
        });

        // 多半格进度特殊处理
        if (half) {
            rate = _calcStatusRate(step * 2 + 1, statusList.length * 2);
        } else {
            rate = _calcStatusRate(step, statusList.length);
        }

        resData.returnStstus = {
            rate: rate,
            statusList: statusList
        };
    }

    return resData;
};

/**
 * 设置退货列表信息
 * @function _setRefundGoodList
 * @param { Object } data 接口返货订单信息
 * @return { Object } 订单退货信息
 */
const _setRefundGoodList = (data) => {
    let resData = {
        specialNotice: data.special_notice,
        speclialReason: data.special_return_reason
    };

    if (data.goods_list) {
        let goods = [];

        _.forEach(data.goods_list, value => {
            let cnAlphabet = value.cn_alphabet ? value.cn_alphabet : '';

            goods.push({
                href: helpers.urlFormat(`/product/pro_${value.product_id}_${value.goods_id}/${cnAlphabet}.html`),
                img: value.goods_image,
                name: value.product_name,
                size: value.size_name,
                color: value.color_name,
                num: 1, // 接口目前不支持
                price: value.last_price,
                skn: value.product_skn,
                skc: value.product_skc,
                sku: value.product_sku,
                type: value.goods_type,
                typeId: value.goods_type_id,
                limitReturn: value.is_limit_skn === 'Y',
                reasonList: data.return_reason
            });

            if (value.hasShoes) {
                resData.hasShoes = true;
            }
        });
        resData.goods = goods;
    }

    let returnMode = [];

    _.forEach(data.return_amount_mode, value => {
        let mode = {
            title: value.name,
            id: value.id,
            cur: value.is_default === 'Y'
        };

        // 退货类型根据id匹配
        switch (mode.id) {
            case 1:
                Object.assign(mode, {
                    name: value.name,
                    tip: '退款会返到您购买时支付的账户中'
                });
                break;
            case 2:
                Object.assign(mode, {
                    class: 'ali-item',
                    alipay: true
                });
                break;
            case 3:
                Object.assign(mode, {
                    class: 'union-item',
                    unionpay: true
                });
                break;
            default:
                mode.name = value.name;
                break;
        }
        returnMode.push(mode);
    });
    resData.returnMode = returnMode;

    return resData;
};

/**
 * 设置退货订单数据
 * @function _setRefundDetailData
 * @param { Object } data 接口返回退货订单数据
 * @return { Object } 模板渲染退货订单数据
 */
const _setRefundDetailData = (data) => {
    let resData = {
        id: data.id || 0,
        orderCode: data.source_order_code || ''
    };

    switch (data.status) {
        case 10:
            Object.assign(resData, {
                orderReview: {pass: true},
                refundExpress: true,
                refundAddress: returnAddress
            });
            break;
        case 20:
            resData.refundExpress = true;
            break;
        case 30:
            resData.backStorage = true;
            break;
        case 40:
            resData.refundSure = {
                mode: data.return_amount_mode_name,
                account: data.return_amount_mode_name,
                amount: data.return_amount,
                coin: parseInt(data.return_yoho_coin * 100, 10)
            };
            break;
        case 91:
            resData.cancelApply = true;
            break;
        default:
            resData.orderReview = true;
            break;
    }

    if (resData.refundExpress) {
        resData.refundExpress = {expressList: data.expressList};

        if (_.get(data, 'notice.id', 0) === 20) {
            let company = _.get(data, 'notice.express_company'),
                key = _.findKey(data.expressList, {name: company});

            Object.assign(resData.refundExpress, {
                id: key ? data.expressList[key].id : 0,
                company: company,
                number: _.get(data, 'notice.express_number', 0)
            });
        }
    }

    let goods = [];

    _.forEach(data.goods_list, value => {
        let evidence;

        if (!_.isEmpty(value.evidence_images)) {
            evidence = [];
            _.forEach(value.evidence_images, img => {
                evidence.push(_getForceSourceUrl(img));
            });
        }
        goods.push({
            img: value.goods_image,
            name: value.product_name,
            size: value.size_name,
            color: value.color_name,
            num: 1, // 接口目前不支持
            reason: value.reason_name || '--',
            price: value.sales_price,
            remark: value.remark,
            evidence: evidence
        });
    });

    resData.goods = goods;

    return resData;
};

/**
 * 设置物流公司列表
 * @function _setExpressData
 * @param { Object } data 接口返回物流公司列表
 * @return { Array } 物流公司列表
 */
const _setExpressData = (data) => {
    var resData = [];

    if (!_.isEmpty(data)) {
        let list = [];

        _.forEach(data, value => {
            list = list.concat(value);
        });
        _.forEach(list, value => {
            resData.push({
                id: value.id,
                name: value.company_name
            });
        });
    }

    return resData;
};

/**
 * 获取退货商品信息
 * @function getRefundGoodsData
 * @param { number } orderCode 订单号
 * @param { number } uid 用户uid
 * @return { Object } 退货商品信息
 */
const getRefundGoodsData = (orderCode, uid) => {
    return returnsAPI.getRefundGoodsAsync(orderCode, uid).then(result => {
        let resData = {};

        Object.assign(resData, _setSideMenu('我的退/换货'));

        resData.returns = {
            title: '退货申请',
            refund: _setDefaultStatus('退货') || {}
        };

        if (result.data) {
            Object.assign(
                resData.returns.refund,
                _setRefundGoodList(result.data),
                {orderCode: orderCode}
            );
        }

        return resData;
    });
};

/**
 * 获取退货详情信息
 * @function getRefundDetailData
 * @param { number } applyId 退货申请id
 * @param { number } uid 用户uid
 * @return { Object } 退货详情信息
 */
const getRefundDetailData = (applyId, uid) => {
    return Promise.all([
        returnsAPI.getRefundDetailAsync(applyId, uid),
        returnsAPI.getExpressCompanyAsync()
    ]).then(result => {
        let resData = {};

        Object.assign(resData, _setSideMenu('我的退/换货'));

        resData.returns = {
            title: '退货申请',
            refundDetail: {}
        };

        if (result[0] && result[0].data) {
            let data = result[0].data;

            if (result[1] && result[1].data) {
                data.expressList = _setExpressData(result[1].data);
            }

            Object.assign(
                resData.returns.refundDetail,
                _setReturnStatus(data.statusList, !data.status), // 设置状态进度
                _setRefundDetailData(data) // 设置退货详情信息
            );
        }

        return resData;
    });
};

/**
 * 保存退货信息
 * @function saveRefund
 * @param { number } orderCode 订单号
 * @param { number } uid 用户uid
 * @param { Object } goods 退货商品信息
 * @param { Object } payment 退款信息
 * @return { Object } 提交退货申请结果
 */
const saveRefund = (orderCode, uid, goods, payment) => {
    return returnsAPI.getOrderInfoAsync(orderCode, uid, '').then(result => {
        if (!_.isEmpty(result)) {
            return returnsAPI.refundSubmitAsync(orderCode, uid, goods, payment).then(subRes => {
                if (subRes && subRes.data) {
                    return {
                        code: 200,
                        data: {
                            refer: `/me/return/refund/detail?orderCode=${subRes.data.apply_id}`
                        }
                    };
                }
                return {code: 400, message: '申请失败'};
            });
        } else {
            return {code: 403, message: '没有找到该订单'};
        }
    });
};

/**
 * 获取商品详情
 * @function getProductInfo
 * @param { number } productId 商品id
 * @param { number } productSkn 商品skn
 * @return { Object } 商品详情信息
 */
const getProductInfo = (productId, productSkn) => {
    return returnsAPI.getProductInfoAsync(productId, productSkn).then(result => {
        if (result.code === 200) {
            camelCase(result);
            result.data.goodsList.forEach(good => {
                good.colorImage = helpers.image(good.colorImage, 20, 20);
            });
        }
        return result;
    });
};

// `
/**
 * 获取换货类型
 * @param { number } uid 用户uid
 * @param { number } areaCode 地区码
 * @return { Object } 换货类型
 */
const getChangeType = (uid, areaCode) => {
    return returnsAPI.getChangeTypeAsync(uid, areaCode).then(result => {
        camelCase(result);

        if (result.data) {
            result.data.forEach(data => {
                data.isDefault = data.isDefault === 'Y';
            });
        }

        return result;
    });
};

/**
 * 获取换货商品列表
 * @function getChangeGoodsList
 * @param { number } orderCode 订单号
 * @param { number } uid 用户uid
 * @return { Object } 换货商品列表信息
 */
const getChangeGoodsList = (orderCode, uid) => {
    return returnsAPI.getChangeGoodsListAsync(orderCode, uid).then(result => {
        const basicData = {
            title: '换货申请'
        };
        let data;


        if (result && result.data) {
            data = camelCase(result.data);
            data.hidePrice = true;
            data.orderCode = orderCode;

            // 设置特殊原因
            _.forEach(data.specialExchangeReason, r => {
                _.find(data.exchangeReason, {id: r.id}).isSpecial = true;
            });

            if (data.goodsList) {
                data.goodsList.forEach(good => {
                    let cnAlphabet = good.cnAlphabet ? good.cnAlphabet : '';

                    good.showCheckbox = true;
                    good.hidePrice = true;
                    good.buyNumber = 1;
                    good.goodUrl = helpers.urlFormat(`/product/pro_${good.productId}_${good.goodsId}/${cnAlphabet}.html`); //eslint-disable-line
                });
            }
        }


        return {
            returnsChange: Object.assign(basicData, data)
        };
    });
};

/**
 * 设置换货详情数据
 * @function _setExchangeDetailData
 * @param { Object } data 接口返回换货订单数据
 * @return { Object } 模板渲染换货数据
 */
const _setExchangeDetailData = (data) => {
    // data = camelCase(data);
    let list = {};

    switch (data.status) {
        case 0:
            if (data.delivery_tpye_name === '寄回换货') {
                Object.assign(list, {
                    audit: true,
                    reminder: true,
                    inDoor: false,
                    sendBack: true,
                    cancel: true,
                    location: true
                });
            } else {
                Object.assign(list, {
                    audit: true,
                    reminder: true,
                    inDoor: true,
                    cancel: true
                });
            }

            break;
        case 10:
            Object.assign(list, {
                through: true,
                reminder: true,
                location: true
            });

            if (data.delivery_tpye_name === '寄回换货') {
                Object.assign(list, {
                    logistics: true,
                    sendBack: true,
                    exchangeAddress: returnAddress,
                    cancel: true,
                    location: true
                });
            } else {
                list.inDoor = true;
            }

            break;
        case 20:
            if (data.delivery_tpye_name === '寄回换货') {
                Object.assign(list, {
                    logistics: true
                });
            } else {
                list.send = true;
                list.reminder = true;
                list.inDoor = true;
                list.auditSuccess = true;
                list.view = `/me/order/detail/?orderCode=${data.order_code}`;
            }

            break;
        case 30:
            if (data.delivery_tpye_name === '寄回换货') {
                list.takeGoods = true;
                list.auditSuccess = true;
                list.view = `/me/order/detail/?orderCode=${data.order_code}`;
            } else {
                list.takeGoods = true;
            }
            break;
        case 50:
            Object.assign(list, {
                send: true,
                doubt: true,
                auditSuccess: true,
                view: `/me/order/detail/?orderCode=${data.order_code}`
            });
            break;
        case 40:
            Object.assign(list, {
                finish: true,
                auditSuccess: true,
                view: `/me/order/detail/?orderCode=${data.order_code}`
            });
            break;
        case 91:
            list.abolish = true;
            break;
        default:
            break;
    }

    return list;
};

/**
 * 获取换货详情信息
 * @function getExchangeDetailData
 * @param { number } id 换货申请单号
 * @param { number } uid 用户uid
 * @return { Object } 换货详情信息
 */
const getExchangeDetailData = (id, uid) => {
    return Promise.all([
        returnsAPI.getExchangeDetail(id, uid),
        returnsAPI.getExpressCompanyAsync()
    ]).then(result => {
        let exchangeData = {};

        Object.assign(exchangeData, _setSideMenu('我的退/换货'));
        exchangeData = {
            title: '换货申请',
            exchangeDetail: {}
        };

        if (result[0] && result[0].data) {
            let data = result[0].data;

            _.forEach(data.goods_list, function(value) {
                _.forEach(value.evidence_images, function(list, index) {
                    value.evidence_images[index] = list.substring(0, list.indexOf('?'));
                });
            });

            data.expressId = data.notice.express_id;

            if (result[1] && result[1].data) {
                data.expressList = _setExpressData(result[1].data);
            }

            Object.assign(
                exchangeData.exchangeDetail,
                _setReturnStatus(data.statusList),
                _setExchangeDetailData(data),
                data// camelCase(data)
            );
        }

        return exchangeData;
    });
};

/**
 * 取消退换货申请
 * @function cancelReturnApply
 * @param { number } id 退换货id
 * @param { number } uid 用户uid
 * @param { string } type 退换货类型 refund--退货 exchange--换货
 * @return { Object } 取消退换货申请结果
 */
const cancelReturnApply = (id, uid, type) => {
    return returnsAPI.cancelReturnAsync(id, uid, type).then(result => {
        let resData = result || {
            code: 400,
            message: '取消失败'
        };

        return resData;
    });
};

/**
 * 设置寄回物流信息
 * @function setBackEepress
 * @param { number } uid 用户uid
 * @param { Object } param 物流参数
 * @return { Object } 设置寄回物流信息结果
 */
const setBackEepress = (uid, param) => {
    const type = param.type === 'exchange';

    return returnsAPI.setExpressNumberAsync(uid, param, type).then(result => {
        let resData = result || {
            code: 400,
            message: '设置寄回单号失败'
        };

        return resData;
    });
};

/**
 * 获取银行列表
 * @function getUnionData
 * @return { Object } 银行列表数据
 */
const getUnionData = () => {
    return returnsAPI.getRefundBank().then(result => {
        let resData = result || {
            code: 400,
            message: '获取银行列表失败'
        };

        return resData;
    });
};

module.exports = {
    getUserReturn,
    getRefundGoodsData,
    getChangeGoodsList,
    getProductInfo,
    getRefundDetailData,
    saveRefund,
    getExchangeDetailData,
    submitChange,
    cancelReturnApply,
    setBackEepress,
    getUnionData,
    getChangeType
};