谷粒商城购物车(三十八)
236 - 247
让人感兴趣的就是线程隔离和拦截器一起用,还有一个就是添加购物车后进行重定向。思路的话,就是临时用户和登录用户的那块逻辑不要忘了,实时增加删减购物车都要对redis进行操作。
ThreadLocal可以查看之前的博客:https://www.cnblogs.com/dalianpai/p/12623044.html
具体的代码如下:
/**
* @author WGR
* @create 2020/7/22 -- 16:06
*/
public class CartInterceptor implements HandlerInterceptor {
public static ThreadLocal<UserInfoVo> threadLocal = new ThreadLocal<>();
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
UserInfoVo userInfoVo = new UserInfoVo();
HttpSession session = request.getSession();
MemberRespVo memberRespVo = (MemberRespVo) session.getAttribute(AuthServerConstant.LOGIN_USER);
if(memberRespVo !=null){
userInfoVo.setUserId(memberRespVo.getId());
}
Cookie[] cookies = request.getCookies();
if(cookies !=null && cookies.length>0){
for (Cookie cookie:cookies){
String name = cookie.getName();
if(name.equals(CartConstant.TEMP_USER_COOKIE_NAME)){
userInfoVo.setUserKey(cookie.getValue());
userInfoVo.setTempUser(true);
}
}
}
if(StringUtils.isBlank(userInfoVo.getUserKey())){
userInfoVo.setUserKey(UUID.randomUUID().toString());
}
threadLocal.set(userInfoVo);
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
UserInfoVo userInfoVo = threadLocal.get();
if(!userInfoVo.isTempUser()){
Cookie cookie = new Cookie(CartConstant.TEMP_USER_COOKIE_NAME, userInfoVo.getUserKey());
cookie.setDomain("gulimall.com");
cookie.setMaxAge(CartConstant.TEMP_USER_COOKIE_TIMEOUT);
response.addCookie(cookie);
}
}
}
web层
/**
* @author WGR
* @create 2020/7/22 -- 16:56
*/
@Controller
public class CartController {
@Autowired
CartService cartService;
@GetMapping("/deleteItem")
public String deleteItem(@RequestParam("skuId") Long skuId){
cartService.deleteItem(skuId);
return "redirect:http://cart.gulimall.com/cart.html";
}
@GetMapping("/checkCount")
public String checkCount(@RequestParam("skuId") Long skuId,@RequestParam("num")Integer num){
cartService.checkCount(skuId,num);
return "redirect:http://cart.gulimall.com/cart.html";
}
@GetMapping("/checkItem")
public String checkItem(@RequestParam("skuId") Long skuId,@RequestParam("check")Integer check){
cartService.checkItem(skuId,check);
return "redirect:http://cart.gulimall.com/cart.html";
}
@GetMapping("/cart.html")
public String cartListPage(Model model) throws ExecutionException, InterruptedException {
UserInfoVo userInfoVo = CartInterceptor.threadLocal.get();
Cart cart = cartService.getCart();
model.addAttribute("cart",cart);
return "cartList";
}
@GetMapping("/addToCart")
public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num,
RedirectAttributes ra) throws ExecutionException, InterruptedException {
cartService.addToCart(skuId,num);
ra.addAttribute("skuId",skuId);
return "redirect:http://cart.gulimall.com/addToCartSuccess.html";
}
@GetMapping("/addToCartSuccess.html")
public String addToCartSuccess(@RequestParam("skuId") Long skuId,Model model){
CartItem cartItem = cartService.getCartItem(skuId);
model.addAttribute("item",cartItem);
return "success";
}
}
service
/**
* @author WGR
* @create 2020/7/22 -- 16:56
*/
@Controller
public class CartController {
@Autowired
CartService cartService;
@GetMapping("/deleteItem")
public String deleteItem(@RequestParam("skuId") Long skuId){
cartService.deleteItem(skuId);
return "redirect:http://cart.gulimall.com/cart.html";
}
@GetMapping("/checkCount")
public String checkCount(@RequestParam("skuId") Long skuId,@RequestParam("num")Integer num){
cartService.checkCount(skuId,num);
return "redirect:http://cart.gulimall.com/cart.html";
}
@GetMapping("/checkItem")
public String checkItem(@RequestParam("skuId") Long skuId,@RequestParam("check")Integer check){
cartService.checkItem(skuId,check);
return "redirect:http://cart.gulimall.com/cart.html";
}
@GetMapping("/cart.html")
public String cartListPage(Model model) throws ExecutionException, InterruptedException {
UserInfoVo userInfoVo = CartInterceptor.threadLocal.get();
Cart cart = cartService.getCart();
model.addAttribute("cart",cart);
return "cartList";
}
@GetMapping("/addToCart")
public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num,
RedirectAttributes ra) throws ExecutionException, InterruptedException {
cartService.addToCart(skuId,num);
ra.addAttribute("skuId",skuId);
return "redirect:http://cart.gulimall.com/addToCartSuccess.html";
}
@GetMapping("/addToCartSuccess.html")
public String addToCartSuccess(@RequestParam("skuId") Long skuId,Model model){
CartItem cartItem = cartService.getCartItem(skuId);
model.addAttribute("item",cartItem);
return "success";
}
}
效果图如下: