plusstar.js 3.97 KB
'use strict';
const api = global.yoho.API;
const serviceAPI = global.yoho.ServiceAPI;
const _ = require('lodash');
const productProcess = require('../../../utils/product-process');

/**
 * 获取潮流优选tab
 * @return {[array]}
 */
const getAllChannels = (params) => {
    let gender = params.gender - 1 || 0;

    return api.get('', {
        method: 'app.blk.getAllChannels'
    }).then(result => {
        let data = {channel: []};

        if (result.code !== 200) {
            return data;
        }

        if (gender === 3 && result.data.length === 3) {
            // 1:男,2:女,3:童装,4:创意生活
            // 如果为3,说明童装没有配置,只配置了创意生活。所以要减一
            gender = 2;
        }

        _.forEach(result.data, (res, index) => {
            data.channel.push({
                id: res.channel_id,
                mame: res.channel_name,
                code: res.content_code,
                focus: index === gender ? true : false
            });
        });

        return data;
    });
};

/**
 * 通过skn查询商品详情
 * @param {[string || array]} productSkn 商品skn
 * @return {[array]}
 */
const getProductBatch = (productSkn) => {
    productSkn = _.isArray(productSkn) ? productSkn : [productSkn];
    return api.get('', {
        method: 'h5.product.batch',
        productSkn: productSkn.join(',')
    }).then(result => {
        return result && result.data ? productProcess.processProductList(result.data.product_list) : [];
    });
};

/**
 * 获取资源位数据
 * @param {[string]} content_code
 * @return {[array]}
 */
const getResources = (params) => {
    params = params || {};

    return serviceAPI.get(
	   'operations/api/v5/resource/get',
        params
	).then(result => {
    let data = {
        goods: {},
        recommend: {}
    };
    let list = {};
    let productSkns = [];

    if (result.code !== 200) {
        return data;
    }

    _.forEach(result.data, (res) => {
        list = {};
        switch (res.template_name) {
            case 'focus':
                list = {
                    data: res.data
                };

                if (res.focus_type * 1 === 1) {
                    data.focus1 = [];
                    data.focus1.push(list);
                } else {
                    data.focus2 = list;
                }

                break;
            case 'title_image':
                if (typeof data[res.template_name] === 'undefined') {
                    data[res.template_name] = [];
                }

                list = {
                    title: res.data.title,
                    moreUrl: res.data.more_url,
                    moreName: res.data.more_name,
                    image: res.data.image
                };

                data.title_image.push(list);
                break;
            case 'titleFloor':
                list = {
                    name: res.data.title.name,
                    moreUrl: res.data.title.more_url,
                    moreName: res.data.title.more_name
                };

                if (res.data.title.name === '热门商品') {
                    data.goods.title = list;
                } else if (res.data.title.name === '热门品类') {
                    data.recommend.title = list;
                }

                break;
            case 'recommend_content_five':
                data.recommend.data = res.data.list;
                break;
            case 'goods':
                _.forEach(res.data, (skn) => {
                    productSkns.push(skn.id);
                });

                data[res.template_name].productSkns = productSkns;
                break;
            default:
                break;
        }
    });

    if (_.isEmpty(data.goods.productSkns)) {
        return data;
    }

    return getProductBatch(data.goods.productSkns || []).then(res => {
        data.goods.data = res;
        return data;
    });
});
};

module.exports = {
    getAllChannels,
    getResources,
    getProductBatch
};