黑马瑞吉外卖项目之用户退出登录功能和点餐购物车减一功能

用户退出登录功能

/**
     * 用户退出
     * @param request
     * @return
     */
    @PostMapping("/loginout")
    public R<String> logout(HttpServletRequest request){
          
   
        //清理Session中保存的当前登陆用户的id
        request.getSession().removeAttribute("user");
        return R.success("退出成功");
    }

点餐购物车减一功能

/**
     * 减一
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
          
   
        log.info("购物车数据:{}",shoppingCart);

        //获取当前是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);
        
		Long dishId = shoppingCart.getDishId();
        if (dishId != null){
          
   
            //添加到购物车的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
          
   
            //添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        //查询购物车中当前菜品或者套餐
        //SQL :select * from shopping_cart where user_id = ?
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);

        //在原来数量基础上减一
        Integer number = cartServiceOne.getNumber();
        cartServiceOne.setNumber(number - 1);
        shoppingCartService.updateById(cartServiceOne);

        return R.success(cartServiceOne);
    }
经验分享 程序员 微信小程序 职场和发展