search.js 2.25 KB
/**
 *  产品搜索 controller
 * @author 陈轩 <xuan.chen@yoho.cn>
 */
'use strict';
const path = require('path');
const _ = require('lodash');
const api = global.yoho.API;
const camelCase = global.yoho.camelCase;
const prettyFilter = require(path.join(global.utils, '/beautify/filters'));
const processProductList = require(path.join(global.utils, '/beautify/product')).processProductList;


/* 搜索 页面 */
exports.index = (req, res) => {
    const view = {
        module: 'product',
        page: 'search'
    };

    res.render('search', view);
};

/* 筛选的二级页面 */
exports.subFilter = (req, res) => {
    const view = {
        module: 'product',
        page: 'filter-sub'
    };

    res.render('filter-sub', view);
};

/* 获取 筛选配置 */
exports.fetchFilters = (req, res, next) => {
    const params = {
        uid: 14741796, // mock data
        page: req.body.page || 1,
        order: req.body.order || 1,
        yh_channel: req.body.yh_channel || 'all',
        channel: req.body.channel || 'all',
    };

    const data = _.merge({
        method: 'app.search.sales',
    }, params);

    api.post('', data, {
        cache: true,
        code: 200
    })
        .then(result => {
            let filterConfig = {};

            if (result.code === 200) {
                prettyFilter(result.data.filter);
                filterConfig = camelCase(result.data.filter);
            }

            res.json({
                code: result.code,
                data: filterConfig
            });
        })
        .catch(next);
};

/* 查询 产品列表 */
exports.fetchProducts = (req, res, next) => {
    const params = {
        uid: 14741796, // mock data
        page: req.body.page || 1,
        order: req.body.order || 1,
        yh_channel: req.body.yh_channel || 'all',
        channel: req.body.channel || 'all',
    };

    const data = _.merge({
        method: 'app.search.sales',
    }, params);

    api.post('', data, {
        cache: true,
        code: 200
    })
        .then(result => {
            if (result.code === 200) {
                result.data.productList = processProductList(result.data.productList);
                result = camelCase(result);
            }
            res.json(result);
        })
        .catch(next);
};