-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgoods.js
237 lines (229 loc) · 5.46 KB
/
goods.js
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// 引入 models
const models = require('../models');
const code = require('../lib/code');
const Sequelize = require('sequelize');
const Op = Sequelize.Op;
detailAction = async (request) => {
try{
// 商品信息
const info = await models.nideshop_goods.findAndCountAll({
where: {
id: request.query.goodsId
},
limit: request.query.limit,
offset: (request.query.page - 1) * request.query.limit,
});
// 商品相关图片
const gallery = await models.nideshop_goods_gallery.findAndCountAll({
where: {
goods_id: request.query.goodsIdImg
},
limit: 4,
});
/**
* 相关属性
* 关联查询两个表 leftJoin
*/
const attribute = await models.nideshop_goods_attribute.findAndCountAll({
where: {
goods_id: request.query.goodsIdAttr
},
attributes: [
'value',
'goods_id'
],
include: {
model: models.nideshop_attribute,
attributes: [
'name',
],
},
limit: request.query.limit,
offset: (request.query.page - 1) * request.query.limit,
});
/**
* 常见问题
*/
const issue = await models.nideshop_goods_issue.findAndCountAll({
limit: request.query.limit,
offset: (request.query.page - 1) * request.query.limit,
});
/**
* 品牌
*/
brand = await models.nideshop_brand.findAndCountAll({
where: {
id: request.query.brandId
},
});
/**
* 热门评论 评论条数
*/
const commentCount = await models.nideshop_comment.findAndCountAll({
where: {
value_id: request.query.comment,
type_id: 0
},
limit: request.query.limit,
offset: (request.query.page - 1) * request.query.limit,
});
/**
* 大家都在看
*/
const productList = await models.nideshop_goods.findAndCountAll({
where: {
category_id: request.query.productLists
},
limit: request.query.limit,
offset: (request.query.page - 1) * request.query.limit,
});
/**
* 判断是否收藏过
*/
const iscollect = await models.nideshop_collect.findAndCountAll({
where: {
"user_id": request.query.openIdUser,
"value_id": request.query.goodsIdValue
},
});
let collected = false;
if (iscollect.length > 0) {
collected = true
};
/**
* 判断该用户是否在购物车有此商品
*/
const oldNumber = await models.nideshop_cart.findAndCountAll({
where: {
user_id: request.query.openIdUserName
},
})
let allnumber = 0;
if (oldNumber.length > 0) {
for (let i = 0; i < oldNumber.length; i++) {
const element = oldNumber[i];
allnumber += element.number
}
}
return {
results: {
info,
gallery,
attribute,
issue,
brand,
commentCount,
productList,
iscollect,
oldNumber,
},
dataBaseError: false
}
}
catch(e){
return { results: e, dataBaseError: true}
}
};
goodsList = async (request) => {
try{
const categoryId = request.query.categoryId;
const isNew = request.query.isNew;
const isHot = request.query.isHot;
let order = request.query.order;
let goodsList = [];
if (categoryId) {
// 获得商品列表
goodsList = await models.nideshop_goods.findAndCountAll({
where: {
"category_id": categoryId
},
});
// 获得当前分类
const currentNav = await models.nideshop_category.findAndCountAll({
where: {
"id": categoryId
}
});
// 如果goodslist没有可能这个分类是主分类 例如: 居家,厨具
if (goodsList.length == 0) {
// 找到与之相关的子类, 再找到与子类相关的商品列表
let subIds = await models.nideshop_category.findAndCountAll({
where: {
"parent_id": categoryId
},
attributes: [
'id',
],
});
// 需要变成数组形式, childCategoryIds [1020000,1036002]
if (subIds.length != 0) {
subIds = subIds.map((item) => {
return item.id;
});
};
goodsList = await models.nideshop_goods.findAndCountAll({
where: {
category_id: {
[Op.in]: subIds
},
},
limit: 50,
});
};
return {
results: {
data: goodsList,
currentNav: currentNav[0]
},
dataBaseError: false
}
};
if (!order) {
order = '';
orderBy = "id"
} else {
orderBy = "retail_price"
};
/**
* 新品列表
*/
if (isNew) {
goodsList = await models.nideshop_goods.findAndCountAll({
where: {
is_new: isNew
},
order: [orderBy, order],
});
return {
results: {
data: goodsList,
},
dataBaseError: false
}
};
/**
* 热门商品
*/
if (isHot) {
goodsList = await models.nideshop_goods.findAndCountAll({
where: {
is_hot: isHot
},
order: [orderBy, order],
});
return {
results: {
data: goodsList,
},
dataBaseError: false
}
}
}
catch(e) {
return { results: e, dataBaseError: true}
}
}
module.exports = {
detailAction,
goodsList
}