helpers.js 9.64 KB
/**
 * 列表页、搜索数据处理工具类
 *
 * @author: jiangfeng<jeff.jiang@yoho.cn>
 *
 */

'use strict';

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

const helpers = {

    /**
     * 生成 A-Z, 0-9 品牌首字母
     * @param numberIndex
     * @returns {Array}
     */
    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;
    },

    getLettersFromBrand(brands, numberIndex) {
        numberIndex = numberIndex || 0;

        let letters = new Set();
        let other = false;

        _.forEach(brands, (b) => {
            let name = b.brandNameEn || b.brandName;
            let char = name.toLowerCase().charAt(0);

            if ((char >= 'a' && char <= 'z') || (char >= 'A' && char <= 'Z')) {
                letters.add(char);
            } else {
                other = true;
            }
        });

        letters = Array.from(letters).sort().map(s => {
            return {
                letter: s.toUpperCase(),
                selected: false
            };
        });

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

        return letters;
    },

    /**
     * 男女条件
     * @returns {*[]}
     */
    genders() {
        return [
            {
                name: '男士',
                value: '1,3'
            }, {
                name: '女士',
                value: '2,3'
            }];
    },

    /**
     * 颜色条件处理
     * @param colors
     * @returns {*}
     */
    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 [];
        }
    },

    /**
     * 根据频道获取导航栏
     * @returns {{link: string, pathTitle: string, name: string}}
     */
    getChannelNav() {
        return {
            link: '#',
            pathTitle: '',
            name: 'MEN首页'
        };
    },

    /**
     * 根据品类生成导航栏
     * @param sort
     * @param sorts
     * @returns {Array}
     */
    getSortNav(msort, misort, sorts) {
        let nav = [];
        let sortQuery = '?';

        if (msort && sorts) {
            sorts.forEach(s => {
                if (s.categoryId === msort) {
                    sortQuery += 'msort=' + msort;
                    s.checked = true;
                    nav.push({
                        link: sortQuery,
                        pathTitle: '',
                        name: s.categoryName
                    });
                }
                if (s.sub && misort) {
                    s.sub.forEach(m => {
                        if (m.categoryId === misort) {
                            sortQuery += '&misort=' + misort;
                            m.checked = true;
                            nav.push({
                                link: sortQuery,
                                pathTitle: '',
                                name: m.categoryName
                            });
                        }
                    });
                }
            });
        }
        return nav;
    },

    /**
     * 生成Filter对象
     * @param key
     * @param value
     * @param name
     * @returns {{key: *, value: *, name: *}}
     */
    newFilter(key, value, name) {
        return {
            key: key,
            value: value,
            name: name
        };
    },

    sortConvert(sorts) {
        return _.map(sorts, s => {
            return {
                categoryId: s.sortId,
                categoryName: s.sortName,
                sub: helpers.sortConvert(s.sub)
            };
        });
    },

    /**
     * 筛选器数据处理
     * @param filter
     * @param q
     * @returns {*}
     */
    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 singleSort = false;
        let filters = [];
        let customPriceLow = '';
        let customPriceHigh = '';
        let showSize = (!!q.sort || !!q.misort);

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

        if (sorts && sorts.length === 1) {
            singleSort = true;

            if (sorts[0].sub && sorts[0].sub.length === 1) {
                sorts[0].sub[0].checked = true;
            }

            sorts = sorts[0].sub;
        }

        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) {
            showSize = false;
            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) {
                brandNames = brandNames.length > 10 ? brandNames.substr(0, 10) + '...' : 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,
            singleSort: singleSort,
            brandData: brands,
            colors: colors,
            size: sizeInfo,
            priceRange: priceRange,
            filters: filters,
            showFilters: filters.length > 0,
            letters: this.getLettersFromBrand(brands),
            customPriceLow: customPriceLow,
            customPriceHigh: customPriceHigh,
            showSize: showSize,
            showPrice: true,
            nav: this.getSortNav(q.msort, q.misort, sorts)
        };
    },

    /**
     * 商品列表数据处理
     * @param list
     * @returns {*}
     */
    handleProductList(list, q) {
        if (_.isArray(list)) {
            list.forEach(g => {
                let goodsList = g.goodsList;
                let defaultImages = g.defaultImages;
                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`;
                        if (q.color && q.color === o.colorId) {
                            defaultImages = o.imagesUrl;
                        }
                    });
                }
                g.salesPrice = g.salesPrice || g.marketPrice;
                g.defaultImages = defaultImages;
                if (g.salesPrice === g.marketPrice) {
                    delete g.marketPrice;
                }
                g.url = `${config.siteUrl}/product/pro_${g.productId}_${goodsId}/${g.cnAlphabet}.html`;
            });
        }
        return list;
    }
};

module.exports = helpers;