helpers.js 6.57 KB
'use strict';

const _ = require('lodash');
const config = global.yoho.config;

const helpers = {
    brandLetters(numberIndex) {
        let letters = [];

        numberIndex = numberIndex || 0;

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

        if (numberIndex === -1) {
            letters.push({
                letter: '0-9',
                selected: false
            });
        } else if (numberIndex === 0) {
            letters.unshift({
                letter: '0-9',
                selected: false
            });
        }

        return letters;
    },

    genders() {
        return [
            {
                name: '男士',
                value: '1,3'
            }, {
                name: '女士',
                value: '2,3'
            }];
    },

    colorConvert(colors) {
        if (colors) {
            return colors.map((c) => {
                return {
                    id: c.colorId,
                    title: c.colorName,
                    rgb: c.colorValue ? `url(${c.colorValue})` : '#' + c.colorCode
                };
            });
        } else {
            return [];
        }
    },

    getChannelNav() {
        return {
            link: '#',
            pathTitle: '',
            name: 'MEN首页'
        };
    },
    getSortNav(sort, sorts) {
        let nav = [];

        if (sort && sorts) {
            sorts.forEach(s => {
                if (s.relationParameter.sort === sort) {
                    nav.push({
                        link: '#',
                        pathTitle: '',
                        name: s.categoryName
                    });
                } else if (s.sub) {
                    s.sub.forEach(m => {
                        if (m.relationParameter.sort === sort) {
                            nav.push({
                                link: '#',
                                pathTitle: '',
                                name: s.categoryName
                            });
                            nav.push({
                                link: '#',
                                pathTitle: '',
                                name: m.categoryName
                            });
                        }
                    });
                }
            });
        }
        return nav;
    },

    newFilter(key, value, name) {
        return {
            key: key,
            value: value,
            name: name
        };
    },

    filterHandle(filter, q) {
        let priceRange = filter.priceRange;
        let sizeInfo = filter.size;
        let genders = this.genders();
        let brands = filter.brand;
        let colors = this.colorConvert(filter.color);
        let sorts = filter.groupSort;
        let filters = [];
        let customPriceLow = '';
        let customPriceHigh = '';

        genders.forEach(g => {
            if (g.value === q.gender) {
                g.checked = true;
                filters.push(this.newFilter('gender', q.gender, g.name));
            }
        });

        let matchPrice = false;

        if (priceRange) {
            priceRange = Object.keys(priceRange).map((k) => {
                let prices = k.split(',');
                let p = {
                    lower: prices[0],
                    higher: prices[1]
                };

                if (k === q.price) {
                    p.checked = true;
                    matchPrice = true;
                    filters.push(this.newFilter('price', q.price, ${prices[0]}-¥${prices[1]}`));
                }
                return p;
            }).sort((a, b) => {
                return a.lower - b.lower;
            });

            if (!matchPrice && q.price) {
                let prices = q.price.split(',');
                let priceTxt = ${prices[0]}-¥${prices[1]}`;

                customPriceLow = prices[0];
                customPriceHigh = prices[1];

                if (prices[1] === Number.MAX_SAFE_INTEGER.toString()) {
                    priceTxt = `大于¥${prices[0]}`;
                    customPriceHigh = '';
                }

                filters.push(this.newFilter('price', q.price, priceTxt));

            }
        }


        if (!_.isArray(sizeInfo) && sizeInfo) {
            sizeInfo.checked = true;
            sizeInfo = [sizeInfo];
        }

        if (q.size) {
            sizeInfo.forEach(s => {
                if (s.sizeId === parseInt(q.size, 10)) {
                    s.checked = true;
                    filters.push(this.newFilter('size', q.size, s.sizeName));
                }
            });
        }

        if (q.brand) {
            let brandNames = brands.filter(b => {
                return (',' + q.brand + ',').indexOf(',' + b.id + ',') >= 0;
            }).map(b => {
                b.checked = true;
                return b.brandName;
            }).join('、');

            if (brandNames) {
                filters.push(this.newFilter('brand', q.brand, brandNames));
            }
        }

        if (q.color) {
            colors.forEach(c => {
                if (c.id === parseInt(q.color, 10)) {
                    c.cur = true;

                    let fi = this.newFilter('color', q.color, c.title);

                    fi.color = c;
                    filters.push(fi);
                }
            });
        }

        return {
            people: genders,
            sortData: sorts,
            brandData: brands,
            colors: colors,
            size: sizeInfo,
            priceRange: priceRange,
            filters: filters,
            showFilters: filters.length > 0,
            letters: this.brandLetters(),
            customPriceLow: customPriceLow,
            customPriceHigh: customPriceHigh,
            showSize: !!q.sort,
            showPrice: true,
            nav: this.getSortNav(q.sort, sorts)
        };
    },

    handleProductList(list) {
        if (_.isArray(list)) {
            list.forEach(g => {
                let goodsList = g.goodsList;
                let goodsId;

                if (goodsList && goodsList.length > 0) {
                    goodsId = goodsList[0].goodsId;

                    goodsList.forEach(o => {
                        o.url = `${config.siteUrl}/product/pro_${g.productId}_${o.goodsId}/${o.cnAlphabet}.html`;
                    });
                }

                g.url = `${config.siteUrl}/product/pro_${g.productId}_${goodsId}/${g.cnAlphabet}.html`;
            });
        }
        return list;
    }
};

module.exports = helpers;