فهرست منبع

处理加购接口返回的新的cart token

fogwind 2 هفته پیش
والد
کامیت
02638eb9ee
1فایلهای تغییر یافته به همراه9 افزوده شده و 2 حذف شده
  1. 9 2
      src/utils/hooks/useAddToCart.ts

+ 9 - 2
src/utils/hooks/useAddToCart.ts

@@ -6,6 +6,7 @@ import { addItem, clearCart } from "@/store/slices/cart-slice";
 import { isObject } from "@utils/type-guards";
 import { getCartToken } from "@utils/getCartToken";
 import { getCookie, setCookie, deleteCookie } from "@utils/cookie-tools";
+import { encodeJWT } from "@/utils/jwt-cookie";
 import { useGuestCartToken } from "./useGuestCartToken";
 import { IS_GUEST,GUEST_CART_TOKEN,GUEST_CART_ID } from "@/utils/constants";
 import { useMutation } from "@apollo/client/react";
@@ -43,9 +44,15 @@ export const useAddProduct = () => {
             const responseCartId = responseData.id;
             const responseCartToken = responseData.cartToken;
             if(isGuest && responseCartId !== responseCartToken) {
-                setCookie(GUEST_CART_TOKEN, responseCartToken);
+                const newToken = encodeJWT({
+                  sessionToken: responseCartToken,
+                  cartId: responseCartId,
+                  isGuest: isGuest,
+                });
+        
+                setCookie(GUEST_CART_TOKEN, newToken,{encode: false});
                 setCookie(GUEST_CART_ID, responseCartId);
-                setCookie(IS_GUEST, "false");
+                setCookie(IS_GUEST, String(isGuest));
             }
             /** 兜底代码 end*/