new.js 6.9 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;

module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }

    /**
     * TODO: remove
     * 商品搜索接口请求
     * @param  {[object]} params
     * @return {[array]}
     */
    _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 this.get({
            data: _.assign({
                method: method
            }, params),
            param: {
                cache: true
            }
        });
    }

    /**
     * 获取新品到着的焦点图资源数据
     */
    getNewFocus(channel) {
        return this.get({
            api: serviceAPI,
            url: 'operations/api/v5/resource/get',
            data: {
                content_code: contentCode.new[channel]
            },
            param: {
                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
     * 获取商品数据
     */
    getSearchData(params) {
        return this._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]}
     */
    getFilterData(params) {
        return this._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
     */
    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 this.get({
            data: params,
            param: {
                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
     */
    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 this.get({
            data: params,
            param: {
                cache: true
            }
        }).then(result => {
            let data = _.get(result, 'data', {});

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

    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 this.get({
            data: _.assign({
                method: method
            }, params),
            param: {
                cache: true
            }
        });
    }

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

        return this.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 [];
            }
        });
    }

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

        return this.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 [];
            }
        });
    }

    filterDataApi(params) {
        return this._searchGoods(params);
    }
};