outletsList.js 2.42 KB
/*
 * @Author: Targaryen
 * @Date:   2016-06-02 15:11:15
 * @Last Modified by:   Targaryen
 * @Last Modified time: 2016-06-08 14:57:54
 */

'use strict';
const utils = '../../../utils';
const Promise = require('bluebird');

const SaleApiModel = require('./sale-api');
const OutletsApiModel = require('./outlets-api');
const productProcess = require(`${utils}/product-process`);
const publicHandler = require('./public-handler');

/**
 * 获取商品列表商品数据 Controller 调用
 * @param  {[type]} params [常规参数]
 * @param  {[type]} extra  [左侧列表额外要拼接的参数]
 * @return {[type]}        [description]
 */
function getListData(params, channel) {
    let apiArr = [];

    if (params.productPool) {
        // 奥莱活动页调用app.search.sales
        apiArr = [
            this.saleApi.getSaleGoodsList({limit: '1', channel: channel}),
            this.saleApi.getSaleGoodsList(Object.assign(params, {channel: channel}))
        ];
    } else {
        // 奥莱品类页调用app.search.li
        apiArr = [
            this.outletsApi.getOutletsGoodsList({limit: '1', channel: channel}),
            this.outletsApi.getOutletsGoodsList(Object.assign(params, {channel: channel}))
        ];
    }
    return Promise.all(apiArr).then(result => {
        let finalResult = {};

        // 获取商品数据和顶部筛选条件
        if (result[0].code === 200) {
            finalResult.leftContent = publicHandler.handleSaleSortData(result[0].data.filter.group_sort, params);
            finalResult.pathNav = publicHandler.handlePathNavData(result[0].data.filter.group_sort, params);

        }

        // 获取左侧类目数据
        if (result[1].code === 200) {
            Object.assign(finalResult, {
                filters: publicHandler.handleSaleFilterData(result[1].data.filter, params),
                opts: publicHandler.handleSaleOptsData(params, result[1].data.total),
                totalCount: result[1].data.total,
                footPager: publicHandler.handlePagerData(result[1].data.total, params),
                goods: productProcess.processProductList(result[1].data.product_list)
            });
        }

        return finalResult;
    });
}

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

        this.outletsApi = new OutletsApiModel(ctx);
        this.saleApi = new SaleApiModel(ctx);

        this.getListData = getListData.bind(this);
    }
};