taotao購物車2 解決購物車本地cookie和伺服器redis不同步的問題
下面的思路邏輯一定要理清楚,比較繞
思路;
前面已經實現了在cookie本地維護購物車的功能,
這次加入和伺服器同步功能,
因為 購物車 操作比較頻繁,所以,後臺伺服器 用redis儲存使用者的購物車資訊
邏輯是:
寫一個後臺操作redis的介面(也可以寫兩個):要實現的功能是
1.通過使用者id從redis中取出使用者的購物車資訊(購物車商品集合)
2.通過使用者id向redis中寫入使用者的購物車資訊
一、使用者向購物車中新增商品時的邏輯:
判斷使用者是否登入,
如果沒有登入,則繼續只操作cookie
如果使用者登入了,則呼叫遠端介面獲取使用者的 redis中的購物車資訊 redisList,再獲取cookie的購物車資訊cookieList
將這兩個購物車中的商品合併(相同商品數量相加,不同商品都新增)組成新的購物車集合 newLIst
然後再 將當期 要加入購物車的商品 和 newList 比較,如果已經存在,則增加數量,如果不存在,則增加商品,
最後將 newList 呼叫遠端介面,寫入redis中
二、使用者修改購物車中商品數量時的邏輯:
修改購物車中商品的數量,一定是在已經開啟購物車列表頁面,那麼也就是說,這時,肯定已經明確過了使用者是否登入,
也就是說,如果使用者已經登入了,那麼這時本地cookie和redis中的購物車一定是同步的。
所以,這時,
不用先和遠端redis同步,可以先修改本地cookie的商品數量,即cookieList,
等修改完成後,再只執行遠端向redis中寫入購物車newList即可
三、刪除購物車中的商品,邏輯 基本 等同於 修改 購物車商品數量,即,只在修改完本地 cookie的list後,向redis寫入即可
四、展示購物車列表邏輯:
先判斷使用者是否登入,
如果使用者沒有登入,則只從cookie中取出cookieList 返回前臺展示即可;
如果使用者已經登入,則只需要從redis中取出 reidsList,返回前臺展示即可;
(原因是:我們這個方法只是展示購物車列表,並不會操作購物車列表,即不會產生本地cookie和redis中不同步的問題,而前面說過,只要會產生本地cookie和遠端redis不同步的方法,我們在方法結束前都已經做過了同步,所以,這裡我們要展示購物車列表,只需要從redis中取redisList即可,因為這時redisList和cookieList一定是同步的
portal門戶專案:
Controller程式碼:
package com.taotao.portal.controller; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.taotao.common.pojo.CartItem; import com.taotao.common.pojo.TaotaoResult; import com.taotao.portal.service.CartService; /** * 購物車的Controller * @author Administrator */ @Controller @RequestMapping("/cart") public class CartController { @Autowired private CartService cartService; /** * 新增商品到購物車 * @param itemId * @param num * @param request * @param response * @return */ @RequestMapping("/add/{itemId}") public String addCartItem(@PathVariable Long itemId, @RequestParam(defaultValue="1")Integer num, HttpServletRequest request,HttpServletResponse response) { cartService.addCartItem(itemId, num, request, response); // return "cartSuccess"; //這樣會開啟新的頁面,所以不用 //為了 避免每次新增完商品都開啟新的頁面,所以這裡這樣重定向 return "redirect:/cart/success.html"; // return "forward:/cart/success.html"; //這種寫法也可以(請求轉發) } //接收重定向的請求返回jsp頁面 @RequestMapping("/success") public String showSuccess(){ return "cartSuccess"; } /** * 查詢購物車中的商品列表返回前臺展示 * @param request * @param response * @param model * @return */ @RequestMapping("/cart") public String showCart(HttpServletRequest request,HttpServletResponse response,Model model){ List<CartItem> cartItemList = cartService.getCartItemList(request, response); model.addAttribute("cartList", cartItemList); return "cart"; } /** * 修改商品數量(包括點加減號和收到寫值) * @param itemId * @param num * @param request * @param response * @return */ @RequestMapping("/update/{itemId}") @ResponseBody public String updateCartItemNum(@PathVariable Long itemId, @RequestParam(defaultValue="0")Integer num, HttpServletRequest request,HttpServletResponse response) { cartService.updateCartItmeNum(itemId, num, request, response); // return "redirect:/cart/cart.html"; return ""; } /** * 刪除購物車中的指定商品 * @param itemId * @param request * @param response * @return */ @RequestMapping("/delete/{itemId}") public String delCartItem(@PathVariable Long itemId, HttpServletRequest request,HttpServletResponse response){ cartService.delCartItem(itemId, request, response); return "redirect:/cart/cart.html"; } }
Service層程式碼:
package com.taotao.portal.service.impl; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import com.taotao.common.pojo.CartItem; import com.taotao.common.pojo.TaotaoResult; import com.taotao.common.utils.CookieUtils; import com.taotao.common.utils.HttpClientUtil; import com.taotao.common.utils.JsonUtils; import com.taotao.pojo.TbItem; import com.taotao.pojo.TbUser; import com.taotao.portal.service.CartService; import com.taotao.portal.service.UserService; /** * 購物車service * @author Administrator */ @Service public class CartServiceImpl implements CartService { //rest服務基礎url @Value("${REST_BASE_URL}") private String REST_BASE_URL; //獲取商品資訊的url @Value("${ITEM_INFO_URL}") private String ITEM_INFO_URL; //同步購物車的url @Value("${REST_CART_SYNC_URL}") private String REST_CART_SYNC_URL; @Autowired private UserService userService; /** * 新增購物車商品 * 1、接收controller傳遞過來的商品id,根據商品id查詢商品資訊。 2、從cookie中取出購物車資訊,轉換成商品pojo列表。 3、把商品資訊新增到商品列表中。 引數: 1、商品id 2、Request 3、response 返回值: TaoTaoResult */ @Override public TaotaoResult addCartItem(long itemId, int num, HttpServletRequest request,HttpServletResponse response) { //合併購物車 Map<String, Object> map = mergeCart(request); List<CartItem> newList = (List<CartItem>) map.get("list"); //當前id對應的購物車商品 CartItem cartItem = null; //先判斷原購物車列表中是否有當前商品 for (CartItem item : newList) { //如果存在 if (item.getId()==itemId) { //增加商品數量 item.setNum(item.getNum()+num); cartItem = item; break; } } //如果原來沒有此商品,則新建 if (cartItem==null) { cartItem = new CartItem(); //呼叫rest服務獲取商品資訊 String doGetResult = HttpClientUtil.doGet(REST_BASE_URL+ITEM_INFO_URL+itemId); TaotaoResult taoTaoResult = TaotaoResult.formatToPojo(doGetResult, TbItem.class); if (taoTaoResult.getStatus()==200) { //封裝成購物車商品物件 TbItem item = (TbItem) taoTaoResult.getData(); cartItem.setId(itemId); cartItem.setImage(item.getImage()==null?"":item.getImage().split(",")[0]); cartItem.setPrice(item.getPrice()); cartItem.setTitle(item.getTitle()); cartItem.setSellPoint(item.getSellPoint()); cartItem.setNum(num); //將商品加入到購物車列表中 newList.add(cartItem); } } //如果登入則向redis傳送資料同步購物車 Boolean isLogin = (Boolean) map.get("isLogin"); if (isLogin) { Long userId = (Long) map.get("userId"); syncCart(userId, JsonUtils.objectToJson(newList)); } //將購物車列表寫回cookie String listJson = JsonUtils.objectToJson(newList); //最後一個引數 true,對資料進行編碼,這樣在 cookie中就看不到中文原始資料了 CookieUtils.setCookie(request, response, "TT_CART", listJson,true); return TaotaoResult.ok(); } //合併購物車的方法 private Map<String, Object> mergeCart(HttpServletRequest request) { List<CartItem> cookieCartList = getCartItemList(request); Map<String, Object> map = new HashMap<>(); //是否登入 Boolean isLogin = false; Long userId = null; List<CartItem> newList; //準備合併redis和當前cookie的購物車 //先判斷使用者是否登入 TbUser currentUser = getCurrentUser(request); if (currentUser!=null) { //如果登入,先獲取redis中的購物車 userId = currentUser.getId(); isLogin = true; List<CartItem> redisCart = syncCart(userId, null); if (redisCart!=null&&redisCart.size()>0) { //將兩個購物車列表合併 Iterator<CartItem> it = redisCart.iterator(); //遍歷redis購物車,對比,如果有和cookie一樣的商品,則把數量加到Cookie中,刪除自身 while (it.hasNext()) { CartItem redisItem = it.next(); for (CartItem cookieItem : cookieCartList) { if (redisItem.getId().equals(cookieItem.getId())) { System.out.println(redisItem.getId()); System.out.println(cookieItem.getId()); cookieItem.setNum(redisItem.getNum()+cookieItem.getNum()); //從resisList中刪除 it.remove(); break; } } } } newList = new ArrayList<CartItem>(); //合併 if (redisCart!=null && redisCart.size()>0) { newList.addAll(redisCart); } if (cookieCartList!=null && cookieCartList.size()>0) { newList.addAll(cookieCartList); } //向redis傳送資料同步購物車 syncCart(userId, JsonUtils.objectToJson(newList)); }else{ //使用者沒有登入時 newList = cookieCartList; } map.put("list", newList); map.put("isLogin", isLogin); map.put("userId", userId); return map; } /** * 從cookie中取商品列表 * @param request * @return */ private List<CartItem> getCartItemList(HttpServletRequest request) { //從cookie中取商品列表 String cartJson = CookieUtils.getCookieValue(request, "TT_CART",true); if (cartJson==null) { return new ArrayList<CartItem>(); } //把json轉換為商品列表 try { List<CartItem> list = JsonUtils.jsonToList(cartJson, CartItem.class); return list; } catch (Exception e) { e.printStackTrace(); } return new ArrayList<CartItem>(); } /** * 獲取 購物車商品列表供前臺展示 */ @Override public List<CartItem> getCartItemList(HttpServletRequest request, HttpServletResponse response) { //展示列表的邏輯: //判斷使用者是否登入,如果未登入,直接取cookie中的資料 //如果已登入,直接取redis中的資料(不涉及到合併及同步問題,因為這個只是展示,合併只有在 使用者新增商品到購物車時、使用者修改購物車時才需要) //定義要返回前臺的資料 List<CartItem> list = null; //判斷使用者是否已經登入,如果登入了,再同步 TbUser currentUser = getCurrentUser(request); if (currentUser!=null) { //如果登入則從redis中取資料到前臺 list = syncCart(currentUser.getId(),null); }else{ list = getCartItemList(request); } return list; } /** * 直接輸入數量,更新購物車中商品的數量(在購物車列表展示頁面中使用) */ @Override public TaotaoResult updateCartItmeNum(long itemId, int num, HttpServletRequest request, HttpServletResponse response){ //執行此方法是,一定是已經打開了購物車列表頁,也就是一定可以確定了使用者是否已經登入 //先進行未登入的正常邏輯 //當前id對應的購物車商品 CartItem cartItem = null; //從cookie中獲取購物車列表 List<CartItem> cartList = getCartItemList(request); //先判斷原購物車列表中是否有當前商品 for (CartItem item : cartList) { //如果存在 if (item.getId()==itemId) { //修改商品數量 item.setNum(num); cartItem = item; break; } } //判斷使用者是否已經登入,如果登入了,再同步 TbUser currentUser = getCurrentUser(request); if (currentUser!=null) { //如果登入則向redis傳送資料同步購物車 syncCart(currentUser.getId(), JsonUtils.objectToJson(cartList)); } //將購物車列表寫回cookie String listJson = JsonUtils.objectToJson(cartList); //最後一個引數 true,對資料進行編碼,這樣在 cookie中就看不到中文原始資料了 CookieUtils.setCookie(request, response, "TT_CART", listJson,true); return TaotaoResult.ok(); } /** * 刪除購物車中商品 * @param itemId * @param request * @param response * @return */ @Override public TaotaoResult delCartItem(long itemId, HttpServletRequest request, HttpServletResponse response){ //執行此方法是,一定是已經打開了購物車列表頁,也就是一定可以確定了使用者是否已經登入 //當前id對應的購物車商品 CartItem cartItem = null; //從cookie中獲取購物車列表 List<CartItem> cartList = getCartItemList(request); Iterator<CartItem> iterator = cartList.iterator(); //遍歷 while (iterator.hasNext()) { CartItem item = iterator.next(); //找到對應的商品 if (item.getId()==itemId) { //執行刪除動作 iterator.remove(); break; } } //判斷使用者是否已經登入,如果登入了,再同步 TbUser currentUser = getCurrentUser(request); if (currentUser!=null) { //如果登入則向redis傳送資料同步購物車 syncCart(currentUser.getId(), JsonUtils.objectToJson(cartList)); } //將購物車列表寫回cookie String listJson = JsonUtils.objectToJson(cartList); //最後一個引數 true,對資料進行編碼,這樣在 cookie中就看不到中文原始資料了 CookieUtils.setCookie(request, response, "TT_CART", listJson,true); return TaotaoResult.ok(); } /** * 從redis中獲取購物車資訊/同步購物車 * @param userId * @param cartList 當此引數為null時,為獲取redis中的購物車資訊;否則為向redis中同步購物車資訊 * @return */ private List<CartItem> syncCart(long userId, String cartList){ //定義返回值 List<CartItem> returnList = new ArrayList<CartItem>(); HashMap<String, String> map = new HashMap<String,String>(); map.put("userId", userId+""); map.put("cartList", cartList); String url = REST_BASE_URL+REST_CART_SYNC_URL; String json = HttpClientUtil.doPost(url, map); if (StringUtils.isNotEmpty(json)) { TaotaoResult taotaoResult = TaotaoResult.formatToList(json, CartItem.class); if (taotaoResult.getStatus()==200) { returnList = (ArrayList<CartItem>) taotaoResult.getData(); } } return returnList; } //獲取當前使用者資訊 public TbUser getCurrentUser(HttpServletRequest request) { //判斷使用者是否登入 //從cookie中取token String token = CookieUtils.getCookieValue(request, "TT_TOKEN"); //根據token換取使用者資訊,呼叫sso系統的介面 ///這裡需要寫一些業務 邏輯,不要在攔截器中寫,單寫一個service,只在這裡注入並呼叫 TbUser user = userService.getUserByToken(token); return user; } }
配置檔案:
#服務層屬性定義
#服務層基礎 url
REST_BASE_URL = http://localhost:8081/rest
#首頁大 廣告位
REST_INDEX_AD_URL = /content/list/89
#同步購物車的url
REST_CART_SYNC_URL=/cart/syncCart
cart.js程式碼:
var TTCart = { load : function(){ // 載入購物車資料 }, itemNumChange : function(){ $(".increment").click(function(){//+ var _thisInput = $(this).siblings("input"); _thisInput.val(eval(_thisInput.val()) + 1); $.post("/cart/update/"+_thisInput.attr("itemId")+".html?num="+_thisInput.val(),function(data){ TTCart.refreshTotalPrice(); }); }); $(".decrement").click(function(){//- var _thisInput = $(this).siblings("input"); if(eval(_thisInput.val()) == 1){ return ; } _thisInput.val(eval(_thisInput.val()) - 1); $.post("/cart/update/"+_thisInput.attr("itemId")+".html?num="+_thisInput.val(),function(data){ TTCart.refreshTotalPrice(); }); }); $(".quantity-form .quantity-text").rnumber(1);//限制只能輸入數字 $(".quantity-form .quantity-text").change(function(){ var _thisInput = $(this); $.post("/cart/update/"+_thisInput.attr("itemId")+".html?num="+_thisInput.val(),function(data){ debugger TTCart.refreshTotalPrice(); }); }); }, refreshTotalPrice : function(){ //重新計算總價 var total = 0; $(".quantity-form .quantity-text").each(function(i,e){ var _this = $(e); total += (eval(_this.attr("itemPrice")) * 10000 * eval(_this.val())) / 10000; }); $(".totalSkuPrice").html(new Number(total/100).toFixed(2)).priceFormat({ //價格格式化外掛 prefix: '¥', thousandsSeparator: ',', centsLimit: 2 }); } }; $(function(){ TTCart.load(); TTCart.itemNumChange(); });
然後是遠端介面服務層程式碼:
Controller層:
package com.taotao.rest.controller; import java.util.List; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.taotao.common.pojo.CartItem; import com.taotao.common.pojo.TaotaoResult; import com.taotao.rest.service.CartService; /**購物車Controller * @author Administrator * *當用戶登入時,同步cookie購物車和redis中購物車的 商品list *也可以在每次購物車中商品數量、新增商品、刪除商品、清空購物車時,都呼叫這個同步方法 */ @Controller @RequestMapping("/cart") public class CartCotroller { @Autowired private CartService cartService; @RequestMapping("/syncCart") @ResponseBody public TaotaoResult syncCart(long userId,String cartList) { /** 先拿到cookie中的購物車商品列表listCookie,然後再拿到redis中的購物車商品列表listRedis 然後遍歷比對,將兩者合併,合併時以cookie中的為準, 具體規則: 如果c中有而r中沒有,則合併後都有 如果c中沒有而r中有,則合併後都有 兩者都有時,把數量相加 */ /** * 因為這裡是rest專案只提供介面,不方便操作cookie,所以這裡只提供如下方法,其他邏輯在 portal專案中完成 一種是前臺根據使用者id獲取redis中儲存的 購物車資訊(適用於使用者登入時) 一種是前臺向後臺提供 購物車中的商品集合,後臺寫入redis */ List<CartItem> list = cartService.syncCart(userId, cartList); return TaotaoResult.ok(list); } }
service層:
package com.taotao.rest.service.impl; import java.util.ArrayList; import java.util.List; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import com.taotao.common.pojo.CartItem; import com.taotao.common.utils.JsonUtils; import com.taotao.rest.dao.JedisClient; import com.taotao.rest.service.CartService; @Service public class CartServiceImpl implements CartService { @Value("REDIS_CART_LIST_KEY") private String REDIS_CART_LIST_KEY; @Autowired private JedisClient jedisClient; @Override public List<CartItem> syncCart(long userId, String cartList) { /** * 因為這裡是rest專案只提供介面,不方便操作cookie,所以這裡只提供如下方法,其他邏輯在 portal專案中完成 一種是前臺根據使用者id獲取redis中儲存的 購物車資訊(適用於使用者登入時) 一種是前臺向後臺提供 購物車中的商品集合,後臺寫入redis */ //定義返回值 List<CartItem> list = new ArrayList<CartItem>(); if (StringUtils.isEmpty(cartList)) { //說明是使用者請求list //從redis中取出使用者的購物車資訊返回給前臺 String json = jedisClient.hget(REDIS_CART_LIST_KEY, userId+""); if (StringUtils.isNotEmpty(json)) { list = JsonUtils.jsonToList(json, CartItem.class); } }else{ //說明是使用者向後臺提供購物成資訊,準備儲存到redis中 jedisClient.hset(REDIS_CART_LIST_KEY, userId+"", cartList); } return list; } }
配置檔案:
#購物車商品列表在redis中儲存的key
REDIS_CART_LIST_KEY=REDIS_CART_LIST_KEY