电商商城系统网站
一、项目介绍
基于SpringBoot+Vue的前后端分离商城系统网站
运行环境:idea或eclipse vscode 数据库:mysql
语言:java
后台: SpringBoot + Mybatis-plus + Mybatis + Hutool工具包 + lombok插件
前台:Vue + Vue Router + ELementUI + Axios
功能:首页推荐、轮播图管理、商品、订单、购物车、商品评论、商品评论点赞、商品收藏、商品点赞、订单支付、公告。
角色:管理员、用户
二、系统部分功能截图
三、部分代码展示
package com.example.controller;
import cn.hutool.core.util.StrUtil;
import com.auth0.jwt.JWT;
import com.example.common.Result;
import com.example.service.UserService;
import com.example.entity.Address;
import com.example.service.AddressService;
import com.example.entity.User;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
@RestController
@RequestMapping("/api/address")
public class AddressController {
@Resource
private AddressService addressService;
@Resource
private HttpServletRequest request;
@Resource
private UserService userService;
public User getUser() {
String token = request.getHeader("token");
String username = JWT.decode(token).getAudience().get(0);
return userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, username));
}
@PostMapping
public Result<?> save(@RequestBody Address address) {
addressService.save(address);
return Result.success();
}
@PutMapping
public Result<?> update(@RequestBody Address address) {
addressService.updateById(address);
return Result.success();
}
@DeleteMapping("/{id}")
public Result<?> delete(@PathVariable Long id) {
addressService.removeById(id);
return Result.success();
}
@GetMapping("/{id}")
public Result<?> findById(@PathVariable Long id) {
return Result.success(addressService.getById(id));
}
@GetMapping
public Result<?> findAll() {
LambdaQueryWrapper<Address> query = Wrappers.<Address>lambdaQuery().orderByDesc(Address::getId);
query.eq(Address::getUserId, getUser().getId());
List<Address> list = addressService.list(query);
return Result.success(list);
}
@GetMapping("/page")
public Result<?> findPage(
@RequestParam(required = false, defaultValue = "1") Integer pageNum,
@RequestParam(required = false, defaultValue = "10") Integer pageSize) {
LambdaQueryWrapper<Address> query = Wrappers.<Address>lambdaQuery().orderByDesc(Address::getId);
// query.eq(Address::getUserId, getUser().getId());
IPage<Address> page = addressService.page(new Page<>(pageNum, pageSize), query);
return Result.success(page);
}
@GetMapping("/page/front")
public Result<?> Front(
@RequestParam(required = false, defaultValue = "1") Integer pageNum,
@RequestParam(required = false, defaultValue = "10") Integer pageSize) {
User user = getUser();
if(user == null) {
return Result.success(new Page<>());
}
LambdaQueryWrapper<Address> query = Wrappers.<Address>lambdaQuery().orderByDesc(Address::getId);
query.eq(Address::getUserId, getUser().getId());
IPage<Address> page = addressService.page(new Page<>(pageNum, pageSize), query);
return Result.success(page);
}
}
package com.example.controller;
import cn.hutool.core.date.DateUtil;
import com.auth0.jwt.JWT;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.entity.Cart;
import com.example.entity.User;
import com.example.service.CartService;
import com.example.service.UserService;
import org.json.JSONException;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/api/cart")
public class CartController {
@Resource
private CartService cartService;
@Resource
private HttpServletRequest request;
@Resource
private UserService userService;
public User getUser() {
String token = request.getHeader("token");
String username = JWT.decode(token).getAudience().get(0);
return userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, username));
}
@PostMapping
public Result<?> save(@RequestBody Cart cart) {
cart.setCreateTime(DateUtil.now());
// 加入购物车,相同的商品累加
Cart userCart = cartService.getOne(Wrappers.<Cart>lambdaQuery().eq(Cart::getGoodsId, cart.getGoodsId()).eq(Cart::getUserId, cart.getUserId()));
if (userCart != null) {
userCart.setCount(cart.getCount() + userCart.getCount());
cartService.updateById(userCart);
} else {
// 不同商品添加新的购物车记录
cartService.save(cart);
}
return Result.success();
}
@PutMapping
public Result<?> update(@RequestBody Cart cart) {
cartService.updateById(cart);
return Result.success();
}
@DeleteMapping("/{id}")
public Result<?> delete(@PathVariable Long id) {
cartService.removeById(id);
return Result.success();
}
@GetMapping("/{id}")
public Result<?> findById(@PathVariable Long id) {
return Result.success(cartService.getById(id));
}
@GetMapping
public Result<?> findAll() throws JSONException {
List<Cart> carts = new ArrayList<>();
User user = getUser();
if (user != null) {
carts = cartService.list(Wrappers.<Cart>lambdaQuery().eq(Cart::getUserId, getUser().getId()));
}
return Result.success(cartService.findAll(carts));
}
@GetMapping("/page")
public Result<?> findPage(@RequestParam(required = false, defaultValue = "") String name,
@RequestParam(required = false, defaultValue = "1") Integer pageNum,
@RequestParam(required = false, defaultValue = "10") Integer pageSize) {
LambdaQueryWrapper<Cart> query = Wrappers.<Cart>lambdaQuery().orderByDesc(Cart::getId);
IPage<Cart> page = cartService.page(new Page<>(pageNum, pageSize), query);
return Result.success(page);
}
}
package com.example.controller;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.auth0.jwt.JWT;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.dto.PreOrderQo;
import com.example.entity.*;
import com.example.exception.CustomException;
import com.example.service.*;
import org.json.JSONException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/api/order")
public class OrderController {
@Resource
private OrderService orderService;
@Resource
private HttpServletRequest request;
@Resource
private UserService userService;
@Resource
private CartService cartService;
@Resource
private OrderGoodsService orderGoodsService;
@Resource
private GoodsService goodsService;
public User getUser() {
String token = request.getHeader("token");
String username = JWT.decode(token).getAudience().get(0);
return userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, username));
}
@Transactional
@PostMapping
public Result<?> save(@RequestBody Order order) {
order.setUserId(getUser().getId());
order.setOrderNo(DateUtil.format(new Date(), "yyyyMMddHHmmss") + RandomUtil.randomNumbers(6));
order.setCreateTime(DateUtil.now());
String cartsStr = order.getCarts();
List<Cart> carts = JSONUtil.toBean(cartsStr, new TypeReference<List<Cart>>() {
}, true);
orderService.save(order);
for (Cart cart : carts) {
Integer count = cart.getCount();
Long goodsId = cart.getGoodsId();
// 扣库存
Goods goods = goodsService.getById(goodsId);
if(goods.getStore() - cart.getCount() < 0) {
throw new CustomException("-1", "库存不足");
}
goods.setStore(goods.getStore() - cart.getCount());
goods.setSales(goods.getSales() + cart.getCount());
goodsService.updateById(goods);
OrderGoods orderGoods = new OrderGoods();
orderGoods.setOrderId(order.getId());
orderGoods.setGoodsId(goodsId);
orderGoods.setCount(count);
orderGoodsService.save(orderGoods);
}
if (order.getType() == 1) { // 1表示购物车,0表示直接购买
// 提交订单时清空个人的购物车商品
cartService.remove(Wrappers.<Cart>lambdaUpdate().eq(Cart::getUserId, getUser().getId()));
}
return Result.success(order);
}
@PutMapping
public Result<?> update(@RequestBody Order order) {
orderService.updateById(order);
return Result.success();
}
/**
* 付款
* @param id
* @return
*/
@Transactional
@PutMapping("/pay/{id}")
public Result<?> pay(@PathVariable Long id) {
Order order = orderService.getById(id);
BigDecimal totalPrice = order.getTotalPrice();
Long userId = getUser().getId();
User user = userService.findById(userId);
if (user.getAccount().compareTo(totalPrice) <= 0) {
throw new CustomException("-1", "余额不足");
}
user.setAccount(user.getAccount().subtract(totalPrice)); // 设置用户余额
userService.updateById(user);
order.setState("待发货");
orderService.updateById(order);
return Result.success();
}
@DeleteMapping("/{id}")
public Result<?> delete(@PathVariable Long id) {
orderService.removeById(id);
return Result.success();
}
/**
* 获取订单的确认信息
*
* @return
*/
@PostMapping("/pre")
public Result<?> pre(@RequestBody PreOrderQo preOrderQo) throws JSONException {
String cartsStr = preOrderQo.getCarts();
// 讲前台传来的json字符串转换成 list对象
List<Cart> carts = JSONUtil.toBean(cartsStr, new TypeReference<List<Cart>>() {
}, true);
Map<String, Object> all = cartService.findAll(carts);
return Result.success(all);
}
@GetMapping("/{id}")
public Result<?> findById(@PathVariable Long id) {
return Result.success(orderService.getById(id));
}
@GetMapping
public Result<?> findAll() {
List<Order> list = orderService.list();
return Result.success(list);
}
@GetMapping("/page")
public Result<?> findPage(@RequestParam(required = false, defaultValue = "") String name,
@RequestParam(required = false, defaultValue = "1") Integer pageNum,
@RequestParam(required = false, defaultValue = "10") Integer pageSize) {
LambdaQueryWrapper<Order> query = Wrappers.<Order>lambdaQuery().orderByDesc(Order::getId);
if (StrUtil.isNotBlank(name)) {
query.like(Order::getOrderNo, name);
}
IPage<Order> page = orderService.page(new Page<>(pageNum, pageSize), query);
return Result.success(page);
}
/**
* 前台查询订单列表
* @param state
* @param pageNum
* @param pageSize
* @return
*/
@GetMapping("/page/front")
public Result<?> findPageFront(@RequestParam(required = false, defaultValue = "") String state,
@RequestParam(required = false, defaultValue = "1") Integer pageNum,
@RequestParam(required = false, defaultValue = "10") Integer pageSize) {
LambdaQueryWrapper<Order> query = Wrappers.<Order>lambdaQuery().orderByDesc(Order::getId);
query.eq(Order::getUserId, getUser().getId());
// 根据状态查询
if (StrUtil.isNotBlank(state)) {
query.eq(Order::getState, state);
}
IPage<Order> page = orderService.page(new Page<>(pageNum, pageSize), query);
for (Order order : page.getRecords()) {
Long orderId = order.getId();
List<Cart> carts = orderGoodsService.findByOrderId(orderId);
order.setCarts(JSONUtil.toJsonStr(carts));
}
return Result.success(page);
}
}
四、底部获取项目
链接:https://pan.baidu.com/s/1qfxy0oiz1vHu74THhGApWw?pwd=h1et
提取码:h1et
--来自百度网盘超级会员V6的分享