seoHelper.js 1.31 KB
'use strict';

const _ = require('lodash');


// 品牌按字母分类
const sortBrands = (brands) => {
    let data = {
            '0-9': []
        };

    let number = /^[\d]*$/;
    _.forEach(_.get(brands, 'data'), (val, key) => {
        let brand = {id: val.id, brandName: val.brand_name};

        if (number.test(val.brand_alif)) {
            data['0-9'].push(brand);
        } else {
            if (!data[val.brand_alif]) {
                data[val.brand_alif] = [];
            }
            data[val.brand_alif].push(brand);
        }
    })

    return data;
}

// 获取子分类
const getSubSorts = (sort, sortId) => {
    let noSortIdresult = [],
        specialSorts = [];

    _.forEach(_.get(sort, 'data.sort'), (msort) => {
        if (sortId) {
            if (sortId === msort.sort_id) {
                specialSorts = msort.sub;
                return;
            }

            _.forEach(msort.sub, (misort) => {
                if (sortId === misort.sort_id) {
                    specialSorts = misort.sub;
                    return;
                }
            })
        } else {
            noSortIdresult.push({id: msort.sort_id, sortName: msort.sort_name});
        }
    })
    return !_.isEmpty(specialSorts) ? specialSorts : noSortIdresult;
}


module.exports = {
    sortBrands,
    getSubSorts

}