recent-view.js
932 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
/**
* 最近浏览controller
* @author: xuqi<qi.xu@yoho.cn>
* @date: 2016/10/11
*/
'use strict';
const rvModel = require('../models/recent-view');
const _ = require('lodash');
const index = (req, res, next) => {
let limit = req.query.limit;
let browserSkn = decodeURIComponent(req.cookies._browseskn);
// 拆解skn
let skn = browserSkn ? browserSkn.replace(/\-(\d)+(\,){0,1}/g, ',') : '';
// 去除skn字符串的最后一个多余的,
if (skn && skn.lastIndexOf(',') === skn.length - 1) {
skn = skn.slice(0, -1);
}
if (!skn) {
res.jsonp({
code: 200,
data: [],
message: 'User info'
});
} else {
skn = _.slice(_.uniq(skn.split(',')), 0, limit).join(','); // 去重+截取
rvModel.index(skn, limit).then(data => {
res.jsonp(data);
}).catch(next);
}
};
module.exports = {
index
};