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

const _ = require('lodash');

const NOW_YEAR = new Date().getFullYear();

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 listDefaultTdk = {
    title: '【潮流商品】_潮流男装_潮流女装_潮流童装-YOHO!BUY 有货',
    keywords: '潮流男装,潮流女装,潮流童装',
    description: '潮流网购就上YOHO!BUY有货,有货网潮流商品专卖店提供潮流商品品牌、潮流商品价格、潮流商品图片等信息。销售正品新款潮流男装、潮流女装、潮流童装,全场低至4折,★全场品牌正品保证,支持货到付款!★' // eslint-disable-line
};

/**
 * 列表页seo
 * @param sorts
 * @param checked
 * @returns {{title: string, keywords: string, description: string}}
 */
const getListSeo = (sorts, checked, extend = {}) => {
    let {brand, channel, color, style, sort} = Object.assign(getSeoCheckedParamsName(sorts, checked), extend);

    if (brand && _.indexOf(brand, '、') > -1) {
        brand = '';
    }
    let name = _.compact([brand, channel, color, style, sort]).join('');

    if (!name) {
        return listDefaultTdk;
    }

    let desc = sort ? `${sort}:潮流网购就上YOHO!BUY有货,有货网${sort}专卖店提供${sort}品牌、${sort}价格、${sort}图片等信息。销售正品新款${sort},全场低至4,★全场品牌正品保证,支持货到付款!★` : listDefaultTdk.description; // eslint-disable-line

    if (name === sort) {
        return {
            title: `【${sort}_${sort}品牌_${sort}价格_${sort}图片-YOHO!BUY 有货`,
            keywords: `${sort},${sort}品牌,${sort}价格,${sort}图片`,
            description: desc
        };
    } else {
        return {
            title: `【${name}_价格,图片,款式-YOHO!BUY 有货`,
            keywords: name,
            description: desc
        };
    }
};

/**
 * 新品到页 seo
 * @param sorts
 * @param checked
 * @returns {{title: string, keywords: string, description: string}}
 */
const getNewListSeo = (sorts, checked) => {
    const tdk = {
        title: `【潮流新品】_引领潮流_${NOW_YEAR}时尚潮流新品-YOHO!BUY 有货`,
        keywords: `潮流新品.引领潮流,${NOW_YEAR}时尚潮流新品`,
        description: `【潮流新品】,${NOW_YEAR}时尚潮流新品,潮流网购就上YOHO!BUY有货,有货网销售正品新款,全场低至4,★全场品牌正品保证,支持货到付款!★`
    };

    const kd = getSeoCheckedParamsName(sorts, checked);
    let name = _.compact([kd.price ? `价格在${kd.price}` : '', kd.brand,
        kd.channel, kd.color, kd.size, kd.sort]).join('_');

    if (name) {
        tdk.title = `【潮流新品】_${name}-YOHO!BUY 有货`;
    }

    return tdk;
};

/**
 * sale列表页seo
 * @param sorts
 * @param checked
 * @param type
 * @returns {{title: string, keywords: string, description: string}}
 */
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
    };
};

/**
 * 全球购列表页seo
 * @param sorts
 * @param checked
 * @returns {{title: string, keywords: string, description: string}}
 */
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
    };
};


/**
 * 店铺首页seo
 * @param shopInfo
 * @returns {{title: string, keywords: string, description: string}}
 */
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
    };
};

/**
 * 店铺列表页seo
 * @param sorts
 * @param checked
 * @param shopInfo
 * @returns {{title: string, keywords: string, description: string}}
 */
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
    };
};

/**
 * 商品详情页seo
 * @param goodsInfo
 * @param navs
 * @returns {{title: string, keywords: string, description: string}}
 */
const getProductDetailSeo = (goodsInfo, navs) => {
    let brandName = _.get(goodsInfo, 'data.brand_info.brand_name', '');
    let sortName = _.get(navs, '[1].name', '');
    let productName = _.get(goodsInfo, 'data.product_name', '');
    let priceName = _.get(goodsInfo, 'data.format_sales_price');

    return {
        title: `${productName}_${brandName}${sortName}-YOHO!BUY 有货`,
        keywords: `${brandName}${sortName},${brandName}${sortName}价格,${brandName}${sortName}图片`,
        description: `${productName},有货网仅售${priceName}元,购买${brandName +
            sortName},了解${brandName + sortName}商品信息就上有货网!`
    };
};

const getHotKeywordsSeo = (keyword, num) => {
    return {
        title: `${keyword}_${keyword}【图片,新款,价格,折扣,搭配】-YOHO!BUY有货`,
        keywords: keyword,
        description: `YOHO!BUY 有货网是国内专业的${keyword}网上潮流购物商城,为您找到${num}${keyword}产品的品牌、图片、款式、价格、折扣等信息。买正品${keyword},就上YOHO!BUY有货!` // eslint-disable-line
    };
};

module.exports = {
    getSeoCheckedParamsName,
    getListSeo,
    getNewListSeo,
    getSaleListSeo,
    getGlobalListSeo,
    getShopHomeSeo,
    getShopListSeo,
    getProductDetailSeo,
    getHotKeywordsSeo
};