guang-helper.js 1.74 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');
};

module.exports = {
    getGenderByCookie,
    getUdid,
    getArticleUrl
};