guang-helper.js
1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* 逛 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
};