Compare commits
4 Commits
579365293c
...
b41919f218
Author | SHA1 | Date |
---|---|---|
|
b41919f218 | |
|
836f95edd6 | |
|
068e7f116d | |
|
b47bfeafb6 |
|
@ -0,0 +1,17 @@
|
|||
package com.atguigu.anno;
|
||||
|
||||
import com.atguigu.interceptor.feign.UserTokenFeignInterceptor;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
// com.atguigu.spzx.common.anno;
|
||||
@Retention(value = RetentionPolicy.RUNTIME)
|
||||
@Target(value = ElementType.TYPE)
|
||||
@Import(value = UserTokenFeignInterceptor.class)
|
||||
public @interface EnableUserTokenFeignInterceptor {
|
||||
|
||||
}
|
|
@ -3,7 +3,6 @@ package com.atguigu.interceptor;
|
|||
import com.alibaba.fastjson.JSON;
|
||||
import com.atguigu.context.BaseContext;
|
||||
import com.atguigu.spzx.model.entity.user.UserInfo;
|
||||
import com.atguigu.utils.EmptyUtil;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
@ -20,8 +19,6 @@ import org.springframework.web.servlet.HandlerInterceptor;
|
|||
public class UserLoginAuthInterceptor implements HandlerInterceptor {
|
||||
@Autowired
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
@Autowired
|
||||
private EmptyUtil emptyUtil;
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull Object handler) throws Exception {
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
package com.atguigu.interceptor.feign;
|
||||
|
||||
import feign.RequestInterceptor;
|
||||
import feign.RequestTemplate;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
public class UserTokenFeignInterceptor implements RequestInterceptor {
|
||||
@Override
|
||||
public void apply(RequestTemplate requestTemplate) {
|
||||
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
assert requestAttributes != null;
|
||||
HttpServletRequest request = requestAttributes.getRequest();
|
||||
String token = request.getHeader("token");
|
||||
requestTemplate.header("token", token);
|
||||
}
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
package com.atguigu;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
*
|
||||
*/
|
||||
public class App
|
||||
{
|
||||
public static void main( String[] args )
|
||||
{
|
||||
System.out.println( "Hello World!" );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.atguigu.feign.cart;
|
||||
|
||||
import com.atguigu.spzx.model.entity.h5.CartInfo;
|
||||
import com.atguigu.spzx.model.vo.result.Result;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@FeignClient(value = "service-cart", path = "/api/order/cart")
|
||||
public interface CartFeignClient {
|
||||
@GetMapping(value = "/auth/getAllCkecked")
|
||||
Result<List<CartInfo>> getAllChecked();
|
||||
}
|
|
@ -7,8 +7,8 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
|
|||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
// 排除数据库的自动化配置,Cart微服务不需要访问数据库
|
||||
@ComponentScan("com.atguigu")
|
||||
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
|
||||
@ComponentScan("com.atguigu")
|
||||
@EnableFeignClients(basePackages = {"com.atguigu"})
|
||||
public class CartApplication {
|
||||
public static void main(String[] args) {
|
||||
|
|
|
@ -62,4 +62,11 @@ public class CartController {
|
|||
cartService.clearCart();
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
@Operation(summary = "选中的购物车")
|
||||
@GetMapping(value = "/auth/getAllCkecked")
|
||||
public Result<List<CartInfo>> getAllChecked() {
|
||||
List<CartInfo> cartInfoList = cartService.getAllChecked();
|
||||
return Result.success(cartInfoList);
|
||||
}
|
||||
}
|
|
@ -46,4 +46,11 @@ public interface CartService {
|
|||
* 清空购物车
|
||||
*/
|
||||
void clearCart();
|
||||
|
||||
/**
|
||||
* 选中的购物车
|
||||
*
|
||||
* @return CartInfo列表
|
||||
*/
|
||||
List<CartInfo> getAllChecked();
|
||||
}
|
||||
|
|
|
@ -157,4 +157,27 @@ public class CartServiceImpl implements CartService {
|
|||
String cartKey = RedisUtils.getCartKey(userId);
|
||||
redisTemplate.delete(cartKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 选中的购物车
|
||||
*
|
||||
* @return CartInfo列表
|
||||
*/
|
||||
@Override
|
||||
public List<CartInfo> getAllChecked() {
|
||||
// 获取用户id
|
||||
Long userId = BaseContext.getUserInfo().getId();
|
||||
String cartKey = RedisUtils.getCartKey(userId);
|
||||
|
||||
// 根据key获取购物车所有商品
|
||||
List<Object> objectList = redisTemplate.opsForHash().values(cartKey);
|
||||
|
||||
if (!CollectionUtils.isEmpty(objectList)) {
|
||||
return objectList.stream()
|
||||
.map(object -> JSON.parseObject(JSON.toJSONString(object), CartInfo.class))
|
||||
.filter(cartInfo -> cartInfo.getIsChecked() == 1)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,6 +18,10 @@
|
|||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.atguigu</groupId>
|
||||
<artifactId>service-user-client</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
|
@ -1,10 +1,14 @@
|
|||
package com.atguigu.order;
|
||||
|
||||
import com.atguigu.anno.EnableUserTokenFeignInterceptor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableFeignClients(basePackages = {"com.atguigu.feign.cart"})
|
||||
@EnableUserTokenFeignInterceptor
|
||||
@Slf4j
|
||||
public class OrderApplication {
|
||||
public static void main(String[] args) {
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
package com.atguigu.order.controller;
|
||||
|
||||
import com.atguigu.order.service.OrderInfoService;
|
||||
import com.atguigu.spzx.model.vo.h5.TradeVo;
|
||||
import com.atguigu.spzx.model.vo.result.Result;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Tag(name = "订单管理")
|
||||
@RestController
|
||||
@RequestMapping(value = "/api/order/orderInfo")
|
||||
public class OrderInfoController {
|
||||
@Autowired
|
||||
private OrderInfoService orderInfoService;
|
||||
|
||||
@Operation(summary = "确认下单")
|
||||
@GetMapping("auth/trade")
|
||||
public Result<TradeVo> trade() {
|
||||
TradeVo tradeVo = orderInfoService.getTrade();
|
||||
return Result.success(tradeVo);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.atguigu.order.mapper;
|
||||
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface OrderInfoMapper {
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.atguigu.order.service;
|
||||
|
||||
import com.atguigu.spzx.model.vo.h5.TradeVo;
|
||||
|
||||
public interface OrderInfoService {
|
||||
/**
|
||||
* 确认下单
|
||||
*
|
||||
* @return 结算实体类
|
||||
*/
|
||||
TradeVo getTrade();
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.atguigu.order.service.impl;
|
||||
|
||||
import com.atguigu.order.service.OrderInfoService;
|
||||
import com.atguigu.order.service.module.OrderInfoServiceModule;
|
||||
import com.atguigu.spzx.model.entity.order.OrderItem;
|
||||
import com.atguigu.spzx.model.vo.h5.TradeVo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class OrderInfoServiceImpl implements OrderInfoService {
|
||||
@Autowired
|
||||
private OrderInfoServiceModule orderInfoServiceModule;
|
||||
|
||||
/**
|
||||
* 确认下单
|
||||
*
|
||||
* @return 结算实体类
|
||||
*/
|
||||
@Override
|
||||
public TradeVo getTrade() {
|
||||
// 远程调用获取购物车中商品列表
|
||||
ArrayList<OrderItem> orderItems = orderInfoServiceModule.getOrderItems();
|
||||
// 计算总金额
|
||||
BigDecimal totalAmount;
|
||||
totalAmount = orderItems.stream()
|
||||
.map(orderItem -> orderItem.getSkuPrice().multiply(new BigDecimal(orderItem.getSkuNum())))
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
TradeVo tradeVo = new TradeVo();
|
||||
tradeVo.setOrderItemList(orderItems);
|
||||
tradeVo.setTotalAmount(totalAmount);
|
||||
|
||||
return tradeVo;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.atguigu.order.service.module;
|
||||
|
||||
import com.atguigu.feign.cart.CartFeignClient;
|
||||
import com.atguigu.spzx.model.entity.h5.CartInfo;
|
||||
import com.atguigu.spzx.model.entity.order.OrderItem;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class OrderInfoServiceModule {
|
||||
@Autowired
|
||||
private CartFeignClient cartFeignClient;
|
||||
|
||||
@NotNull
|
||||
public ArrayList<OrderItem> getOrderItems() {
|
||||
List<CartInfo> cartInfoList = cartFeignClient.getAllChecked().getData();
|
||||
// 创建LIst集合封装订单项
|
||||
ArrayList<OrderItem> orderItems = new ArrayList<>();
|
||||
cartInfoList.forEach(cartInfo -> {
|
||||
OrderItem orderItem = new OrderItem();
|
||||
orderItem.setSkuId(cartInfo.getSkuId());
|
||||
orderItem.setSkuName(cartInfo.getSkuName());
|
||||
orderItem.setSkuNum(cartInfo.getSkuNum());
|
||||
orderItem.setSkuPrice(cartInfo.getCartPrice());
|
||||
orderItem.setThumbImg(cartInfo.getImgUrl());
|
||||
orderItems.add(orderItem);
|
||||
});
|
||||
return orderItems;
|
||||
}
|
||||
}
|
|
@ -1,7 +1,18 @@
|
|||
bunny:
|
||||
nacos:
|
||||
server-addr: z-bunny.cn:8848
|
||||
discovery:
|
||||
namespace: spzx
|
||||
|
||||
datasource:
|
||||
host: 106.15.251.123
|
||||
port: 3305
|
||||
sqlData: db_spzx
|
||||
username: root
|
||||
password: "02120212"
|
||||
|
||||
redis:
|
||||
host: 47.120.65.66
|
||||
port: 6379
|
||||
database: 2
|
||||
password: "02120212"
|
|
@ -22,6 +22,13 @@ spring:
|
|||
username: ${bunny.datasource.username}
|
||||
password: "${bunny.datasource.password}"
|
||||
|
||||
data:
|
||||
redis:
|
||||
host: ${bunny.redis.host}
|
||||
port: ${bunny.redis.port}
|
||||
database: ${bunny.redis.database}
|
||||
password: ${bunny.redis.password}
|
||||
|
||||
logging:
|
||||
level:
|
||||
com.atguigu.spzx.order.mapper: debug
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="com.atguigu.order.mapper.OrderInfoMapper">
|
||||
|
||||
</mapper>
|
|
@ -0,0 +1,30 @@
|
|||
package com.atguigu.user.controller;
|
||||
|
||||
import com.atguigu.spzx.model.entity.user.UserAddress;
|
||||
import com.atguigu.spzx.model.vo.result.Result;
|
||||
import com.atguigu.user.service.UserAddressService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Tag(name = "用户地址接口")
|
||||
@RestController
|
||||
@RequestMapping(value = "/api/user/userAddress")
|
||||
@Slf4j
|
||||
public class UserAddressController {
|
||||
@Autowired
|
||||
private UserAddressService userAddressService;
|
||||
|
||||
@Operation(summary = "获取用户地址列表")
|
||||
@GetMapping("auth/findUserAddressList")
|
||||
public Result<List<UserAddress>> findUserAddressList() {
|
||||
List<UserAddress> list = userAddressService.findUserAddressList();
|
||||
return Result.success(list);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.atguigu.user.mapper;
|
||||
|
||||
import com.atguigu.spzx.model.entity.user.UserAddress;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface UserAddressMapper {
|
||||
/**
|
||||
* 查询用户地址栏
|
||||
*
|
||||
* @param userId 用户id
|
||||
* @return 地址列表
|
||||
*/
|
||||
List<UserAddress> selectByUserId(Long userId);
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.atguigu.user.service;
|
||||
|
||||
import com.atguigu.spzx.model.entity.user.UserAddress;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface UserAddressService {
|
||||
/**
|
||||
* 获取用户地址列表
|
||||
*
|
||||
* @return 用户地址列表
|
||||
*/
|
||||
List<UserAddress> findUserAddressList();
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package com.atguigu.user.service.impl;
|
||||
|
||||
import com.atguigu.constant.MessageConstant;
|
||||
import com.atguigu.context.BaseContext;
|
||||
import com.atguigu.exception.BunnyException;
|
||||
import com.atguigu.spzx.model.entity.user.UserAddress;
|
||||
import com.atguigu.user.mapper.UserAddressMapper;
|
||||
import com.atguigu.user.service.UserAddressService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class UserAddressServiceImpl implements UserAddressService {
|
||||
@Autowired
|
||||
private UserAddressMapper userAddressMapper;
|
||||
|
||||
/**
|
||||
* 获取用户地址列表
|
||||
*
|
||||
* @return 用户地址列表
|
||||
*/
|
||||
@Override
|
||||
public List<UserAddress> findUserAddressList() {
|
||||
// 查询用户地址栏
|
||||
Long userId = BaseContext.getUserInfo().getId();
|
||||
if (userId != null) {
|
||||
return userAddressMapper.selectByUserId(userId);
|
||||
}
|
||||
throw new BunnyException(MessageConstant.FIND_ID_IS_NOT_EMPTY);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="com.atguigu.user.mapper.UserAddressMapper">
|
||||
|
||||
<!-- 查询用户地址栏 -->
|
||||
<select id="selectByUserId" resultType="com.atguigu.spzx.model.entity.user.UserAddress">
|
||||
select *
|
||||
from user_address
|
||||
where user_id = #{userId}
|
||||
</select>
|
||||
</mapper>
|
Loading…
Reference in New Issue