recommend.js
2.24 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
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* 推荐商品(店铺推荐|为您优选|最近浏览)controller
* @author: wsl<shuiling.wang@yoho.cn>
* @date: 2016/11/21
*/
'use strict';
const recommend = require('../models/recommend');
const _ = require('lodash');
const md5 = require('md5');
const requestIp = require('request-ip');
const _channel = (channel) => {
let yhChannel = 1;
switch (channel) {
case 'men':
yhChannel = 1;
break;
case 'women':
yhChannel = 2;
break;
case 'lifestyle':
yhChannel = 4;
break;
default:
break;
}
return yhChannel;
};
/**
* 获取客户端唯一标识
* @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;
};
/**
* 为您优选
* @param req
* @param res
* @param next
*/
const getRecommendProduct = (req, res, next) => {
let params = {
yh_channel: _channel(req.query.channel || req.cookies._Channel || 'men'),
udid: getUdid(req, res)
};
if (req.user.uid) {
params.uid = req.user.uid;
}
recommend.getRecommendProduct(params).then((result) => {
res.json(result);
}).catch(next);
};
/**
* 店铺推荐
* @param req
* @param res
* @param next
*/
const getRecommendShop = (req, res, next) => {
recommend.getRecommendShop(req.query.productSkn).then((result) => {
res.json(result);
}).catch(next);
};
/**
* 最近浏览
* @param req
* @param res
* @param next
*/
const recentPreview = (req, res, next) => {
let browserSkn = req.cookies._browseskn || '';
let limit = req.query.limit || 20;
let skn = _.trim(browserSkn ? decodeURIComponent(browserSkn).replace(/\-(\d)+(\,){0,1}/g, ',') : '', ',');
if (skn) {
skn = _.slice(_.uniq(skn.split(',')), 0, limit).join(','); // 去重+截取
recommend.recentPreview(skn, limit).then(data => {
res.json(data);
}).catch(next);
} else {
res.json(false);
}
};
module.exports = {
getRecommendProduct,
getRecommendShop,
recentPreview
};