newArrive-service.js 10.3 KB
/*
 * @Author: hongwei.gao
 * @Date:   2016-12-28
 */
'use strict';

const headerModel = require('../../../doraemon/models/header');
const api = require('./newArrive-api');
const _ = require('lodash');
const searchHandler = require('./search-handler');
const publicHandler = require('./public-handler');
const helpers = global.yoho.helpers;
const moment = require('moment');

const channelMap = {
    boys: {
        code: '29ce9c740c452b97d421f9630893dfe6',
        yh_channel: 1,
        gender: '1,3'
    },
    girls: {
        code: 'cc145b163edb010c5d6320a2c680f24a',
        yh_channel: 2,
        gender: '2,3'
    },
    kids: {
        code: '4eed401c1ba714099585b593dd25dfbe',
        yh_channel: 3,
        gender: '2,3'
    },
    lifestyle: {
        code: 'c6c7421b3fddddf04799c13d3a84b4f2',
        yh_channel: 4,
        gender: '2,3'
    }
};

/**
 * 处理列表数据、及页码
 * @param data
 * @returns {{footPager: type[], newProList: Array}}
 * @private
 */
const _proListHandler = (data, params) => {
    let total = data.total || '',
        proList = data.product_list || [],
        newProList = [];

    // 页码
    let footPager = searchHandler.handlePagerData(total,
            Object.assign(params, {limit: _.get(data, 'page_size', '50')}), true);

    // 商品列表
    _.forEach(proList, pro => {
        let proObj = {};

        // 应季热门
        if (pro.recommend_type === 'seasonSort') {
            let tags = [];

            _.forEach(pro.data, subData => {
                tags.push({
                    url: helpers.urlFormat('', {sort: subData.relationParameter}, 'list'),
                    name: subData.categoryName,
                    cateId: subData.categoryId
                });
            });

            proObj.tags = tags;
            proObj.tagType = true;
        }

        // 热门搜索
        if (pro.recommend_type === 'hotSearchTerm') {
            let tags = [];

            _.forEach(pro.data, subData => {
                tags.push({
                    url: helpers.urlFormat('', {query: subData}, 'search'),
                    name: subData
                });
            });

            proObj.tags = tags;
            proObj.tagType = true;
            proObj.hotSearch = true;
        }

        // 店铺
        if (pro.recommend_type === 'hotShop') {

            proObj = {
                url: helpers.urlFormat('', {shopId: pro.data.shops_id}, pro.data.shop_domain),
                src: pro.data.shop_logo,
                newNum: pro.data.new_product_num,
                collNum: pro.data.favorite_num,
                shopId: pro.data.shops_id,
                shopName: pro.data.shop_name
            };
            proObj.shopType = true;
        }

        // 文章
        if (pro.recommend_type === 'fashionArticle') {

            proObj = {
                url: helpers.urlFormat('/' + pro.data.id + '.html', {channel: params.channel}, 'guang'),
                src: pro.data.src,
                title: pro.data.title,
                publishTime: moment(parseInt(pro.data.publish_time_long, 10)).format('YYYY年MM月DD HH:mm'),
                num: pro.data.browse || 0,
                articalId: pro.data.id
            };
            proObj.articalType = true;
        }

        // 普通商品
        if (!pro.recommend_type) {
            let defaultGoods = _.find(pro.goods_list, {is_default: 'Y'});

            // 无默认商品取商品列表第一个
            if (!defaultGoods) {
                defaultGoods = pro.goods_list[0];
            }
            proObj = {
                url: helpers.getUrlBySkc(pro.product_skn),
                src: pro.default_images,
                proName: pro.product_name,
                salesPrice: pro.sales_price,
                proSkn: pro.product_skn
            };
            proObj.goodsType = true;
        }
        proObj.recType = pro.recommend_type;
        newProList.push(proObj);
    });

    return {
        footPager: footPager,
        newProList: newProList
    };
};

/**
 * 加载新品到着首页
 * @param req
 * @returns {Promise.<TResult>}
 */
const index = (req) => {

    let channel = req.yoho.channel;
    let params = {
        yh_channel: channelMap[channel].yh_channel,
        channel: channel,
        uid: req.user.uid || ''
    };

    let shopParams = Object.assign({}, params, {contentCode: channelMap[channel].code});
    let recParams = Object.assign({}, params, {
        sort: req.query.sort || '',
        page: req.query.page || 1
    });

    let cateId = req.query.cateId || '';

    let apiMethod = [
        headerModel.requestHeaderData(channel),
        api.recShop(shopParams),
        api.recBrand(params),
        api.recList(Object.assign({}, params)), // 获取品类
        api.recList(recParams)// 获取商品数据
    ];

    return Promise.all(apiMethod).then(result => {
        var respData = {
            slide: {
                list: []
            },
            recommond: {
                name: '为您推荐',
                dataList: []
            },
            newBrand: {
                name: '新品牌入驻',
                brandList: []
            },
            newProduct: {
                name: '最新上架',
                newProList: [],
                ctyList: []
            },
            firstLook: {
                name: '精彩抢先看',
                lookBanner: {
                    list: []
                },
                lookTwoImg: []
            }
        };

        // 头部数据
        Object.assign(respData, result[0]);

        // 面包屑头部
        Object.assign(respData, searchHandler.handlePathNavData({}, {}, 'new', channel));

        if (result[1].code === 200 && result[1].data) {

            _.forEach(_.get(result, '[1].data.ads', []), ads => {
                // banner
                if (ads.template_name === 'focus') {
                    Object.assign(respData.slide.list, ads.data);
                }

                // 精彩想先看,左轮播右2图
                if (ads.template_name === 'NL2R') {
                    Object.assign(respData.firstLook.lookBanner.list, ads.data.left);
                    Object.assign(respData.firstLook.lookTwoImg, ads.data.right);
                }
            });
        }

        // 新品牌入驻
        if (result[2].code === 200 && result[2].data) {
            let hotBrandsData = _.get(result, '[2].data.hot_brand_list', []),
                recList = [];

            _.forEach(hotBrandsData, hotBrand => {
                recList.push({
                    url: helpers.urlFormat('', {shopId: hotBrand.shops_id}, hotBrand.shop_domain),
                    src: hotBrand.shop_logo,
                    num: hotBrand.new_product_num,
                    shopId: hotBrand.shops_id,
                    brandId: hotBrand.brand_id
                });
            });

            // 为您推荐
            Object.assign(respData.recommond.dataList, recList);

            let brandsData = _.get(result, '[2].data.new_brand_list', []),
                brandList = [];

            _.forEach(brandsData, brand => {
                brandList.push({
                    url: helpers.urlFormat('', {shopId: brand.shops_id}, brand.shop_domain),
                    src: brand.shop_logo,
                    shopId: brand.shops_id,
                    brandId: brand.brand_id
                });
            });

            Object.assign(respData.newBrand.brandList, brandList);
        }

        // 最新上架
        if (result[3].code === 200 && result[3].data) {
            let groupSort = _.get(result, '[3].data.filter.group_sort', []),
                ctyList = [];

            // 推荐品类
            _.forEach(groupSort, group =>{
                _.forEach(group.sub, subSort =>{
                    let cateObj = {
                        cateId: subSort.category_id,
                        url: publicHandler.handleFilterUrl(params, {
                            sort: subSort.relation_parameter.sort,
                            cateId: subSort.category_id
                        }),
                        name: subSort.category_name,
                        active: false
                    };

                    if (ctyList.length < 20) {
                        // 默认选中判断
                        if (cateId === subSort.category_id) {
                            cateObj.active = true;
                        }
                        ctyList.push(cateObj);
                    } else {
                        return true;
                    }
                });
            });
            Object.assign(respData.newProduct.ctyList, ctyList);
        }

        // 最新上架
        if (result[4].code === 200 && result[4].data) {
            // 列表数据
            Object.assign(respData.newProduct, _proListHandler(result[4].data,
                Object.assign({}, recParams, {channel: channel, cateId: cateId})));
        }

        // 数据为空,不显示页面模块
        if (_.isEmpty(respData.slide.list)) {
            delete respData.slide;
        }
        if (_.isEmpty(respData.recommond.dataList)) {
            delete respData.recommond;
        }
        if (_.isEmpty(respData.newBrand.brandList)) {
            delete respData.newBrand;
        }
        if (_.isEmpty(respData.newProduct.newProList) && _.isEmpty(respData.newProduct.ctyList)) {
            delete respData.newProduct;
        }
        if (_.isEmpty(respData.firstLook.lookBanner.list) && _.isEmpty(respData.firstLook.lookTwoImg)) {
            delete respData.firstLook;
        }
        return respData;
    });
};

/**
 * 加载最新上架数据
 * @param req
 * @returns {Promise.<TResult>}
 */
const getRecList = (req) => {
    let channel = req.yoho.channel;

    let params = {
        yh_channel: channelMap[channel].yh_channel,
        channel: channel,
        uid: req.user.uid || ''
    };

    Object.assign(params, {
        sort: req.query.sort || '',
        cateId: req.query.cateId || '',
        page: req.query.page || 1
    });

    let apiMethod = [
        api.recList(params)
    ];

    return Promise.all(apiMethod).then(result => {
        let respData = {};

        if (result[0].code === 200 && result[0].data) {
            Object.assign(respData, _proListHandler(result[0].data, params));
        }
        return respData;
    });
};

module.exports = {
    index,
    getRecList
};