opt.js 3.42 KB
/**
 * 逛操作
 * @author: chenfeng<feng.chen@yoho.cn>
 * @date: 2016/09/06
 */
'use strict';

const mRoot = '../models';
const optModel = require(`${mRoot}/opt`);
const brandModel = require(`${mRoot}/brand`);
const helpers = global.yoho.helpers;
const stringProcess = require(`${global.utils}/string-process`);
const _ = require('lodash');

/**
 * [资讯文章点赞 (H5里显示点赞)]
 */
const praiseArticle = (req, res, next) => {
    /* 判断是不是AJAX请求 */
    if (!req.xhr) {
        res.json({ code: 400, message: '非法请求', data: '' });
        return;
    }

    /* 判断参数是否有效 */
    let id = req.body.id,
        opt = req.body.opt || 'ok',
        udid = req.cookies.udid || require('yoho-md5')(req.ip) || 'yoho';

    if (!stringProcess.isNumeric(id)) {
        res.json({ code: 400, message: '非法请求', data: '' });
        return;
    }

    /* 执行点赞或取消操作 */
    return req.ctx(optModel).praiseArticle(udid, id, opt).then((data) => {
        res.json(data);
    }).catch(next);
};

/**
 * 资讯文章收藏 (APP里显示收藏)
 */
const collectArticle = (req, res, next) => {
    let allowOrigin = _.get(req, 'headers.origin', null) ?
        req.headers.origin : req.protocol + '://' + req.headers.host;

    res.setHeader('Access-Control-Allow-Origin', allowOrigin);
    res.setHeader('Access-Control-Allow-Credentials', 'true');

    let result = {
        code: 400,
        message: '您未登录,无法收藏或者取消收藏。请先登录!',
        data: ''
    };


    /* 判断参数是否有效 */
    /* 判断用户是否登录 */
    let id = req.body.id,
        opt = req.body.opt || 'ok',
        uid = req.user.uid;

    /* 判断是不是登录成功 */
    if (!uid) {
        res.json(result);
        return;
    }

    if (!stringProcess.isNumeric(id) || !stringProcess.isNumeric(uid)) {
        res.json(result);
        return;
    }

    /* 执行收藏或取消操作 */
    return req.ctx(optModel).collectArticle(uid, id, opt).then(data => {
        if (!data) {
            res.json({
                code: 400,
                message: '操作失败',
                data: ''
            });
            return;
        }
        res.json({
            code: 200,
            message: '成功',
            data: ''
        });
    }).catch(next);
};

/**
 * [品牌收藏]
 */
const favoriteBrand = (req, res, next) => {
    let refer = helpers.urlFormat('/signin.html', {
        refer: req.headers.referer
    });
    let result = {
        code: 400,
        message: '未登录',
        data: refer
    };

    /* 判断是不是AJAX请求 */
    if (!req.xhr) {
        res.json(result);
        return;
    }

    /* 判断参数是否有效 */
    let id = req.body.id,
        opt = req.body.opt || 'ok',
        uid = req.user.uid,
        isBrand = req.body.isBrand;

    if (!stringProcess.isNumeric(id) || !stringProcess.isNumeric(uid)) {
        res.json(result);
        return;
    }

    // 执行收藏或取消操作
    return req.ctx(brandModel).favoriteBrand(uid, id, opt, isBrand).then(data => {
        if (!data) {
            res.json({
                code: 400,
                message: '操作失败'
            });
            return;
        }
        res.json({
            code: 200,
            message: data.message,
            data: ''
        });
    }).catch(next);
};


module.exports = {
    praiseArticle,
    collectArticle,
    favoriteBrand
};