Skip to content

Commit 0bf3512

Browse files
committed
获取商品数据
1 parent d82be7d commit 0bf3512

File tree

4 files changed

+247
-6
lines changed

4 files changed

+247
-6
lines changed

db/goods.js

+112-3
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,15 @@ detailAction = async (request) => {
9999
where: {
100100
user_id: request.query.openIdUserName
101101
},
102-
// attributes: [
103-
// 'number'
104-
// ],
105102
})
103+
let allnumber = 0;
104+
105+
if (oldNumber.length > 0) {
106+
for (let i = 0; i < oldNumber.length; i++) {
107+
const element = oldNumber[i];
108+
allnumber += element.number
109+
}
110+
}
106111
return {
107112
results: {
108113
info,
@@ -121,8 +126,112 @@ detailAction = async (request) => {
121126
catch(e){
122127
return { results: e, dataBaseError: true}
123128
}
129+
};
130+
131+
goodsList = async (request) => {
132+
try{
133+
const categoryId = request.query.categoryId;
134+
const isNew = request.query.isNew;
135+
const isHot = request.query.isHot;
136+
let order = request.query.order;
137+
let goodsList = [];
138+
139+
if (categoryId) {
140+
// 获得商品列表
141+
goodsList = await models.nideshop_goods.findAndCountAll({
142+
where: {
143+
"category_id": categoryId
144+
},
145+
});
146+
// 获得当前分类
147+
const currentNav = await models.nideshop_category.findAndCountAll({
148+
where: {
149+
"id": categoryId
150+
}
151+
});
152+
153+
// 如果goodslist没有可能这个分类是主分类 例如: 居家,厨具
154+
if (goodsList.length == 0) {
155+
// 找到与之相关的子类, 再找到与子类相关的商品列表
156+
let subIds = await models.nideshop_category.findAndCountAll({
157+
where: {
158+
"parent_id": categoryId
159+
},
160+
attributes: [
161+
'id',
162+
],
163+
});
164+
// 需要变成数组形式, childCategoryIds [1020000,1036002]
165+
if (subIds.length != 0) {
166+
subIds = subIds.map((item) => {
167+
return item.id;
168+
});
169+
};
170+
goodsList = await models.nideshop_goods.findAndCountAll({
171+
where: {
172+
category_id: {
173+
[Op.in]: subIds
174+
},
175+
},
176+
limit: 50,
177+
});
178+
};
179+
return {
180+
results: {
181+
data: goodsList,
182+
currentNav: currentNav[0]
183+
},
184+
dataBaseError: false
185+
}
186+
};
187+
188+
if (!order) {
189+
order = '';
190+
orderBy = "id"
191+
} else {
192+
orderBy = "retail_price"
193+
};
194+
/**
195+
* 新品列表
196+
*/
197+
if (isNew) {
198+
goodsList = await models.nideshop_goods.findAndCountAll({
199+
where: {
200+
is_new: isNew
201+
},
202+
order: [orderBy, order],
203+
});
204+
return {
205+
results: {
206+
data: goodsList,
207+
},
208+
dataBaseError: false
209+
}
210+
};
211+
/**
212+
* 热门商品
213+
*/
214+
if (isHot) {
215+
goodsList = await models.nideshop_goods.findAndCountAll({
216+
where: {
217+
is_hot: isHot
218+
},
219+
order: [orderBy, order],
220+
});
221+
return {
222+
results: {
223+
data: goodsList,
224+
},
225+
dataBaseError: false
226+
}
227+
}
228+
}
229+
catch(e) {
230+
return { results: e, dataBaseError: true}
231+
}
124232
}
125233

126234
module.exports = {
127235
detailAction,
236+
goodsList
128237
}

plugins/hapi-pagination.js

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ const pageOptions = {
7777
'/category/currentAction',
7878
'/category/categoryNav',
7979
'/goods/detailaction',
80+
'/goods/goodsList',
8081
],
8182
exclude: [],
8283
},

routes/goods.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,23 @@ module.exports = [
3333
},
3434
{
3535
method: 'GET',
36-
path: `/${GROUP_NAME}/{categoryId}/goodsList`,
36+
path: `/${GROUP_NAME}/goodsList`,
3737
handler: async (request, reply) => {
38-
reply();
38+
let res = await controllers.goods.goodsList(request);
39+
middleware.dbErrorMiddleware(request, res, reply);
3940
},
4041
config: {
4142
tags: ['api', GROUP_NAME],
42-
description: '获取商品列表'
43+
description: '获取商品列表',
44+
validate: {
45+
query: {
46+
...paginationDefine,
47+
categoryId: Joi.number().description('获得商品类别Id'),
48+
isNew: Joi.boolean().description('是否有新品列表'),
49+
order: Joi.number().description('订单ID'),
50+
isHot: Joi.boolean().description('最热商品'),
51+
}
52+
}
4353
}
4454
}
4555
]

0 commit comments

Comments
 (0)