美文网首页
从购物车或者减少菜品的时候触发的接口(要考虑到满减活动)

从购物车或者减少菜品的时候触发的接口(要考虑到满减活动)

作者: 风中凌乱的男子 | 来源:发表于2020-12-12 16:05 被阅读0次
// 从购物车或者减少菜品的时候触发的接口
router.post('/select/food/delete/:id', (req, res) => {
  vertoken.getToken(req.headers.token).then(data => {
    const user_id = data._id;
    const food_id = req.params.id
    Food.find({
      _id: food_id
    }, (err, val) => {
      const shop_id = val[0].shop_id
      const food_id = val[0]._id
      const foodName = val[0].foodName
      const foodPrice = val[0].foodPrice
      const shopCard = {}
      shopCard.shop_id = shop_id;
      shopCard.user_id = user_id;
      shopCard.food_id = food_id;
      shopCard.foodName = foodName;
      shopCard.foodPrice = foodPrice;
      ShoppingCart.find({
        user_id: user_id,
        shop_id: shop_id,
        food_id: food_id
      }, (err, val) => {
        if (val.length == 0) {
          res.json({
            code: -1,
            message: '没有该菜品供你删除!'
          })
        } else {
          if (val[0].count == 1) {
            // 要删除这个菜
            ShoppingCart.findOneAndRemove({
              food_id: req.params.id
            }).then(val => {
              console.log(val);
              ShoppingCart.find({
                user_id: user_id,
                shop_id: shop_id
              }).lean().exec((err, ele) => {
                let obj = {
                  list: []
                }
                ele.forEach(item => {
                  item.totalPrice = item.foodPrice * item.count
                })
                const arr = []
                const arr2 = []
                ele.forEach(item => {
                  arr.push(item.totalPrice);
                  arr2.push(item.count);
                })
                const sum = (...arr) => [].concat(...arr).reduce((acc, val) => Number(acc) + Number(val), 0);
                obj.totalPrice = sum(arr)
                obj.count = sum(arr2)
                obj.list = ele
                Discount.find({
                  shop_id: shop_id,
                }).then(val => {
                  obj.totalPrice = manjian(obj.totalPrice, val)
                  res.json({
                    code: 200,
                    data: obj,
                    message: "计算成功"
                  })
                })
              })
            })

          } else {
            const shoppingCard = {}
            shoppingCard.count = --val[0].count;
            ShoppingCart.findOneAndUpdate({
              food_id: val[0].food_id
            }, {
              $set: shoppingCard
            }, {
              new: true
            }).then(value => {
              ShoppingCart.find({
                user_id: user_id,
                shop_id: shop_id
              }).lean().exec((err, ele) => {
                let obj = {
                  list: []
                }
                ele.forEach(item => {
                  item.totalPrice = item.foodPrice * item.count
                })
                const arr = []
                const arr2 = []
                ele.forEach(item => {
                  arr.push(item.totalPrice);
                  arr2.push(item.count);
                })
                const sum = (...arr) => [].concat(...arr).reduce((acc, val) => Number(acc) + Number(val), 0);
                obj.totalPrice = sum(arr)
                obj.count = sum(arr2)
                obj.list = ele
                Discount.find({
                  shop_id: shop_id,
                }).then(val => {
                  obj.totalPrice = manjian(obj.totalPrice, val)
                  res.json({
                    code: 200,
                    data: obj,
                    message: "计算成功"
                  })
                })
              })
            })
          }
        }

      })
    })
  }).catch(err => {
    res.json({
      code: 401,
      message: "token失效了"
    })
  })
})
// 清空购物车触发的接口
router.post('/clearShopCard', (req, res) => {
  vertoken.getToken(req.headers.token).then(data => {
    const user_id = data._id;
    const shop_id = req.body.shop_id;
    ShoppingCart.deleteMany({
      user_id: user_id,
      shop_id: shop_id,
    }, (err, val) => {
      res.json({
        code: 200,
        message: `成功删除了${val.deletedCount}条数据!`
      })
    })
  }).catch(err => {
    res.json({
      code: 401,
      message: "token失效了"
    })
  })
})

相关文章

网友评论

      本文标题:从购物车或者减少菜品的时候触发的接口(要考虑到满减活动)

      本文链接:https://www.haomeiwen.com/subject/sntygktx.html