search-process.js 10.5 KB
/**
 * 搜索相关数据处理
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/7/29
 */
const _ = require('lodash');

/**
 * 根据频道判断出性别
 */
const getGenderByChannel = (channel) => {
    channel = channel ? channel : 'boys';

    switch (channel) {
        case 'boys': // 男
            return '1,3';
        case 'girls': // 女
            return '2,3';
        default: // 其它
            return '1,2,3';
    }
};

/**
 * seo 获取性别
 */
const _getGenderForSeo = (gender) => {
    let seoGender = '男生|女生';

    switch (gender) {
        case '1,3':
            seoGender = '男生';
            break;
        case '2,3':
            seoGender = '女生';
            break;
        default:
            break;
    }

    return seoGender;
};

// 频道转换
const getChannelType = (channel) => {
    channel = channel ? channel : 'boys';

    switch (channel) {
        case 'boys': // 男
            return '1';
        case 'girls': // 女
            return '2';
        case 'kids': // 童装
            return '3';
        case 'lifestyle': // 创意生活
            return '4';
        default: // all
            return '1,2,3,4';
    }
};

// 排序转换
const getTypeCont = (type, order) => {
    switch (type) {
        case 'price':
            return order === '0' ? 's_p_desc' : 's_p_asc';
        case 'discount':
            return order === '0' ? 'p_d_desc' : 'p_d_asc';
        case 'sale':
            return order === '0' ? 's_n_desc' : 's_n_asc';
        case 'newest':
            return order === '0' ? 's_t_desc' : 's_t_asc';
        case 'stock':
            return order === '0' ? 's_s_desc' : 's_s_asc';
        case 'all':
            return order === '0' ? 'd_s_desc' : 'd_s_asc';
        case 'category':
            return order === '0' ? 's_t_desc' : 's_t_asc';
        case 'new':
            return order === '0' ? 's_t_desc' : 's_t_asc';
        case 'hot':
            return order === '0' ? 's_n_desc' : 's_n_asc';
        case 'popularity':
            return order === '0' ? 'h_v_desc' : 'h_v_asc';
        default:
            return order === '0' ? 's_t_desc' : 's_t_asc';
    }
};

/**
 * 
 * @param 查询参数 queryParams
 * @param 接口数据 apiData
 */
const getFilterValueForSeo = (queryParams, apiData) => {
    let seoParams = {
        color: '',
        size: '',
        style: '',
        brand: '',
        gender: '',
        sort: '',
        price: '',
        senior: ''
    };
    let seniors = [];

    _.forEach(queryParams, (value, key) => {
        if (_.find(['color', 'size', 'style'], o => {
            return key === o;
        })) {
            let colorObj = _.find(_.get(apiData, `filter.${key}`, []), o => {
                return _.parseInt(value) === _.get(o, `${key}_id`);
            });

            seoParams[`${key}`] = _.get(colorObj, `${key}_name`, '');
        }
        if (key === 'brand') {
            let brands = _.get(apiData, 'filter.brand', []);
            let brandObj = _.find(brands, brand => {
                return _.parseInt(value) === brand.id;
            });

            seoParams.brand = _.get(brandObj, 'brand_name', '');
        }
        if (key === 'gender') {
            seoParams.channel = _getGenderForSeo(value);
        }
        if (key === 'sort' || key === 'category_id') {
            let sorts = _.get(apiData, 'filter.group_sort', []);
            let testSorts = _.cloneDeep(sorts);

            _.forEach(sorts, sort => {
                let sortSub = _.get(sort, 'sub', []);

                if (sortSub) {
                    _.forEach(sortSub, subSort => {
                        testSorts.push(subSort);
                    });
                }
            });

            let sortObj = _.find(testSorts, sort => {
                return _.find(_.split(sort.category_id, ','), o => {
                    return value === o;
                });
            });

            seoParams.sort = _.get(sortObj, 'category_name', '');
        }
        if (key === 'price') {
            seoParams.price = `¥${_.replace(value, ',', '-')}`;
        }
        if (key === 'standard') {
            let paramGroups = _.split(value, ',');

            _.forEach(paramGroups, paramGroup => {
                let paramValues = _.split(paramGroup, '_');
                let standards = _.get(apiData, 'standard', []);
                let standardObj = _.find(standards, o => {
                    return paramValues[0] === o.standard_id;
                });
                let standardSeObj = _.find(_.get(standardObj, 'sub', []), o => {
                    return paramValues[1] === o.standard_id;
                });

                seniors.push(_.get(standardSeObj, 'standard_name', ''));
            });
        }
    });

    if (seoParams.style) {
        seniors.push(seoParams.style);
    }

    seoParams.senior = _.join(seniors, ',');

    return seoParams;
};

/**
 * 品类列表页 SEO 数据
 * @param {*} gender
 * @param {*} sort_name
 */
const getListSeoData = (params) => {
    let seoData = {
        title: '潮流服装配饰,创意生活用品_男生|女生|潮童服装,鞋履,配饰品牌正品-YOHO!BUY有货',
        keywords: '潮流服装配饰,创意生活用品,男生服装配饰,女生服装配饰,潮童服装配饰',
        description: '潮流服装配饰及创意生活正品网购!YOHO!BUY有货提供男生、女生、潮童服装配饰。100%品牌正品保证,支持货到付款。'
    };

    if (params) {
        let baseInfo = `${params.brand}${params.sort}`;

        seoData = {
            title: `${baseInfo ? baseInfo + '|' : ''}新款${params.brand}${params.channel}${params.color}${params.size}${params.sort}${params.price},${params.brand}${params.senior}${params.sort}品牌正品|YOHO!BUY有货`, // eslint-disable-line
            keywords: `${baseInfo ? baseInfo + ',' : ''}新款${params.brand}${params.channel}${params.color}${params.size}${params.sort}${params.price},${params.brand}${params.senior}${params.sort}品牌正品`, // eslint-disable-line
            description: `${baseInfo}正品网购!YOHO!BUY有货提供新款${params.brand}${params.channel}${params.color}${params.size}${params.sort}${params.price},${params.brand}${params.senior}${params.sort}100%品牌正品保证,支持货到付款。` // eslint-disable-line
        };
    }
    return seoData;
};

/**
 * 获取筛选的参数,不包括方法名
 */
const getSearchParamsWithoutMethod = (params) => {
    let finalParams = {
        limit: params.limit || 24,
        status: params.status || 1,
        sales: params.sales || 'Y',
        stocknumber: params.stocknumber || 1,
        attribute_not: params.attribute_not || 2
    };

    if (params.query) { // 模糊查询参数
        finalParams.query = params.query.replace(/\+/g, ',');
    }
    if (params.yh_channel || params.channel) { // 频道
        finalParams.yh_channel = params.yh_channel || getChannelType(params.channel);
    }
    if ((params.brand_id && params.brand_id !== '0') || (params.brand && params.brand !== '0')) {
        // 品牌ID
        finalParams.brand = params.brand_id || params.brand;
    }
    if (params.shop_id && params.shop_id !== '0') { // 店铺ID
        finalParams.shop_id = params.shop_id;
    }
    if (params.type) { // 排序类型
        finalParams.type = params.type;
    }
    if (params.type !== 'default') { // 排序参数
        finalParams.order = params.order ? getTypeCont(params.type || '', params.order) : 's_t_asc';
    }
    if (params.limited) {
        finalParams.limited = params.limited;
    }
    if (params.page) {
        finalParams.page = params.page;
    }
    if (params.gender) {
        finalParams.gender = params.gender;
    }
    if (params.color && params.color !== '0') {
        finalParams.color = params.color;
    }
    if (params.price && params.price !== '0') {
        finalParams.price = params.price;
    }
    if (params.size && params.size !== '0') {
        finalParams.size = params.size;
    }
    if (params.p_d) {
        finalParams.p_d = params.p_d;
    }
    if (params.sort && params.sort !== '0') {
        finalParams.sort = params.sort;
    }
    if (params.msort && params.msort !== '0') {
        finalParams.msort = params.msort;
    }
    if (params.misort && params.misort !== '0') {
        finalParams.misort = params.misort;
    }
    if (params.tags_filter) {
        finalParams.tags_filter = params.tags_filter;
    }
    if (params.category_id && params.category_id !== '0') {
        finalParams.category_id = params.category_id;
    }
    if ((params.age_level && params.age_level !== '0') ||
        (params.ageLevel && params.ageLevel !== '0')) {
        finalParams.age_level = params.age_level || params.ageLevel;
    }
    if (params.outlets) {
        finalParams.outlets = params.outlets;
    }
    if (params.new) { // newArrival
        finalParams.new = params.new;
    }
    if (params.style) {
        finalParams.style = params.style;
    }
    if (params.standard) {
        finalParams.standard = params.standard;
    }
    if (params.specialoffer) {
        finalParams.specialoffer = params.specialoffer;
    }
    if (params.productPool || params.filter_poolId) { // 商品池
        finalParams.productPool = params.productPool || params.filter_poolId;
    }
    if (params.promotion) {
        finalParams.promotion = params.promotion;
    }
    if (params.promotion_id) {
        finalParams.promotion_id = params.promotion_id;
    }
    if (params.app_type && params.app_type === '1') {
        finalParams.app_type = 1;
    }
    if (params.coupon_id) { // 个人中心优惠券立即使用 - 商品列表
        finalParams.coupon_id = params.coupon_id;
    }
    if (params.coupon_code) { // 个人中心优惠券立即使用 - 商品列表
        finalParams.coupon_code = params.coupon_code;
    }
    if (params.specialsale_id) { // sale 要用到的参数,预留
        finalParams.specialsale_id = params.specialsale_id;
    }
    if (params.coin) { // 学生返币查询参数处理
        delete finalParams.filter_poolId;

        if (params.type === 'newest') {
            delete finalParams.order;
            delete finalParams.type;
        }
    }
    if (params.isblknew) {
        finalParams.app_type = 1;
        if (params.type === 'default') {
            finalParams.order = 's_t_asc';
        }
    }
    if (params.shelveTime) {
        finalParams.shelveTime = params.shelveTime;
    }

    return finalParams;
};

module.exports = {
    getGenderByChannel,
    getChannelType,
    getTypeCont,
    getFilterValueForSeo,
    getListSeoData,
    getSearchParamsWithoutMethod
};