new.js 6.02 KB
/**
 * 新品到着 models
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/7/28
 */

'use strict';
const utils = '../../../utils';
const logger = global.yoho.logger;
const _ = require('lodash');
const contentCode = require('../../../config/content-code');
const resourcesProcess = require(`${utils}/resources-process`);
const searchProcess = require(`${utils}/search-process`);
const productProcess = require(`${utils}/product-process`);
const serviceAPI = global.yoho.ServiceAPI;
const api = global.yoho.API;

/**
 * TODO: remove
 * 商品搜索接口请求
 * @param  {[object]} params
 * @return {[array]}
 */
const _searchGoods = (params) => {
    let method = 'app.search.newProduct';

    // 排除基本筛选项默认值为0的对象
    for (let str in params) {
        if (str !== 'order' && params[str] === '0' || params[str] === null) {
            delete params[str];
        }
    }

    params.yh_channel = searchProcess.getChannelType(params.channel);

    delete params.channel;

    params = _.assign({
        limit: '60'
    }, params);

    params.order = params.order === '0' ? 's_t_desc' : 's_t_asc';

    return api.get('', _.assign({
        method: method
    }, params), {
        cache: true
    });
};

/**
 * 获取新品到着的焦点图资源数据
 */
const getNewFocus = (channel) => {
    return serviceAPI.get('operations/api/v5/resource/get', {
        content_code: contentCode.new[channel]
    }, {
        cache: true
    }).then((result) => {
        if (result && result.code === 200) {
            return resourcesProcess(result.data);
        } else {
            logger.error('get newGoods banner return code is not 200');
            return [];
        }
    });
};

/**
 * TODO remove
 * 获取商品数据
 */
const getSearchData = (params) => {
    return _searchGoods(params).then((result) => {
        if (result && result.code === 200) {
            let newList = {};

            newList.list = productProcess.processProductList(result.data.product_list || [], {
                showTags: true,
                showSimilar: true
            });

            if (parseInt(params.page, 10) === 1) {
                newList.total = result.data.total;
            }

            return newList;
        } else {
            logger.error('get product search api return code is not 200');
            return [];
        }
    });
};

/**
 * TODO remove
 * 获取筛选数据
 * @param  {[object]} params
 * @return {[array]}
 */
const getFilterData = (params) => {
    return _searchGoods(params).then((result) => {
        if (result && result.code === 200) {
            return productProcess.processFilter(result.data.filter || []);
        } else {
            logger.error('get filter data api return code is not 200');
            return [];
        }
    });
};

/**
 * method=app.newproduct.recshop  推荐店铺
 * http://git.yoho.cn/yoho-documents/api-interfaces/blob/master/BigData/新品到着.md
 */
const indexData = (uid, channel, limit, page) => {
    let yh_channel = searchProcess.getChannelType(channel);

    limit = limit || 20;
    page = page || 1;

    let params = {
        method: 'app.newproduct.recshop',
        yh_channel,
        contentCode: contentCode.newV2[channel],
        limit,
        page,
        uid
    };

    return api.get('', params, {cache: true})
        .then(result=> {
            let shopData = _.get(result, 'data', {});

            return shopData;
        })
        .catch(() => {});
};


/**
 * method=app.newproduct.recbrand 推荐品牌
 * http://git.yoho.cn/yoho-documents/api-interfaces/blob/master/BigData/新品到着.md
 */
const recbrand = (uid, channel, limit, page) => {
    let yh_channel = searchProcess.getChannelType(channel);

    limit = limit || 20;
    page = page || 1;

    let params = {
        method: 'app.newproduct.recbrand',
        yh_channel,
        limit,
        page,
        uid
    };

    return api.get('', params, {cache: true})
        .then(result => {
            let data = _.get(result, 'data', {});

            return data;
        })
        .catch(() => {});
};

const newGoodsAPI = (params) => {
    let method = 'app.newproduct.reclist';

    // 排除基本筛选项默认值为0的对象
    for (let str in params) {
        if ((str !== 'order' && params[str] === '0') || params[str] === null) {
            delete params[str];
        }
    }

    params.yh_channel = searchProcess.getChannelType(params.channel);

    delete params.channel;

    params = _.assign({
        limit: '60'
    }, params);

    return api.get('', _.assign({
        method: method
    }, params), {
        cache: true
    });
};

/**
 * method:app.newproduct.reclist  新品上架
 * http://git.yoho.cn/yoho-documents/api-interfaces/blob/master/BigData/新品到着.md
 */
const reclist = (uid, channel, searchOptions) => {
    let params = Object.assign({}, {uid, channel}, searchOptions);

    return newGoodsAPI(params).then(result => {
        if (result && result.code === 200) {
            let newList = {
                channel: channel
            };

            newList.list = productProcess.processProductList(result.data.product_list || [], {showTags: true});

            if (parseInt(params.page, 10) === 1) {
                newList.total = result.data.total;
                newList.pageTotal = result.data.page_total;
            }

            return newList;
        } else {
            logger.error('get product search api return code is not 200');
            return [];
        }
    });
};

const reclistFilter = (uid, channel) => {
    let params = Object.assign({}, {uid, channel});

    return newGoodsAPI(params).then(result => {
        if (result && result.code === 200) {
            return productProcess.processFilter(result.data.filter || []);
        } else {
            logger.error('get filter data api return code is not 200');
            return [];
        }
    });
};

module.exports = {
    getNewFocus,
    getSearchData, // TODO remove
    getFilterData, // TODO remove

    indexData,
    recbrand,
    reclist,
    reclistFilter,
    filterDataApi: _searchGoods
};