set-yoho-data.js
1.28 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
/**
* 设置 YOHO 数据
* @author: 赵彪<bill.zhao@yoho.cn>
* @date: 2016/6/16
*/
'use strict';
const requestIp = require('request-ip');
const md5 = require('md5');
const _getGender = (channel) => {
switch (channel) {
case 'boys':
return '1,3';
case 'girls':
return '2,3';
default:
return '1,2,3';
}
};
module.exports = () => {
return (req, res, next) => {
let yoho = {
pageChannel: {}
};
const channel = req.query.channel || req.cookies._Channel || 'boys';
// 用于头部颜色控制
yoho.pageChannel[channel] = true;
// 当前频道设置
yoho.channel = channel;
// 当前性别
yoho.gender = _getGender(channel);
yoho.isApp = req.query.app_version || req.query.appVersion;
// uuid
yoho.udid = (function() {
let udid = req.cookies.udid;
if (!udid) {
udid = md5(req.ip || requestIp.getClientIp(req));
if (res && res.cookie) {
res.cookie('udid', udid);
}
}
return udid;
}());
Object.assign(res.locals, yoho);
Object.assign(req.yoho, yoho);
next();
};
};