文章目录
- 一、项目演示
- 二、项目介绍
- 三、运行截图
- 四、主要代码
一、项目演示
项目演示地址: 视频地址
二、项目介绍
项目描述:这是一个基于SpringBoot+微信小程序框架开发的在线订餐小程序系统。首先,这是一个前后端分离的项目,代码简洁规范,注释说明详细,易于理解和学习。其次,这项目功能丰富,具有一个在线订餐小程序系统该有的所有功能。
项目功能:此项目分为两个角色:普通用户和管理员。普通用户有登录注册、浏览商品信息、添加购物车、结算订单、查看个人信息、查看个人订单详情、管理个人地址信息、发布评价信息等等功能。管理员有管理所有商品信息、管理所有订单信息、管理所有用户信息、管理所有评价信息等等功能。
应用技术:SpringBoot + 微信小程序 + MySQL + MyBatis + Redis + ElementUI + Lua + Vue + Vant Weapp
运行环境:IntelliJ IDEA2019.3.5 + 微信开发者工具(项目压缩包中自带)+ MySQL5.7(项目压缩包中自带) + Redis5.0.5(项目压缩包中自带) + JDK1.8 + Maven3.6.3(项目压缩包中自带)+ Node14.16.1(项目压缩包中自带)
三、运行截图
四、主要代码
1.提交订单代码
/**
* 提交订单操作处理
* @param orderDTO
* @return
*/
@Override
public ResponseDTO<OrderDTO> submitOrder(OrderDTO orderDTO) {
UserDTO userDTO = new UserDTO();
userDTO.setToken(orderDTO.getToken());
ResponseDTO<UserDTO> loginUserResponse = userService.getLoginUser(userDTO);
if(!CodeMsg.SUCCESS.getCode().equals(loginUserResponse.getCode())) {
return ResponseDTO.errorByMsg(CodeMsg.USER_SESSION_EXPIRED);
}
// 获取登录用户信息
userDTO = loginUserResponse.getData();
AddressExample addressExample = new AddressExample();
addressExample.createCriteria().andUserIdEqualTo(userDTO.getId());
if(addressMapper.selectByExample(addressExample).size() == 0) {
return ResponseDTO.errorByMsg(CodeMsg.ADDRESS_NOT_EXIST);
}
String[] cartIdList = orderDTO.getCartIdList().split(",");
CartExample cartExample = new CartExample();
cartExample.createCriteria().andIdIn(Arrays.stream(cartIdList).collect(Collectors.toList()));
List<Cart> cartList = cartMapper.selectByExample(cartExample);
List<String> productIdList = cartList.stream().map(Cart::getProductId).collect(Collectors.toList());
ProductExample productExample = new ProductExample();
productExample.createCriteria().andIdIn(productIdList);
List<Product> productList = productMapper.selectByExample(productExample);
List<String> productNameList = productList.stream().map(Product::getName).collect(Collectors.toList());
List<String> productPriceList = productList.stream().map(Product::getPrice).map(String::valueOf).collect(Collectors.toList());
List<String> productPhotoList = productList.stream().map(Product::getPhoto).map(String::valueOf).collect(Collectors.toList());
List<String> cartQuantityList = cartList.stream().map(Cart::getQuantity).map(String::valueOf).collect(Collectors.toList());
String orderId = UuidUtil.getShortUuid();
String orderDate = CommonUtil.getFormatterDate(new Date(), "yyyy-MM-dd HH:mm:ss");
// 执行lua脚本
String result = stringRedisTemplate.execute(
SECKILL_SCRIPT,
Collections.singletonList(RedisConstant.STOCK_REDIS_KEY_TEMPLATE),
StringUtils.join(productIdList, ","),
StringUtils.join(productNameList, ","),
StringUtils.join(cartQuantityList, ","),
orderId,
userDTO.getId(),
orderDate,
StringUtils.join(productPriceList, ","),
StringUtils.join(productPhotoList, ","),
StringUtils.join(Arrays.asList(cartIdList), ",")
);
if(!"成功".equals(result)) {
CodeMsg codeMsg = CodeMsg.PRODUCT_STOCK_OVER;
codeMsg.setMsg(result);
return ResponseDTO.errorByMsg(codeMsg);
}
orderDTO.setId(orderId);
return ResponseDTO.success(orderDTO);
}
2.购物车保存操作(添加、减少)代码
/**
* 购物车保存操作(添加、减少)
* @param cartDTO
* @return
*/
@Override
public ResponseDTO<Boolean> saveCart(CartDTO cartDTO) {
UserDTO userDTO = new UserDTO();
userDTO.setToken(cartDTO.getToken());
ResponseDTO<UserDTO> loginUserResponse = userService.getLoginUser(userDTO);
if(!CodeMsg.SUCCESS.getCode().equals(loginUserResponse.getCode())) {
return ResponseDTO.errorByMsg(CodeMsg.USER_SESSION_EXPIRED);
}
// 获取登录用户信息
userDTO = loginUserResponse.getData();
cartDTO.setUserId(userDTO.getId());
Product product = productMapper.selectByPrimaryKey(cartDTO.getProductId());
if (product == null) {
return ResponseDTO.errorByMsg(CodeMsg.PRODUCT_NOT_EXIST);
}
Cart cart = CopyUtil.copy(cartDTO, Cart.class);
// 判断购物车是否已经有此商品
CartExample cartExample = new CartExample();
cartExample.createCriteria().andProductIdEqualTo(cartDTO.getProductId()).andUserIdEqualTo(cartDTO.getUserId());
List<Cart> cartList = cartMapper.selectByExample(cartExample);
if(cartList.size() > 0) {
// 购物车中已经有此商品
if(CartOperateEnum.ADD.getCode().equals(cartDTO.getOperateType())) {
// 添加操作
if(cart.getQuantity() + cartList.get(0).getQuantity() > product.getStock()) {
return ResponseDTO.errorByMsg(CodeMsg.PRODUCT_STOCK_OVER);
}
cartList.get(0).setQuantity(cart.getQuantity() + cartList.get(0).getQuantity());
} else if (CartOperateEnum.SUB.getCode().equals(cartDTO.getOperateType())) {
// 减少操作
if(cartList.get(0).getQuantity() <= cart.getQuantity()) {
// 删除
CartDTO copy = CopyUtil.copy(cartList.get(0), CartDTO.class);
copy.setToken(cartDTO.getToken());
removeCart(copy);
return ResponseDTO.successByMsg(true, "购物车操作成功!");
} else {
cartList.get(0).setQuantity(cartList.get(0).getQuantity() - cart.getQuantity());
}
}
// 更新数据
if(cartMapper.updateByPrimaryKeySelective(cartList.get(0)) == 0) {
return ResponseDTO.errorByMsg(CodeMsg.CART_SAVE_ERROR);
}
} else {
// 购物车中没有此商品
if(product.getStock() == 0) {
return ResponseDTO.errorByMsg(CodeMsg.PRODUCT_STOCK_OVER);
}
cart.setId(UuidUtil.getShortUuid());
// 添加数据
if(cartMapper.insertSelective(cart) == 0) {
return ResponseDTO.errorByMsg(CodeMsg.CART_SAVE_ERROR);
}
}
return ResponseDTO.successByMsg(true, "购物车操作成功!");
}
3.小程序登录操作代码
/**
* 小程序用户登录操作
* @param userDTO
* @return
*/
@Override
public ResponseDTO<UserDTO> appLogin(UserDTO userDTO) {
// 进行是否为空判断
if(CommonUtil.isEmpty(userDTO.getUsername())){
return ResponseDTO.errorByMsg(CodeMsg.USERNAME_EMPTY);
}
if(CommonUtil.isEmpty(userDTO.getPassword())){
return ResponseDTO.errorByMsg(CodeMsg.PASSWORD_EMPTY);
}
// 对比昵称和密码是否正确
UserExample userExample = new UserExample();
userExample.createCriteria().andUsernameEqualTo(userDTO.getUsername()).andPasswordEqualTo(userDTO.getPassword());
List<User> userList = userMapper.selectByExample(userExample);
if(userList == null || userList.size() != 1){
return ResponseDTO.errorByMsg(CodeMsg.USERNAME_PASSWORD_ERROR);
}
// 生成登录token并存入Redis中
User selectedUser = userList.get(0);
UserDTO selectedUserDTO = CopyUtil.copy(selectedUser, UserDTO.class);
String token = UuidUtil.getShortUuid();
selectedUserDTO.setToken(token);
//把token存入redis中 有效期1小时
stringRedisTemplate.opsForValue().set("USER_" + token, JSON.toJSONString(selectedUser), 3600, TimeUnit.SECONDS);
return ResponseDTO.successByMsg(selectedUserDTO, "登录成功!");
}