Search in sources :

Example 6 with TbUser

use of cn.e3mall.manager.pojo.TbUser in project e3mall by colg-cloud.

the class LoginInterceptor method preHandle.

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    /*
		 * 从cookie中取token
		 * 	token存在:	调用sso服务取用户信息
		 * 					取到:		是登录状态, 需要把用户信息写入request, 判断cookie中是否有购物车数据, 如果有就合并到服务端
		 * 					取不到:	用户登录已过期, 需要登录
		 * 	token不存在:	未登录状态, 跳转到sso系统的登录页面, 用户登录成功后, 跳转到当前请求的url
		 */
    String token = CookieUtils.getCookieValue(request, TOKEN_KEY);
    if (StringUtils.isBlank(token)) {
        // token不存在
        response.sendRedirect(SSO_URL + "/page/login?redirect=" + request.getRequestURL());
        // 拦截
        return false;
    }
    // token存在
    E3Result e3Result = tokenService.getUserByToken(token);
    if (!e3Result.isSuccess()) {
        // 取不到
        response.sendRedirect(SSO_URL + "/page/login?redirect=" + request.getRequestURL());
        // 拦截
        return false;
    }
    TbUser tbUser = (TbUser) e3Result.getData();
    request.setAttribute("user", tbUser);
    // 判断cookie中是否有购物车数据
    String jsonString = CookieUtils.getCookieValue(request, COOKIE_CART, true);
    if (StringUtils.isNotBlank(jsonString)) {
        // 合并购物车
        cartService.mergeCart(tbUser.getId(), JSON.parseArray(jsonString, TbItem.class));
    }
    return true;
}
Also used : E3Result(cn.e3mall.common.pojo.E3Result) TbUser(cn.e3mall.manager.pojo.TbUser) TbItem(cn.e3mall.manager.pojo.TbItem)

Example 7 with TbUser

use of cn.e3mall.manager.pojo.TbUser in project e3mall by colg-cloud.

the class CartController method showCartList.

/**
 * 展示购物车列表, 返回购物车列表视图
 *
 * @param request
 * @return
 */
@GetMapping("/cart")
public String showCartList(HttpServletRequest request, HttpServletResponse response) {
    /*
		 * 	1. 从cookie中获取购物车列表
		 *  2. 判断用户是否登录
		 *  	登录状态:
		 *  		1). 把客户端(cookie)中的购物车商品和服务端(redis)中的购物车商品合并
		 *  		2). 删除cookie中的购物车
		 *  		3). 从redis中获取购物车列表, 传递到页面, 展示
		 *  	未登录:	把cookie中列表传递到页面,展示
		 */
    // 从cookie中取购物车列表
    List<TbItem> cartList = this.getCartListFromCookie(request);
    // 判断用户是否为登录状态
    TbUser tbUser = (TbUser) request.getAttribute("user");
    if (tbUser != null) {
        // 合并
        Long userId = tbUser.getId();
        cartService.mergeCart(userId, cartList);
        // 删除cookie中的购物车
        CookieUtils.deleteCookie(request, response, COOKIE_CART);
        // 从redis中获取购物车列表
        cartList = cartService.getCartList(userId);
    }
    // 把列表传递给页面
    request.setAttribute("cartList", cartList);
    // 返回逻辑视图
    return "cart";
}
Also used : TbUser(cn.e3mall.manager.pojo.TbUser) TbItem(cn.e3mall.manager.pojo.TbItem) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 8 with TbUser

use of cn.e3mall.manager.pojo.TbUser in project e3mall by colg-cloud.

the class CartController method deleteCartItem.

/**
 * 删除购物车商品
 *
 * @return 购物车列表视图
 */
@GetMapping("/delete/{itemId}")
public String deleteCartItem(@PathVariable Long itemId, HttpServletRequest request, HttpServletResponse response) {
    // 判断用户是否登录
    TbUser tbUser = (TbUser) request.getAttribute("user");
    if (tbUser != null) {
        cartService.deleteCartItem(tbUser.getId(), itemId);
        return "redirect:/cart/cart.html";
    }
    // 从cookie中取购物车列表
    List<TbItem> cartList = this.getCartListFromCookie(request);
    // 遍历列表,找到要删除的商品
    Iterator<TbItem> iterator = cartList.iterator();
    while (iterator.hasNext()) {
        TbItem tbItem = iterator.next();
        if (tbItem.getId() == itemId.longValue()) {
            // 删除商品
            iterator.remove();
            break;
        }
    }
    // 把购物车列表写入cookie
    CookieUtils.setCookie(request, response, COOKIE_CART, JSON.toJSONString(cartList), COOKIE_CART_EXPIRE, true);
    // 重定向到逻辑视图, 绝对路径 在项目后面拼接 /cart/cart.html
    return "redirect:/cart/cart.html";
}
Also used : TbUser(cn.e3mall.manager.pojo.TbUser) TbItem(cn.e3mall.manager.pojo.TbItem) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

TbUser (cn.e3mall.manager.pojo.TbUser)8 TbItem (cn.e3mall.manager.pojo.TbItem)5 GetMapping (org.springframework.web.bind.annotation.GetMapping)3 E3Result (cn.e3mall.common.pojo.E3Result)2 TbUserService (cn.e3mall.sso.service.TbUserService)1 Test (org.junit.Test)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1