美文网首页
选择食品加购物车触发的接口(要考虑到满减活动)

选择食品加购物车触发的接口(要考虑到满减活动)

作者: 风中凌乱的男子 | 来源:发表于2020-12-06 18:47 被阅读0次
// 选择食品加购物车触发的接口
router.post('/select/food/add/: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) {
          // 说明购物车内没有这个菜,走新增流程,把count设置为1
          new ShoppingCart(shopCard).save().then(card => {
            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
              // res.json({
              //   code: 200,
              //   data: obj,
              //   message: "计算成功"
              // })
              // res.json(obj)
              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失效了"
    })
  })
})
// 满减方法
function manjian(totalPrice, rules) {
  if (rules == '') {
    return totalPrice
  } else {
    let sale;
    for (let i = 0; i < rules.length; i++) {
      if (rules[i].totalPrice == totalPrice) {
        sale = rules[i].cutPrice
      } else if (i >= 1) {
        if (totalPrice < rules[i].totalPrice && totalPrice > rules[i - 1].totalPrice) {
          sale = rules[i - 1].cutPrice
        }
      } else if (totalPrice < rules[0].totalPrice) {
        sale = 0;
      } else if (totalPrice > rules[rules.length - 1].totalPrice) {
        sale = rules[rules.length - 1].cutPrice
      }
    }
    return totalPrice - sale
  }

}

返回示例

{
    "code": 200,
    "data": {
        "list": [
            {
                "_id": "5fd475908984da57f2d311f4",
                "count": "29",
                "shop_id": "5fbd20455e2f15606b946828",
                "user_id": "5f637340bc117f4f93c543c5",
                "food_id": "5fcb99e1b1d98f04e55ea517",
                "foodName": "平菇",
                "foodPrice": "3",
                "date": "2020-12-12T07:47:28.762Z",
                "__v": 0,
                "totalPrice": 87
            },
            {
                "_id": "5fd47828eec4d8584c607cbc",
                "count": "4",
                "shop_id": "5fbd20455e2f15606b946828",
                "user_id": "5f637340bc117f4f93c543c5",
                "food_id": "5fcb9a35ff27e804f49245b1",
                "foodName": "海鲜菇",
                "foodPrice": "3",
                "date": "2020-12-12T07:58:32.979Z",
                "__v": 0,
                "totalPrice": 12
            }
        ],
        "totalPrice": 29,
        "count": 33
    },
    "message": "计算成功"
}

相关文章

网友评论

      本文标题:选择食品加购物车触发的接口(要考虑到满减活动)

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