seo-handler.js 7.35 KB
/**
 * seo handler
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2017/12/22
 */

const _ = require('lodash');

const getSeoCheckedParamsName = (sorts, checked) => {
    let kd = {};

    _.forEach(sorts, val => {
        if (val.active) {
            kd.sort = val && val.name;
            _.forEach(val.childList, sub => {
                if (sub.childActive && (!sub.categoryId || sub.categoryId !== val.categoryId)) {
                    kd.sort = sub && sub.name;
                }
            });
        }
    });

    checked = checked || [];
    _.forEach(checked, ck => {
        if (!(ck && ck.itemType)) {
            return false;
        }

        switch (ck && ck.itemType) {
            case 'brand':
            case 'color':
            case 'size':
            case 'price':
            case 'style':
                kd[ck.itemType] = ck.name;
                break;
            case 'gender':
                kd.channel = ck.name === 'GIRLS' ? '女生' : '男生';
                break;
            default:
                if (ck.itemType) {
                    if (!kd.senior) {
                        kd.senior = '';
                    } else {
                        kd.senior += '、';
                    }
                    kd.senior += ck.name;
                }
                break;
        }
    });

    return kd;
};

const saleDefaultSeoMap = {
    vip: {
        title: 'VIP会员专享|会员专享|YOHO!BUY有货',
        keywords: '会员专享,VIP专享,专享商品',
        description: '有货网为您带VIP会员专享精选商品,大量潮流品牌商品,VIP价格优惠幅度大,支持货到付款,让您全面了解VIP会员专享商品价格、品牌、图片、折扣等方面信息,想网购VIP会员专享商品就上有货网!' // eslint-disable-line
    },
    breakingYards: {
        title: '断码商品|断码折扣商品|YOHO!BUY有货',
        keywords: '断码商品、断码专区、断码折扣',
        description: '有货网为您带断码专区精选商品,大量断码商品,价格优惠幅度大,支持货到付款,让您全面了解潮流断码商品价格、品牌、图片、折扣等方面信息,想网购潮流断码区商品就上有货网!'
    },
    newSale: {
        title: '降价优选商品|潮流降价精品|YOHO!BUY有货',
        keywords: '降价优选、降价商品,潮流商品',
        description: '有货网为您带来降价优选商品,大量降价折扣优选商品,价格优惠幅度大,质量保证,支持货到付款,让您全面了解降价优选商品价格、品牌、图片、折扣等方面信息,想网购潮流降价商品就上有货网!'
    }
};

const getSaleListSeo = (sorts, checked, type) => {
    const defaultTdk = saleDefaultSeoMap[type] || {};
    const kd = getSeoCheckedParamsName(sorts, checked);

    const typeNameMap = {
        vip: 'VIP特惠',
        breakingYards: '断码',
        newSale: '降价'
    };

    // 无筛选参数时默认tdk
    if (_.isEmpty(kd)) {
        return defaultTdk;
    }

    _.forEach(['sort', 'brand', 'color', 'size', 'price', 'channel', 'style', 'senior'], val => {
        kd[val] = kd[val] || '';
    });

    return {
        title: `SALE${typeNameMap[type] || ''}超值潮品|${kd.price ? '价格在' : ''}${kd.price}${kd.brand}${kd.channel}${kd.color}${kd.size}${kd.sort}|YOHO!BUY有货`, // eslint-disable-line
        keywords: `${kd.size}${kd.color}${kd.brand}${kd.channel}${kd.sort}${kd.style ? ',' : ''}${kd.style}` || defaultTdk.keywords, // eslint-disable-line
        description: `超值潮流网购${kd.brand}就上YOHO!BUY有货,有货网销售正品新款${kd.size}${kd.color}${kd.brand}${kd.channel}${kd.sort},全场低至4,100%品牌正品保证,支持货到付款。` // eslint-disable-line
    };
};

const getGlobalListSeo = (sorts, checked) => {
    const defaultTdk = {
        title: '全球购潮流服装配饰,创意生活用品_男生|女生|潮童服装,鞋履,配饰品牌正品-YOHO!BUY有货',
        keywords: '潮流服装配饰,创意生活用品,男生服装配饰,女生服装配饰,潮童服装配饰',
        description: '全球购潮流服装配饰及创意生活正品网购!YOHO!BUY有货提供男生、女生、潮童服装配饰。100%品牌正品保证,支持货到付款。'
    };

    const kd = getSeoCheckedParamsName(sorts, checked);

    // 无筛选参数时默认tdk
    if (_.isEmpty(kd)) {
        return defaultTdk;
    }

    _.forEach(['sort', 'brand', 'color', 'size', 'price', 'channel', 'style', 'senior'], val => {
        kd[val] = kd[val] || '';
    });

    return {
        title: `全球购${kd.price ? '价格在' : ''}${kd.price}${kd.brand}${kd.channel}${kd.color}${kd.size}${kd.sort}|YOHO!BUY有货`, // eslint-disable-line
        keywords: `${kd.size}${kd.color}${kd.brand}${kd.channel}${kd.sort}${kd.style ? ',' : ''}${kd.style}` || defaultTdk.keywords, // eslint-disable-line
        description: `全球购潮流网购${kd.brand}就上YOHO!BUY有货,有货网销售正品新款${kd.size}${kd.color}${kd.brand}${kd.channel}${kd.sort},全场低至4,100%品牌正品保证,支持货到付款。` // eslint-disable-line
    };
};

const getShopHomeSeo = (shopInfo) => {
    let params = {
        nameEn: '',
        name: ''
    };

    if (shopInfo) {
        Object.assign(params, {
            nameEn: _.get(shopInfo, 'brandNameEn', ''),
            nameCn: _.get(shopInfo, 'brandNameCn', '')
        });

        params.name = `${params.nameEn} ${params.nameCn}`;

        if (shopInfo.shopName) {
            Object.assign(params, {
                nameEn: shopInfo.shopName,
                name: shopInfo.shopName
            });
        }
    }

    return {
        title: `${params.nameEn} | ${params.name}品牌旗舰店 | 正品保证, YOHO!BUY 有货`,
        keywords: `${params.nameEn}, ${params.name}品牌旗舰店,${params.name}正品`,
        description: `有货网${params.nameEn}旗舰店销售${params.name}正品商品,100%质量保证,支持货到付款,想了解${params.name}价格、图片、评价等信息,就上YOHO!BUY 有货中国大型潮流商品购物网站!` // eslint-disable-line
    };
};

const getShopListSeo = (sorts, checked, shopInfo) => {
    const kd = getSeoCheckedParamsName(sorts, checked);
    const defaultTdk = getShopHomeSeo(shopInfo);

    // 无筛选参数时返回空对象
    if (_.isEmpty(kd)) {
        return defaultTdk;
    }

    _.forEach(['sort', 'brand', 'color', 'size', 'price', 'channel', 'style', 'senior'], val => {
        kd[val] = kd[val] || '';
    });

    let shopName = shopInfo.shopName || `${shopInfo.brandNameEn} ${shopInfo.brandNameCn}`;

    shopName += shopName ? '品牌旗舰店' : '';

    return {
        title: `${shopName}|${kd.price ? '价格在' : ''}${kd.price}${kd.brand}${kd.channel}${kd.color}${kd.size}${kd.sort}|YOHO!BUY有货`, // eslint-disable-line
        keywords: `${kd.size}${kd.color}${kd.brand}${kd.channel}${kd.sort}${kd.style ? ',' : ''}${kd.style}` || defaultTdk.keywords, // eslint-disable-line
        description: `潮流网购${kd.brand}就上YOHO!BUY有货${shopName},有货网销售正品新款${kd.size}${kd.color}${kd.brand}${kd.channel}${kd.sort},全场低至4,100%品牌正品保证,支持货到付款。` // eslint-disable-line
    };
};

module.exports = {
    getSeoCheckedParamsName,
    getSaleListSeo,
    getGlobalListSeo,
    getShopHomeSeo,
    getShopListSeo,
};