recent-view.js 1.29 KB
/**
 * 最近浏览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);
    }
};

/** 为你优选**/
const getRecommend = (req, res, next) => {
    let uid = req.user.uid;
    let udid = req.user.uid + req.yoho.udid;

    rvModel.recommend({
        yh_channel: req.yoho.channelNum,
        uid: uid,
        udid: udid,
        rec_pos: req.body.recPos,
        limit: req.body.limit
    }).then(d => {
        res.json(d);
    }).catch(next);
};

module.exports = {
    index,
    getRecommend
};