recent-view.js 932 Bytes
/**
 * 最近浏览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 = req.cookies._browseskn;

    // 拆解skn
    let skn = browserSkn ? decodeURIComponent(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
};