index-service.js 4.63 KB
'use strict';

const Promise = require('bluebird');
const co = Promise.coroutine;
const _ = require('lodash');
const Fn = require('lodash/fp');

const helpers = global.yoho.helpers;

const orderService = require('./orders-service');
const indexApi = require('./index-api');

const CHANNEL_NUM = {
    boys: 1,
    girls: 2,
    kids: 3,
    lifestyle: 4
};

const _channelNum = channel => CHANNEL_NUM[channel] || CHANNEL_NUM.boys;

/**
 * 处理品牌
 */
const _handleBrand = (brands, needNum) => {
    const handle = Fn.pipe(Fn.filter({is_hot: 'Y'}), Fn.take(needNum), Fn.map((brand) => ({
        href: helpers.urlFormat(null, null, brand.brand_domain),
        logo: helpers.getForceSourceUrl(brand.brand_ico),
        name: brand.brand_name
    })));

    return handle(brands);
};

/**
 * 处理商品
 */
const _handleProduct = (products) => {
    return products.map((product) => {
        let img = helpers.image(_.get(product, 'default_images', ''), 100, 100);

        if (img.indexOf('imageView') !== -1) {
            img = img.split('imageView', 1) +
                'imageMogr2/thumbnail/100x100/extent/100x100/background/d2hpdGU=/position/center/quality/90';
        }

        return {
            href: helpers.getUrlBySkc(product.product_id,
                _.get(product, 'goods_list[0].product_skc', ''),
                product.cn_alphabet
            ),
            thumb: img,
            name: product.product_name,
            price: product.sales_price,
            productId: product.product_id
        };
    });
};

/**
 * 消息数量提示
 */
const _msgNumber = co(function * (uid, udid) {
    let result = [
        {href: helpers.urlFormat('/home/orders'), name: '待处理订单', count: 0},
        {href: helpers.urlFormat('/home/message'), name: '未读消息', count: 0},
        {href: helpers.urlFormat('/home/comment'), name: '待评论商品', count: 0}
    ];

    let reqData = yield Promise.props({
        pending: indexApi.pendingOrderCount(uid), // 待处理订单
        unread: indexApi.unreadMessageCount(uid, udid), // 未读消息
        needComment: indexApi.needCommentCount(uid) // 待评论商品
    });

    result[0].count = _.get(reqData, 'pending.data.count', 0);
    result[1].count = _.get(reqData, 'unread.data.count', 0);
    result[2].count = _.get(reqData, 'needComment.data.count', 0);

    return result;
});

/**
 * 个人中心——最新订单
 */
const _recentOrder = co(function * (uid) {
    let latestOrder = yield Promise.props(orderService.getOrders(orderService.ORDER_TYPE.all, uid, 1, 2));

    return {
        more: helpers.urlFormat('/home/orders'),
        orders: latestOrder
    };
});

/**
 * 你喜欢的品牌
 */
const _guessYouLikeBrand = co(function * () {
    let brand = yield indexApi.guessBrand();
    const NEED_BRAND_NUM = 6;

    return _handleBrand(_.get(brand, 'data', []), NEED_BRAND_NUM);
});

/**
 * 新品
 */
const _newProduct = co(function * () {
    let newProduct = yield indexApi.newArrival();

    return _handleProduct(_.get(newProduct, 'data.product_list', []));
});

/**
 * 为你优选
 */
const _recommend = co(function * (channelNum, uid, udid) {
    let resData = yield indexApi.recommend(channelNum, uid, udid, '100004', 30);

    return _handleProduct(_.get(resData, 'data.product_list', []));
});

/**
 * 底部banner
 */
const _footerBanner = co(function * () {
    const CODE = '20110609-152143';
    let banner = yield indexApi.getByNodeContent(CODE);

    return _.get(banner, 'data', '').replace('http://', '//');
});

/**
 * 取消订单
 */
const _cancelReason = orderService.closeReason;

const index = co(function * (uid, udid, channel, isStudent) {
    let reqData = yield Promise.props({
        msgNumber: _msgNumber(uid, udid),
        recentOrder: _recentOrder(uid),
        guessBrand: _guessYouLikeBrand(),
        newProduct: _newProduct(),
        recommendProduct: _recommend(_channelNum(channel), uid, udid),
        footerBanner: _footerBanner(),
        reason: _cancelReason()
    });

    return {
        certifiedName: isStudent ? '学生身份已验证' : '身份验证',
        certifiedUrl: helpers.urlFormat('/product/students/'),
        cancelReason: reqData.reason,
        content: [
            {
                message: reqData.msgNumber
            },
            {
                latestOrders: reqData.recentOrder,
                favBrand: {
                    more: '/brands',
                    brands: reqData.guessBrand
                },
                newArrival: reqData.newProduct
            }
        ],
        recommend: reqData.recommendProduct,
        banner: reqData.footerBanner,
        helpUsUrl: ''
    };
});


module.exports = {
    index
};