list.js 3.16 KB

'use strict';

const Search = require('../models/list');
const camelCase = global.yoho.camelCase;

function brandLetters() {
    let letters = [];

    letters.push({
        letter: '0-9',
        selected: false
    });

    for (let i = 'A'.charCodeAt(0); i <= 'Z'.charCodeAt(0); i++) {
        letters.push({
            letter: String.fromCharCode(i),
            selected: false
        });
    }

    return letters;
}

function peopleChoose(channel) {
    return [
        {
            name: '男士',
            value: '0',
            checked: channel === 'men'
        }, {
            name: '女士',
            value: '1',
            checked: channel === 'women'
        }];
}

const list = {
    index: (req, res, next) => {
        let channel = req.query.channel || req.cookies._Channel;
        let sex = req.query.sex;

        if (sex === '0') {
            channel = 'men';
        } else if (sex === '1') {
            channel = 'women';
        }

        Search.queryProduct({}).then(result => {
            if (result && result.code === 200 && result.data) {
                let data = camelCase(result.data);
                let priceRange = data.filter.priceRange;

                priceRange = Object.keys(priceRange).map((k) => {
                    let prices = k.split(',');

                    return {
                        lower: prices[0],
                        higher: prices[1]
                    };
                }).sort((a, b) => {
                    return a.lower - b.lower;
                });
                res.display('list', {
                    letters: brandLetters(),
                    people: peopleChoose(channel),
                    sortData: data.filter.groupSort,
                    brandData: data.filter.brand,
                    colors: data.filter.color,
                    size: data.filter.size,
                    priceRange: priceRange,
                    total: data.total,
                    pageTotal: data.pageTotal,
                    pageNo: data.page,
                    products: data.productList,
                    navPath: {
                        nav: true,
                        pathNav: [
                            {
                                link: true,
                                href: '',
                                pathTitle: '',
                                name: 'MEN首页'
                            },
                            {
                                link: true,
                                href: '',
                                pathTitle: '',
                                name: '咨询'
                            },
                            {
                                link: false,
                                href: '',
                                pathTitle: '',
                                name: '咨询'
                            }
                        ]
                    },
                    module: 'product',
                    page: 'list',
                    title: '列表'
                });
            } else {
                throw 'query product error';
            }
        }).catch(next);

    }
};

module.exports = list;