Jelajahi Sumber

游客购物车产品删除清空之后不再重新创建cart token

fogwind 2 minggu lalu
induk
melakukan
883109747e
1 mengubah file dengan 21 tambahan dan 4 penghapusan
  1. 21 4
      src/utils/hooks/useAddToCart.ts

+ 21 - 4
src/utils/hooks/useAddToCart.ts

@@ -5,9 +5,9 @@ import { useAppDispatch } from "@/store/hooks";
 import { addItem, clearCart } from "@/store/slices/cart-slice";
 import { isObject } from "@utils/type-guards";
 import { getCartToken } from "@utils/getCartToken";
-import { getCookie } from "@utils/cookie-tools";
+import { getCookie, setCookie, deleteCookie } from "@utils/cookie-tools";
 import { useGuestCartToken } from "./useGuestCartToken";
-import { IS_GUEST } from "@/utils/constants";
+import { IS_GUEST,GUEST_CART_TOKEN,GUEST_CART_ID } from "@/utils/constants";
 import { useMutation } from "@apollo/client/react";
 import {
   CREATE_ADD_PRODUCT_IN_CART,
@@ -20,7 +20,7 @@ import { formatCartDetail } from "@/utils/cartDetailTools";
 
 export const useAddProduct = () => {
   const dispatch = useAppDispatch();
-  const { createGuestToken, resetGuestToken } = useGuestCartToken();
+  const { createGuestToken } = useGuestCartToken();
   const { showToast } = useCustomToast();
 
   const [mutateAsync, { loading: isCartLoading }] = useMutation(
@@ -36,6 +36,19 @@ export const useAddProduct = () => {
         }
         if (responseData) {
           if (responseData.success) {
+            /** 兜底代码 start*/
+            // 游客加购,然后清空购物车,然后刷新页面,然后再加购,然后再刷新页面,购物车会失效,所以添加兜底代码
+            const isGuest = getCookie(IS_GUEST) !== "false";// 'true' 是游客
+            // 如果之前的cartToken有效,responseCartId和responseCartToken的值是一样的都是cart id; 否则两者不相等
+            const responseCartId = responseData.id;
+            const responseCartToken = responseData.cartToken;
+            if(isGuest && responseCartId !== responseCartToken) {
+                setCookie(GUEST_CART_TOKEN, responseCartToken);
+                setCookie(GUEST_CART_ID, responseCartId);
+                setCookie(IS_GUEST, "false");
+            }
+            /** 兜底代码 end*/
+
             const cartDetail = formatCartDetail(responseData);
             dispatch(addItem(cartDetail));
             showToast("Product added to cart successfully", "success");
@@ -60,10 +73,12 @@ export const useAddProduct = () => {
     token?: string;
     cartId?: number | string;
   }) => {
+    
     // Ensure token exists - create if needed
     let token = getCartToken(); // 从cookie获取token
 
     if (!token) {
+      // 没有token,则创建一个并写入cookie
       token = await createGuestToken();
 
       if (!token) {
@@ -71,6 +86,7 @@ export const useAddProduct = () => {
         return;
       }
     }
+    
     const param : { productId: number; quantity:number; variantId?: number; } = {
       productId: parseInt(productId),
       quantity,
@@ -106,7 +122,8 @@ export const useAddProduct = () => {
 
             const isGuest = getCookie(IS_GUEST);
             if (isGuest === "true") {
-              resetGuestToken();
+              deleteCookie(GUEST_CART_TOKEN);
+              deleteCookie(GUEST_CART_ID);
             }
           }
         } else {