list.js 1.74 KB
'use strict';

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

const list = {
    index: (req, res, next) => {
        let page = req.query.page || 1;
        let brand = req.query.brand || 0;
        let sort = req.query.sort || '';
        let gender = req.query.gender || '';
        let price = req.query.price || '';
        let size = req.query.size || '';
        let color = req.query.color || '';
        let order = req.query.order || '';

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

        Search.queryProduct({
            page: page,
            brand: brand,
            sort: sort,
            gender: gender,
            size: size,
            price: price,
            color: color,
            order: order
        }).then(result => {

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

                if (data.filter) {
                    retDate.filter = DateHelper.filterHandle(data.filter, req.query);
                }

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

    }
};

module.exports = list;