product-list.js 2.37 KB
/**
 *  产品列表页 controller
 * @author 陈轩 <xuan.chen@yoho.cn>
 */
'use strict';

const _ = require('lodash');
const api = global.yoho.API;
const camelCase = global.yoho.camelCase;
const prettyFilter = require(`${global.utils}/beautify/filters`);
const processProductList = require(`${global.utils}/beautify/product`);


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

    res.render('product-list', 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 || !result.hasOwnProperty('code')) {
                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.hasOwnProperty('code')) {
                result.code = 200;
            }
            if (result.code === 200) {
                prettyFilter(result.data.filter);
                result.data.productList = processProductList(result.data.productList);
                result = camelCase(result);
            }

            res.json(result);
        })
        .catch(next);
};