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

/**
 * 根据频道判断出性别
 */
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':
        case '1,3':
            seoGender = '男士';
            break;
        case '2':
        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 = {};
    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);
    }

    if (seniors.length) {
        seoParams.senior = _.join(seniors, ',');
    }

    return seoParams;
};

/**
 * 品类列表页 SEO 数据
 * @param {*} gender
 * @param {*} sort_name
 * @param isNewArrival 是否是新品到着
 */
const getListSeoData = (params, isNewArrival) => {
    let genderName = _.get(params, 'channel', '');
    let seoData = {};
    let seoParams = {
        color: '',
        size: '',
        style: '',
        brand: '',
        gender: '',
        sort: '',
        price: '',
        senior: '',
        channel: ''
    };

    if (_.get(params, 'channel', null) === '') {
        delete params.channel;
    }

    if (_.isEmpty(params)) {
        seoData = {
            title: '【潮流商品】_潮流男装_潮流女装_潮流童装-YOHO!BUY 有货',
            keywords: '潮流服装配饰,创意生活用品,男生服装配饰,女生服装配饰,潮童服装配饰',
            description: '潮流网购就上YOHO!BUY有货,有货网潮流商品专卖店提供潮流商品品牌、潮流商品价格、潮流商品图片等信息。销售正品新款潮流男装、潮流女装、潮流童装,全场低至4折,★全场品牌正品保证,支持货到付款!★' // eslint-disable-line
        };
    } else {
        _.assign(seoParams, params);

        delete params.sort;

        if (_.isEmpty(params)) {
            seoData = {
                title: `【${seoParams.sort}${isNewArrival ? '新品': ''}_${seoParams.sort}价格,${seoParams.sort}图片,${seoParams.sort}款式-YOHO!BUY 有货`, // eslint-disable-line
                keywords: `${seoParams.sort},${seoParams.sort}品牌,${seoParams.sort}价格,${seoParams.sort}图片`, // eslint-disable-line
                description: `潮流网购就上YOHO!BUY有货,有货网${seoParams.sort}专卖店提供${seoParams.sort}品牌、${seoParams.sort}价格、${seoParams.sort}图片等信息。销售正品新款${seoParams.sort},全场低至4,★全场品牌正品保证,支持货到付款!★` // eslint-disable-line
            };
        } else {
            seoData = {
                title: `【${genderName}${seoParams.color}${seoParams.senior}${seoParams.brand}${seoParams.sort}${isNewArrival ? '新品': ''}_价格,图片,款式-YOHO!BUY 有货`, // eslint-disable-line
                keywords: `${genderName}${seoParams.color}${seoParams.senior}${seoParams.brand}${seoParams.sort}`, // eslint-disable-line
                description: `潮流网购就上YOHO!BUY有货,有货网${seoParams.sort}专卖店提供${seoParams.sort}品牌、${seoParams.sort}价格、${seoParams.sort}图片等信息。销售正品新款${seoParams.sort},全场低至4,★全场品牌正品保证,支持货到付款!★` // 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) { // 模糊查询参数
        let queryString = stringProcess.decodeURIComponent(params.query).replace(/\+/g, ',');

        finalParams.query = queryString;
    }
    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';
    } else if (params.order && params.order !== '0' && params.order !== '1') {
        finalParams.order = params.order;
    }
    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;
    }
    if (params.saleType) {
        finalParams.saleType = params.saleType;
    }
    if (params.physical_channel) {
        finalParams.physical_channel = params.physical_channel;
    }
    if (params.from) {
        finalParams.from = params.from;
    }

    finalParams = _.mapValues(finalParams, value => {
        return stringProcess.decodeURIComponent(value);
    });

    return finalParams;
};

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