list.js 2.94 KB
'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;

        q.page = q.page || 1;

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

        Search.queryProduct(q).then(result => {

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

                if (data.filter) {
                    retData.filter = DataHelper.filterHandle(data.filter, q);
                    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),
                    order: q.order
                }));
            } else {
                return Promise.reject('query product error');
            }
        }).catch(next);

    },

    newPage: (req, res, next) => {
        let q = req.query;

        q.page = q.page || 1;

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

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

            retData.banner = banner;

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

                if (data.filter) {
                    retData.filter = DataHelper.filterHandle(data.filter, q);
                }

                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),
                    order: q.order
                }));
            } else {
                return Promise.reject('query product error');
            }
        }).catch(next);

    }
};

module.exports = list;