set-yoho-data.js
1.84 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
/**
* 设置 YOHO 数据
* @author: 赵彪<bill.zhao@yoho.cn>
* @date: 2016/6/16
*/
'use strict';
const _ = require('lodash');
module.exports = () => {
return (req, res, next) => {
const channelMap = {
1: 'men',
2: 'women'
};
const indexMap = {
men: 0,
women: 1
};
let yoho = {
pageChannel: {},
yohoTitle: 'BLK'
};
const channel = channelMap[req.query.app_channel] || req.query.channel || req.cookies._Channel || 'men';
// 设置频道
res.cookie('_Channel', channel, {path: '/'});
res.cookie('_ChannelIndex', indexMap[channel], {path: '/'});
// 用于头部颜色控制
yoho.pageChannel[channel] = true;
// 当前频道设置
yoho.channel = channel;
// 判断请求是否来自app
let userAgent = req.get('User-Agent');
yoho.isApp = /yh_blk/i.test(req.get('User-Agent')) || /YH_BLK/i.test(req.get('User-Agent'));
yoho.isiOS = /\(i[^;]+;( U;)? CPU.+Mac OS X/i.test(userAgent);
yoho.isAndroid = /Android/i.test(userAgent);
yoho.isYohoBuy = /YohoBuy/i.test(userAgent);
// client ip
yoho.clientIp = (function() {
let remoteIp = req.get('X-Yoho-Real-IP') || req.get('X-Forwarded-For') || req.get('X-Real-IP') || req.ip || ''; // eslint-disable-line
if (remoteIp.indexOf(',') > 0) {
let arr = remoteIp.split(',');
remoteIp = _.trim(arr[0]);
}
if (_.startsWith(remoteIp, '10.66.')) {
remoteIp = req.get('X-Real-IP');
}
remoteIp = _.trim(remoteIp);
return remoteIp;
}());
Object.assign(res.locals, yoho);
Object.assign(req.yoho, yoho);
next();
};
};