Files
ClassContent/项目合集/电商App/shop_backend/routes/car.js
2025-02-19 00:34:51 +08:00

280 lines
7.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const _ = require("../import");
/**
* @api {post} /car/addCar addCar
* @apiDescription 详情页添加商品到购物车
* @apiName addCar
* @apiGroup car
* @apiParam {number} waresid 需要被收藏的商品idwares_id字段
* @apiSuccessExample {json} 请求成功的返回:
* {
* "status" : 200,
* "message" : "成功添加购物车"
* "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
* }
* @apiErrorExample {json} 请求失败的返回
* {
* "status": 404,
* "message" : "不要重复收藏"
* "result": null
* }
* @apiSampleRequest http://127.0.0.1:9090/car/addCar
* @apiVersion 1.0.0
*/
_.router.post('/addCar', async function(req, res, next) {
var shopid = req.headers.data.waresid;
var IsAddShopCar = await _.db.query({
sql: _.config.sql.selectCarShop,
data: [ _.jwt.decode(req.headers.token).id, shopid]
});
if(IsAddShopCar.length != 0) {
res.json({
status: 404,
message: '不要添加购物车',
result: null
});
} else {
var data = await _.db.query({
sql: _.config.sql.addCar,
data: [ _.jwt.decode(req.headers.token).id, shopid, 1]
});
res.json({
status: 200,
message: '成功添加购物车',
result: data
});
}
});
/**
* @api {post} /car/getCarList getCarList
* @apiDescription 获取用户的购物车商品数据(不需要参数,传空对象 {} 即可
* @apiName getCarList
* @apiGroup car
* @apiSuccessExample {json} 请求成功的返回:
* {
* "status" : 200,
* "message" : "请求成功"
* "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
* }
* @apiErrorExample {json} 请求失败的返回
* {
* "status": 404,
* "message" : "购物车没有数据"
* "result": null
* }
* @apiSampleRequest http://127.0.0.1:9090/car/getCarList
* @apiVersion 1.0.0
*/
_.router.post('/getCarList', async function(req, res, next) {
var data = await _.db.query({
sql: _.config.sql.getCarList,
data: [ _.jwt.decode(req.headers.token).id]
});
if(data.length != 0) {
res.json({
status: 200,
message: '请求成功',
result: data
});
} else {
res.json({
status: 200,
message: '购物车没有数据',
result: {}
});
}
});
/**
* @api {post} /car/checkCar checkCar
* @apiDescription 检查商品是否已经被用户添加到购物车,已添加 result 为true未添加 result 为false
* @apiName checkCar
* @apiGroup car
* @apiParam {number} waresid 商品idwares_id字段
* @apiSuccessExample {json} 请求成功的返回:
* {
* "status" : 200,
* "message" : "商品已添加购物车"
* "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
* }
* @apiErrorExample {json} 请求失败的返回
* {
* "status": 200,
* "message" : "商品未添加购物车"
* "result": null
* }
* @apiSampleRequest http://127.0.0.1:9090/car/checkCar
* @apiVersion 1.0.0
*/
_.router.post('/checkCar', async function(req, res, next) {
var shopid = req.headers.data.waresid;
console.log("req.headers.data: ", req.headers.data);
var IsAddShopCar = await _.db.query({
sql: _.config.sql.selectCarShop,
data: [ _.jwt.decode(req.headers.token).id, shopid]
});
console.log("IsAddShopCar ==== :", IsAddShopCar);
if(IsAddShopCar.length != 0) {
res.json({
status: 200,
message: '商品已添加购物车',
result: {
isAddCar: true
}
});
} else {
res.json({
status: 200,
message: '商品未添加购物车',
result: {
isAddCar: false
}
});
}
});
/**
* @api {post} /car/deleteCar deleteCar
* @apiDescription 删除被添加到购物车的商品
* @apiName deleteCar
* @apiGroup car
* @apiParam {Array} carid 购物车商品id从getCarList接口中获得car_id即可。参数格式"[1,2,3]"
* @apiSuccessExample {json} 请求成功的返回:
* {
* "status" : 200,
* "message" : "删除成功"
* "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
* }
* @apiErrorExample {json} 请求失败的返回
* {
* "status": 200,
* "message" : "删除失败"
* "result": null
* }
* @apiSampleRequest http://127.0.0.1:9090/car/deleteCar
* @apiVersion 1.0.0
*/
_.router.post('/deleteCar', async function(req, res, next) {
var carid = req.headers.data.carid;
carid.forEach(async function(val) {
var deleteCar = await _.db.query({
sql: _.config.sql.deleteCarShop,
data: [ val ]
});
});
res.json({
status: 200,
message: '删除成功',
result: {}
});
});
/**
* @api {post} /car/updateCarNum updateCarNum
* @apiDescription 更新购物车需要购买的商品数量
* @apiName updateCarNum
* @apiGroup car
* @apiParam {number} carid 购物车商品id从getCarList接口中获得car_id即可
* @apiParam {number} quantity 购物车商品的数量
* @apiSuccessExample {json} 请求成功的返回:
* {
* "status" : 200,
* "message" : "更新成功"
* "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
* }
* @apiErrorExample {json} 请求失败的返回
* {
* "status": 200,
* "message" : "更新失败"
* "result": null
* }
* @apiSampleRequest http://127.0.0.1:9090/car/updateCarNum
* @apiVersion 1.0.0
*/
_.router.post('/updateCarNum', async function(req, res, next) {
var carid = req.headers.data.carid;
var quantity = req.headers.data.quantity;
var updateCarNum = await _.db.query({
sql: _.config.sql.updateCarNum,
data: [ quantity, carid ]
});
console.log(updateCarNum)
if(updateCarNum.affectedRows == 1) {
res.json({
status: 200,
message: '更新成功',
result: {}
});
} else {
res.json({
status: 404,
message: '更新失败',
result: {}
});
}
});
/**
* @api {post} /car/submitCar submitCar
* @apiDescription 更新购物车需要购买的商品数量,提交的数据是个对象,例如:"{"carList":[{"waresid":113,"goodsnum":2,"goodsprice":1000},{"waresid":114,"goodsnum":1,"goodsprice":1100}]}"
* @apiName submitCar
* @apiGroup car
* @apiParam {Object} carList 为对象,包含下面三个参数,需要前端自己构造数组
* @apiParam {string} waresid 商品的waresid
* @apiParam {string} goodsnum 购买的数量
* @apiParam {string} goodsprice 购买的单价
* @apiSuccessExample {json} 请求成功的返回:
* {
* "status" : 200,
* "message" : "订单增加成功"
* "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
* }
* @apiErrorExample {json} 请求失败的返回
* {
* "status": 200,
* "message" : "订单增加失败"
* "result": null
* }
* @apiSampleRequest http://127.0.0.1:9090/car/submitCar
* @apiVersion 1.0.0
*/
_.router.post('/submitCar', async function(req, res, next) {
//"{"carList":[{"waresid":113,"goodsnum":2,"goodsprice":1000},{"waresid":114,"goodsnum":1,"goodsprice":1100}]}"
var carList = JSON.parse(req.headers.data).carList;
carList.forEach(async function(val) {
val.user_id = _.jwt.decode(req.headers.token).id;
var addCarToOrder = await _.db.query({
sql: _.config.sql.addCarToOrder,
data: [ val.user_id,val.waresid,val.goodsnum,val.goodsprice,val.goodsnum * val.goodsprice]
});
});
res.json({
status: 200,
message: '订单增加成功',
result: {}
});
});
module.exports = _.router;