list.js 3.76 KB
/**
 * 列表页、新品到着列表页
 *
 * @author: jiangfeng<jeff.jiang@yoho.cn>
 *
 */

'use strict';

const _ = require('lodash');
const Search = require('../models/search');
const camelCase = global.yoho.camelCase;
const DataHelper = require('../models/helpers');
const Resouces = require('../models/resouces');

const list = {
    // 列表页
    index: (req, res, next) => {
        let q = req.query;
        let channel = req.cookies._Channel || 'men';

        q.page = parseInt(q.page || 1, 10);
        q.order = q.order || 's_n_desc';
        q.channel = channel;

        let retData = {
            module: 'product',
            page: 'list',
            title: '列表'
        };

        Promise.all([Search.queryAllSort(), Search.queryProduct(q)]).then(allResult => {
            let allSort = camelCase(allResult[0]);
            let result = allResult[1];

            if (result && result.code === 200 && result.data) {
                let data = camelCase(result.data);
                let nav = [DataHelper.getChannelNav(channel)];

                if (data.filter) {
                    data.filter.groupSort = DataHelper.sortConvert(allSort.data.sort);
                    retData.filter = DataHelper.filterHandle(data.filter, q);
                    retData.filter.showPrice = data.total > 10;
                    nav = _.concat(nav, retData.filter.nav);
                }

                retData.navPath = {
                    nav: nav
                };

                retData.paginationData = {
                    page: q.page,
                    limit: data.limit || 45,
                    total: data.total,
                    pageTotal: data.pageTotal,
                    queryParams: q
                };
                res.display('list', _.assign(retData, {
                    products: DataHelper.handleProductList(data.productList, q),
                    order: q.order
                }));
            } else {
                return Promise.reject('query product error');
            }
        }).catch(next);

    },

    // 新品到着
    newPage: (req, res, next) => {
        let q = req.query;

        q.page = parseInt(q.page || 1, 10);
        q.order = q.order || 's_n_desc';

        let retData = {
            module: 'product',
            page: 'list',
            title: '新品'
        };

        Promise.all([Resouces.newProductBanner(), Search.queryAllSort(), Search.queryNewProduct(q)]).then(result => {
            let banner = result[0];
            let sortData = camelCase(result[1]);
            let listData = result[2];
            let nav = [DataHelper.getChannelNav(), {
                name: '新品'
            }];

            retData.banner = banner;

            if (listData && listData.code === 200 && listData.data) {
                let data = camelCase(listData.data);

                if (data.filter) {
                    data.filter.groupSort = DataHelper.sortConvert(sortData.data.sort);
                    retData.filter = DataHelper.filterHandle(data.filter, q);
                    retData.filter.showPrice = data.total > 10;
                }

                retData.navPath = {
                    nav: nav
                };

                retData.paginationData = {
                    page: q.page,
                    limit: data.limit || 45,
                    total: data.total,
                    pageTotal: data.pageTotal,
                    queryParams: q
                };
                res.display('newList', _.assign(retData, {
                    products: DataHelper.handleProductList(data.productList, q),
                    order: q.order
                }));
            } else {
                return Promise.reject('query product error');
            }
        }).catch(next);

    }
};

module.exports = list;