guang-helper.js 3 KB
/**
 * 逛 model
 * @author: lcy<chuanyang.liu@yoho.cn>
 * @date: 2016/09/06
 */

'use strict';

const md5 = require('md5');
const requestIp = require('request-ip');
const _ = require('lodash');
const helpers = global.yoho.helpers;

/* COOKIE标识访问的是男生频道 */
// const COOKIE_NAME_BOYS = 'boys';
// const COOKIE_DOMAIN = '.yohobuy.com';

const getGenderByCookie = (req) => {
    let gender = null;
    let channel = req.yoho.channel;

    switch (channel) {
        case 'boys':
            {
                gender = '1,3';
                break;
            }
        case 'girls':
            {
                gender = '2,3';
                break;
            }
        default:
            {
                gender = '1,2,3';
            }
    }

    return gender;
};

/**
 * 获取客户端唯一标识
 *
 * @return string
 */
const getUdid = (req, res) => {
    let udid = req.cookies.udid;

    if (!udid) {
        udid = md5(req.ip || requestIp.getClientIp(req));

        if (res && res.cookie) {
            res.cookie('udid', udid);
        }
    }

    return udid;
};

/**
  * 获得文章 url
  * @param type string url 后台返回的url
  * @param type id 产品编号
  * @return type url
  */
const getArticleUrl = (url, id, channel) => {

   /* 格式由url:{url:'aaa'} 更改为 url: 'aaa'
    try {
        let urlObj = JSON.parse(url);

        if (urlObj && urlObj['url']) {
            return urlObj['url'];
        }
    } catch (e) {}*/

    if (_.isString(url) && _.trim(url) !== '') {
        return url;
    }

    let param = {};

    if (channel) {
        param.channel = channel;
    }

    return helpers.urlFormat(`/${id}.html`, param, 'guang');
};

const getIndexSeo = (params, tabs) => {
    let tdk = {
        title: '逛|逛潮流,逛购物,官方授权正品潮流购物中心|YOHO!BUY有货',
        keywords: '逛,逛潮流,逛购物',
        description: 'YOHO!BUY有货逛频道,来YOHO!玩潮流!潮搭大解析!年轻人潮流购物中心,中国潮流购物风向标,吴亦凡重磅代言!YOHO!BUY有货100%正品保证,支持货到付款。'
    };

    params = params || {};

    if (params.hasOwnProperty('type')) {
        let tab = _.find(tabs, {isActive: true});

        if (tab && tab.type) {
            Object.assign(tdk, {
                title: `潮流${tab.type}-|YOHO!BUY有货`,
                keywords: `潮流${tab.type}`,
                description: `YOHO!BUY有货潮流${tab.type}!`
            });
        }
    }

    return tdk;
};

const getEditorSeo = (name) => {
    return {
        title: `潮流编辑${name}|YOHO!BUY有货`,
        keywords: `潮流编辑${name}`,
        description: `YOHO!BUY有货潮流编辑${name}!`
    };
};

const getTagSeo = (tagName) => {
    return {
        title: `${tagName}|YOHO!BUY有货`,
        keywords: `${tagName}`,
        description: `YOHO!BUY有货潮流${tagName}!`
    };
};

module.exports = {
    getGenderByCookie,
    getUdid,
    getArticleUrl,
    getIndexSeo,
    getEditorSeo,
    getTagSeo
};