|
@@ -5,9 +5,9 @@ import { useAppDispatch } from "@/store/hooks";
|
|
|
import { addItem, clearCart } from "@/store/slices/cart-slice";
|
|
import { addItem, clearCart } from "@/store/slices/cart-slice";
|
|
|
import { isObject } from "@utils/type-guards";
|
|
import { isObject } from "@utils/type-guards";
|
|
|
import { getCartToken } from "@utils/getCartToken";
|
|
import { getCartToken } from "@utils/getCartToken";
|
|
|
-import { getCookie } from "@utils/cookie-tools";
|
|
|
|
|
|
|
+import { getCookie, setCookie, deleteCookie } from "@utils/cookie-tools";
|
|
|
import { useGuestCartToken } from "./useGuestCartToken";
|
|
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 { useMutation } from "@apollo/client/react";
|
|
|
import {
|
|
import {
|
|
|
CREATE_ADD_PRODUCT_IN_CART,
|
|
CREATE_ADD_PRODUCT_IN_CART,
|
|
@@ -20,7 +20,7 @@ import { formatCartDetail } from "@/utils/cartDetailTools";
|
|
|
|
|
|
|
|
export const useAddProduct = () => {
|
|
export const useAddProduct = () => {
|
|
|
const dispatch = useAppDispatch();
|
|
const dispatch = useAppDispatch();
|
|
|
- const { createGuestToken, resetGuestToken } = useGuestCartToken();
|
|
|
|
|
|
|
+ const { createGuestToken } = useGuestCartToken();
|
|
|
const { showToast } = useCustomToast();
|
|
const { showToast } = useCustomToast();
|
|
|
|
|
|
|
|
const [mutateAsync, { loading: isCartLoading }] = useMutation(
|
|
const [mutateAsync, { loading: isCartLoading }] = useMutation(
|
|
@@ -36,6 +36,19 @@ export const useAddProduct = () => {
|
|
|
}
|
|
}
|
|
|
if (responseData) {
|
|
if (responseData) {
|
|
|
if (responseData.success) {
|
|
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);
|
|
const cartDetail = formatCartDetail(responseData);
|
|
|
dispatch(addItem(cartDetail));
|
|
dispatch(addItem(cartDetail));
|
|
|
showToast("Product added to cart successfully", "success");
|
|
showToast("Product added to cart successfully", "success");
|
|
@@ -60,10 +73,12 @@ export const useAddProduct = () => {
|
|
|
token?: string;
|
|
token?: string;
|
|
|
cartId?: number | string;
|
|
cartId?: number | string;
|
|
|
}) => {
|
|
}) => {
|
|
|
|
|
+
|
|
|
// Ensure token exists - create if needed
|
|
// Ensure token exists - create if needed
|
|
|
let token = getCartToken(); // 从cookie获取token
|
|
let token = getCartToken(); // 从cookie获取token
|
|
|
|
|
|
|
|
if (!token) {
|
|
if (!token) {
|
|
|
|
|
+ // 没有token,则创建一个并写入cookie
|
|
|
token = await createGuestToken();
|
|
token = await createGuestToken();
|
|
|
|
|
|
|
|
if (!token) {
|
|
if (!token) {
|
|
@@ -71,6 +86,7 @@ export const useAddProduct = () => {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
const param : { productId: number; quantity:number; variantId?: number; } = {
|
|
const param : { productId: number; quantity:number; variantId?: number; } = {
|
|
|
productId: parseInt(productId),
|
|
productId: parseInt(productId),
|
|
|
quantity,
|
|
quantity,
|
|
@@ -106,7 +122,8 @@ export const useAddProduct = () => {
|
|
|
|
|
|
|
|
const isGuest = getCookie(IS_GUEST);
|
|
const isGuest = getCookie(IS_GUEST);
|
|
|
if (isGuest === "true") {
|
|
if (isGuest === "true") {
|
|
|
- resetGuestToken();
|
|
|
|
|
|
|
+ deleteCookie(GUEST_CART_TOKEN);
|
|
|
|
|
+ deleteCookie(GUEST_CART_ID);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|