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


'use strict';

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

const Query = {
    index: (req, res, next) => {
        let q = req.query;
        let query = helper.xssHtml(q.query) || '';
        let channel = req.query._Channel || req.cookies._Channel || 'men';

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

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

        Promise.all([Search.queryProduct(q)]).then(allResult => {
            let result = allResult[0];

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

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

                if (data.filter && data.total) {
                    data.filter.standard = data.standard;

                    retData.filter = DataHelper.filterHandle(data.filter, q);
                    retData.filter.showPrice = data.total > 10;
                    retData.filter.showInfo = (retData.filter.style && retData.filter.style.length > 0)
                        || (data.standard && data.standard.length > 0);  //eslint-disable-line
                    retData.filter.useSmallSort = 1;
                }

                retData.navPath = {
                    nav: nav
                };

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

    },
    suggest: (req, res, next) => {
        Search.getSuggest(req.query).then(result => {
            res.json(result);
        }).catch(next);
    }
};

module.exports = Query;