set-yoho-data.js
871 Bytes
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
/**
* 设置 YOHO 数据
* @author: 赵彪<bill.zhao@yoho.cn>
* @date: 2016/6/16
*/
'use strict';
const _ = require('lodash');
/**
* 获取 IP
* @param {*} req
*/
const _getClientIp = req => {
let remoteIp = req.get('X-Forwarded-For') || req.get('X-Real-IP') || req.ip;
if (remoteIp.indexOf(',') > 0) {
let arr = remoteIp.split(',');
remoteIp = _.trim(arr[arr.length - 1]);
}
if (_.startsWith(remoteIp, '10.66.')) {
remoteIp = req.get('X-Real-IP');
}
return _.trim(remoteIp);
};
module.exports = () => {
return (req, res, next) => {
let yoho = {
pageChannel: {}
};
// IP 地址
yoho.clientIp = _getClientIp(req);
Object.assign(res.locals, yoho);
Object.assign(req.yoho, yoho);
res.locals.showHeader = true;
next();
};
};