query.js 1.83 KB
/**
 * 搜索页
 *
 * @author: jiangfeng<jeff.jiang@yoho.cn>
 * @date: 2016/7/12
 */


'use strict';

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

const Query = {
    index: (req, res, next) => {
        let q = req.query;
        let query = q.query || '';

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

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

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

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

                nav.push({
                    name: query ? `"${query}" ${data.total}个结果` : `共${data.total}个结果`
                });

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

    }
};

module.exports = Query;