Favorite.js
3.68 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
146
147
148
149
150
151
152
/**
* 我的优惠券 controller
* @author: weiqingting<qingting.wei@yoho.cn>
* @date: 2016/05/16
*/
'use strict';
const Promise = require('bluebird');
const co = Promise.coroutine;
const requestIp = require('request-ip');
const md5 = require('md5');
const favoriteService = require('../models/favorite-service');
const TABS = {
product: 'product',
brand: 'brand',
article: 'article'
};
const udidRequired = (req, res, next) => {
let udid = req.cookies.udid;
if (!udid) {
udid = md5(req.ip || requestIp.getClientIp(req));
if (res && res.cookie) {
res.cookie('udid', udid);
}
req.user.udid = udid;
}
next();
};
const index = (req, res, next)=> {
let uid = req.user.uid;
let udid = req.user.udid;
let page = +req.query.page || 1;
let type = req.query.type || TABS.product;
let selectedSort = +req.query.sort_id || 0;
let reduction = req.query.is_reduction || 'N';
let promotion = req.query.is_promotion || 'N';
let limit = +req.query.limit || 10;
co(function*() {
let data = {};
data.tabs = favoriteService.getFavoriteTabs(type);
switch (type) {
case TABS.brand:
data.favBrands = yield favoriteService.favoriteBrandListAsync(uid, page, limit, type);
break;
case TABS.article:
data.favArticles = yield favoriteService.favoriteArticleListAsync(uid, udid, page, limit, type);
break;
default:
data.favProducts = yield favoriteService.favoriteProductListAsync(
uid, page, limit, selectedSort, 'N', reduction, promotion, req.query
);
break;
}
return data;
})().then((result) => {
return res.render('favorite', {
meFavoritePage: true,
meFavorite: result
});
}).catch(next);
};
const newProduct = (req, res, next) => {
let uid = req.user.uid;
let page = +req.query.page || 1;
let limit = +req.query.limit || 10;
let id = +req.query.id || 0;
if (!id) {
return res.json({
code: 400,
message: '缺少参数'
});
}
favoriteService.newProductAsync(uid, page, limit, id).then((result) => {
return res.json({
code: 200,
data: result
});
}).catch(next);
};
const reduction = (req, res, next) => {
let uid = req.user.uid;
let page = +req.query.page || 1;
let type = req.query.type || '';
let limit = 10;
favoriteService.reductionAsync(uid, page, limit, type, 0, 'Y').then((result) => {
return res.render('home/favorite/reduction', result);
}).catch(next);
};
const notice = (req, res, next) => {
let uid = req.user.uid;
let id = req.query.id;
let mobile = req.query.mobile;
favoriteService.enableNoticeAsync(uid, mobile, id).then((result) => {
return res.json(result);
}).catch(next);
};
const cancelNotice = (req, res, next) => {
let uid = req.user.uid;
let id = req.query.id;
favoriteService.disableNoticeAsync(uid, id).then((result) => {
return res.json(result);
}).catch(next);
};
const cancel = (req, res, next) => {
let uid = req.user.uid;
let id = req.query.id;
let type = req.query.type || 'product';
if (!uid || !id) {
return res.json({
code: 400,
message: '缺少参数'
});
}
favoriteService.cancelAsync(uid, id, type).then((result) => {
return res.json(result);
}).catch(next);
};
module.exports = {
index,
newProduct,
reduction,
notice,
cancelNotice,
cancel,
udidRequired
};