-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcart.js
111 lines (108 loc) · 2.32 KB
/
cart.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
// 引入 models
const models = require('../models');
const code = require('../lib/code');
const Sequelize = require('sequelize');
const Op = Sequelize.Op;
addCart = async (request) => {
try{
// const { number, goodsId, openId } = request.payload;
// 判断购物车是否包含此数据
const haveGoods = await models.nideshop_cart.findAndCountAll({
where: {
user_id: request.payload.openId,
goods_id: request.payload.goodsId
}
});
console.log(haveGoods);
if (haveGoods.length == 0) {
const goods = await models.nideshop_goods.findAndCountAll({
where: {
id: request.payload.goodsId
},
});
const { retail_price, name, list_pic_url } = goods[0];
console.log(goods[0]);
// 如果不存在
await models.nideshop_cart.create({
user_id: openId,
goods_id: goodsId,
number,
goods_name: name,
list_pic_url,
retail_price
});
} else {
// 如果存在
const oldNumber = await models.nideshop_cart.findAndCountAll({
where: {
user_id: openId,
goods_id: goodsId
},
attributes: ['number']
});
console.log(oldNumber);
// 更新数据
await models.nideshop_cart.update({
where: {
number: oldNumber[0].number + number
}
});
}
return {
results: {
data: "success"
},
dataBaseError: false
}
}
catch(e) {
return { results: e, dataBaseError: true}
}
}
cartList = async (request) => {
try{
// const { openId } = request.query;
const cartList = await models.nideshop_cart.findAndCountAll({
where: {
user_id: request.query.openId,
}
});
return {
results: {
data: cartList,
},
dataBaseError: false
}
}
catch(e) {
return { results: e, dataBaseError: true}
}
}
deleteAction = async (parms) => {
const data = await models.nideshop_cart.destroy({
where: {
id: parms.id,
}
});
console.log(data);
if (data) {
return {
results: {
data: true
},
dataBaseError: false
}
} else {
return {
results: {
data: false
},
dataBaseError: false
}
}
}
module.exports = {
addCart,
cartList,
deleteAction
}