Blame view

apps/guang/controllers/opt.js 3.42 KB
1 2 3 4 5 6 7 8 9 10 11 12
/**
 * 逛操作
 * @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`);
13
const _ = require('lodash');
14 15 16 17

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

    /* 判断参数是否有效 */
    let id = req.body.id,
陈峰 authored
27
        opt = req.body.opt || 'ok',
郭成尧 authored
28
        udid = req.cookies.udid || require('yoho-md5')(req.ip) || 'yoho';
陈峰 authored
29
30 31 32 33 34 35
    if (!stringProcess.isNumeric(id)) {
        res.json({ code: 400, message: '非法请求', data: '' });
        return;
    }

    /* 执行点赞或取消操作 */
zhangxiaoru authored
36
    return req.ctx(optModel).praiseArticle(udid, id, opt).then((data) => {
陈峰 authored
37
        res.json(data);
38 39 40 41 42 43
    }).catch(next);
};

/**
 * 资讯文章收藏 (APP里显示收藏)
 */
44
const collectArticle = (req, res, next) => {
45 46 47 48 49 50
    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');
51 52 53 54
    let result = {
        code: 400,
        message: '您未登录,无法收藏或者取消收藏。请先登录!',
        data: ''
陈峰 authored
55
    };
56 57 58 59 60


    /* 判断参数是否有效 */
    /* 判断用户是否登录 */
    let id = req.body.id,
陈峰 authored
61
        opt = req.body.opt || 'ok',
郭成尧 authored
62
        uid = req.user.uid;
陈峰 authored
63
64 65 66 67 68 69
    /* 判断是不是登录成功 */
    if (!uid) {
        res.json(result);
        return;
    }
70 71 72 73 74 75
    if (!stringProcess.isNumeric(id) || !stringProcess.isNumeric(uid)) {
        res.json(result);
        return;
    }

    /* 执行收藏或取消操作 */
zhangxiaoru authored
76
    return req.ctx(optModel).collectArticle(uid, id, opt).then(data => {
77 78 79 80 81
        if (!data) {
            res.json({
                code: 400,
                message: '操作失败',
                data: ''
陈峰 authored
82
            });
83
            return;
陈峰 authored
84
        }
85
        res.json({
陈峰 authored
86 87 88 89 90
            code: 200,
            message: '成功',
            data: ''
        });
    }).catch(next);
91 92 93 94 95
};

/**
 * [品牌收藏]
 */
96
const favoriteBrand = (req, res, next) => {
97 98
    let refer = helpers.urlFormat('/signin.html', {
        refer: req.headers.referer
陈峰 authored
99
    });
100 101 102
    let result = {
        code: 400,
        message: '未登录',
陈峰 authored
103 104
        data: refer
    };
105 106 107 108 109 110 111 112 113

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

    /* 判断参数是否有效 */
    let id = req.body.id,
陈峰 authored
114
        opt = req.body.opt || 'ok',
郭成尧 authored
115
        uid = req.user.uid,
zhangxiaoru authored
116
        isBrand = req.body.isBrand;
陈峰 authored
117
118 119 120 121 122 123
    if (!stringProcess.isNumeric(id) || !stringProcess.isNumeric(uid)) {
        res.json(result);
        return;
    }

    // 执行收藏或取消操作
zhangxiaoru authored
124
    return req.ctx(brandModel).favoriteBrand(uid, id, opt, isBrand).then(data => {
125 126 127
        if (!data) {
            res.json({
                code: 400,
128
                message: '操作失败'
陈峰 authored
129
            });
130
            return;
陈峰 authored
131
        }
132
        res.json({
陈峰 authored
133
            code: 200,
zhangxiaoru authored
134
            message: data.message,
陈峰 authored
135 136 137
            data: ''
        });
    }).catch(next);
138 139 140 141
};


module.exports = {
142 143 144
    praiseArticle,
    collectArticle,
    favoriteBrand
145
};