feat(手机端): 购物车模块

完善购物车模块
master
waynaqua 4 years ago
parent 5dfdd02d35
commit 9fc8060daa

@ -2,23 +2,22 @@ package com.wayn.mobile.api.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wayn.common.base.BaseController;
import com.wayn.common.core.domain.shop.Goods;
import com.wayn.common.core.domain.shop.GoodsProduct;
import com.wayn.common.core.domain.system.User;
import com.wayn.common.core.service.shop.IGoodsProductService;
import com.wayn.common.core.service.shop.IGoodsService;
import com.wayn.common.util.R;
import com.wayn.common.util.security.SecurityUtils;
import com.wayn.mobile.api.domain.Cart;
import com.wayn.mobile.api.service.ICartService;
import com.wayn.mobile.framework.security.util.SecurityUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wayn.common.base.BaseController;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
@ -34,44 +33,16 @@ import java.util.Objects;
@RequestMapping("cart")
public class CartController extends BaseController {
@Autowired
private IGoodsService iGoodsService;
@Autowired
private IGoodsProductService iGoodsProductService;
@Autowired
private ICartService iCartService;
/**
*
* <p>
*
*
*
* @param cart { goodsId: xxx, productId: xxx, number: xxx }
* @return
*/
@PostMapping("add")
public R add(@RequestBody Cart cart) {
return iCartService.addCart(cart);
}
Integer goodsId = cart.getGoodsId();
Integer productId = cart.getProductId();
Integer number = cart.getNumber();
if (ObjectUtils.allNotNull(goodsId, productId, number) || number <= 0) {
return R.error("参数错误");
}
Goods goods = iGoodsService.getById(goodsId);
if (Objects.isNull(iGoodsProductService) || !goods.getIsOnSale()) {
return R.error("商品已经下架");
}
List<GoodsProduct> products = iGoodsProductService.list(new QueryWrapper<GoodsProduct>().eq("goods_id", goodsId));
Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
boolean flag = iCartService.checkExistsGoods(userId, goodsId, productId);
if (flag) {
}
return null;
@GetMapping("goodsCount")
public R goodsCount() {
return iCartService.goodsCount();
}
}

@ -1,5 +1,6 @@
package com.wayn.mobile.api.service;
import com.wayn.common.util.R;
import com.wayn.mobile.api.domain.Cart;
import com.baomidou.mybatisplus.extension.service.IService;
@ -20,5 +21,22 @@ public interface ICartService extends IService<Cart> {
* @param productId
* @return
*/
boolean checkExistsGoods(Long userId, Integer goodsId, Integer productId);
Cart checkExistsGoods(Long userId, Integer goodsId, Integer productId);
/**
*
* <p>
*
*
*
* @param cart { goodsId: xxx, productId: xxx, number: xxx }
* @return R
*/
R addCart(Cart cart);
/**
*
* @return R
*/
R goodsCount();
}

@ -2,13 +2,23 @@ package com.wayn.mobile.api.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wayn.common.core.domain.shop.Goods;
import com.wayn.common.core.domain.shop.GoodsProduct;
import com.wayn.common.core.service.shop.IGoodsProductService;
import com.wayn.common.core.service.shop.IGoodsService;
import com.wayn.common.util.R;
import com.wayn.mobile.api.domain.Cart;
import com.wayn.mobile.api.mapper.CartMapper;
import com.wayn.mobile.api.service.ICartService;
import com.wayn.mobile.framework.security.util.SecurityUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
/**
* <p>
@ -24,12 +34,72 @@ public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements IC
@Autowired
private CartMapper cartMapper;
@Autowired
private IGoodsService iGoodsService;
@Autowired
private IGoodsProductService iGoodsProductService;
@Override
public boolean checkExistsGoods(Long userId, Integer goodsId, Integer productId) {
Cart cart = cartMapper.selectOne(new QueryWrapper<Cart>()
public Cart checkExistsGoods(Long userId, Integer goodsId, Integer productId) {
return cartMapper.selectOne(new QueryWrapper<Cart>()
.eq("user_id", userId)
.eq("goods_id", goodsId)
.eq("product_id", productId));
return false;
}
@Override
public R addCart(Cart cart) {
Integer goodsId = cart.getGoodsId();
Integer productId = cart.getProductId();
Integer number = cart.getNumber();
if (!ObjectUtils.allNotNull(goodsId, productId, number) || number <= 0) {
return R.error("参数错误");
}
Goods goods = iGoodsService.getById(goodsId);
if (Objects.isNull(iGoodsProductService) || !goods.getIsOnSale()) {
return R.error("商品已经下架");
}
Long userId = SecurityUtils.getLoginUser().getMember().getId();
GoodsProduct product = iGoodsProductService.getById(productId);
Cart existsCart = checkExistsGoods(userId, goodsId, productId);
if (Objects.isNull(existsCart)) {
if (Objects.isNull(product) || product.getNumber() < number) {
return R.error("库存不足");
}
cart.setGoodsSn(goods.getGoodsSn());
cart.setGoodsName(goods.getName());
if (StringUtils.isEmpty(product.getUrl())) {
cart.setPicUrl(goods.getPicUrl());
} else {
cart.setPicUrl(product.getUrl());
}
cart.setPrice(product.getPrice());
cart.setSpecifications((product.getSpecifications()));
cart.setUserId(Math.toIntExact(userId));
cart.setChecked(true);
cart.setCreateTime(LocalDateTime.now());
save(cart);
} else {
int num = existsCart.getNumber() + number;
if (num > product.getNumber()) {
return R.error("库存不足");
}
existsCart.setNumber(num);
cart.setUpdateTime(LocalDateTime.now());
if (!updateById(existsCart)) {
return R.error();
}
}
return goodsCount();
}
@Override
public R goodsCount() {
Long userId = SecurityUtils.getLoginUser().getMember().getId();
List<Cart> cartList = list(new QueryWrapper<Cart>()
.eq("user_id", userId));
return R.success().add("count", cartList.size());
}
}

@ -0,0 +1,77 @@
package com.wayn.mobile.framework.security.util;
import com.wayn.common.exception.BusinessException;
import com.wayn.mobile.framework.security.LoginUserDetail;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
/**
*
*
* @author ruoyi
*/
public class SecurityUtils {
/**
*
**/
public static String getUsername() {
try {
return getLoginUser().getUsername();
} catch (Exception e) {
throw new BusinessException("获取用户账户异常", HttpStatus.UNAUTHORIZED.value());
}
}
/**
*
**/
public static LoginUserDetail getLoginUser() {
try {
return (LoginUserDetail) getAuthentication().getPrincipal();
} catch (Exception e) {
throw new BusinessException("获取用户信息异常", HttpStatus.UNAUTHORIZED.value());
}
}
/**
* Authentication
*/
public static Authentication getAuthentication() {
return SecurityContextHolder.getContext().getAuthentication();
}
/**
* BCryptPasswordEncoder
*
* @param password
* @return
*/
public static String encryptPassword(String password) {
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
return passwordEncoder.encode(password);
}
/**
*
*
* @param rawPassword
* @param encodedPassword
* @return
*/
public static boolean matchesPassword(String rawPassword, String encodedPassword) {
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
return passwordEncoder.matches(rawPassword, encodedPassword);
}
/**
*
*
* @param userId ID
* @return
*/
public static boolean isAdmin(Long userId) {
return userId != null && 1L == userId;
}
}
Loading…
Cancel
Save