opt.js
3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* 逛操作
* @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.sessionID || require('yoho-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 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.body.uid;
/* 判断是不是登录成功 */
if (!uid) {
res.json(result);
return;
}
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(req.body.uid, 10)) || 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
};