opt.js 3.25 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 crypto = global.yoho.crypto;
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.sessionID || require('md5')(req.ip) || 'yoho';

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

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

/**
 * 资讯文章收藏 (APP里显示收藏)
 */
const collectArticle = (req, res, next) => {
    let result = {
        code: 400,
        message: '您未登录,无法收藏或者取消收藏。请先登录!',
        data: ''
    };

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

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

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

    /* 执行收藏或取消操作 */
    return 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 || (req.body.uid && parseInt(crypto.decrypt(null, req.body.uid))) || req.cookies.appUid,
        isBrand = req.body.isBrand;

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

    // 执行收藏或取消操作
    return 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
};