From 308ed895a070086a52a78808036e2317c22dd3ba Mon Sep 17 00:00:00 2001 From: liangzai <2440983361@qq.com> Date: Sat, 21 Sep 2024 12:10:02 +0800 Subject: [PATCH] v2.0 --- src/app/(console)/create/page.tsx | 2 + src/app/(console)/create/paypalCallback.tsx | 47 +++++++++++++++++++++ src/hooks/useUser.tsx | 38 +++++++++++++++++ src/ui/login/login-form.tsx | 4 +- 4 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 src/app/(console)/create/paypalCallback.tsx create mode 100644 src/hooks/useUser.tsx diff --git a/src/app/(console)/create/page.tsx b/src/app/(console)/create/page.tsx index 7dea6ab..0f975af 100644 --- a/src/app/(console)/create/page.tsx +++ b/src/app/(console)/create/page.tsx @@ -1,5 +1,6 @@ import { useTranslations } from "next-intl"; import CreateTabs from "@/ui/(console)/create/create-tabs"; +import PaypalCallback from "./paypalCallback"; export default function Page() { const t = useTranslations("createPage"); @@ -19,6 +20,7 @@ export default function Page() { + ); } diff --git a/src/app/(console)/create/paypalCallback.tsx b/src/app/(console)/create/paypalCallback.tsx new file mode 100644 index 0000000..6e16750 --- /dev/null +++ b/src/app/(console)/create/paypalCallback.tsx @@ -0,0 +1,47 @@ +"use client"; + +import useFetch from "@/hooks/useFetch"; +import { useEffect } from "react"; +import { useSearchParams, useRouter } from "next/navigation"; +import { useToast } from "@/contexts/ToastContext"; +import { useTranslations } from "next-intl"; +import useUser from "@/hooks/useUser"; + +export default function PaypalCallback() { + const searchParams = useSearchParams(); + const router = useRouter(); + const paymentId = searchParams.get("paymentId"); + const token = searchParams.get("token"); + const PayerID = searchParams.get("PayerID"); + const t = useTranslations("pricing"); + const { addToastSuccess } = useToast(); + const { updateUser } = useUser(); + const { + fetchData: executePayment, + loading: paymentLoading, + data: paymentData, + } = useFetch({ + url: "/api/paypal/execute/", + method: "POST", + }); + + useEffect(() => { + if (paymentId && token && PayerID) { + executePayment({ + paymentId, + PayerID, + }) + .then(() => { + addToastSuccess(t("PaymentSuccess")); + // 删除URL上的参数 + }) + .finally(() => { + const newUrl = window.location.pathname; + window.history.replaceState(null, "", newUrl); + updateUser(); + }); + } + }, [paymentId, token, PayerID]); + + return null; +} diff --git a/src/hooks/useUser.tsx b/src/hooks/useUser.tsx new file mode 100644 index 0000000..7aab082 --- /dev/null +++ b/src/hooks/useUser.tsx @@ -0,0 +1,38 @@ +"use client"; + +import { useEffect } from "react"; +import useFetch from "@/hooks/useFetch"; +import useUserStore from "@/store/userStore"; + +export default function useUser() { + const setUser = useUserStore((state) => state.setUser); + + const { + fetchData: GetUserinfo, + loading: userinfoLoading, + data: userinfoData, + } = useFetch({ + url: "/api/profile/", + method: "POST", + }); + + const updateUser = async () => { + try { + const res = await GetUserinfo(); + console.log("userinfo", res); + setUser(res); + } catch (error) { + console.error("Error updating user:", error); + } + }; + + // useEffect(() => { + // updateUser(); + // }, []); // 空数组依赖确保只在组件挂载时执行一次 + + return { + updateUser, + userinfoLoading, + userinfoData, + }; +} diff --git a/src/ui/login/login-form.tsx b/src/ui/login/login-form.tsx index 0f84807..5c01689 100644 --- a/src/ui/login/login-form.tsx +++ b/src/ui/login/login-form.tsx @@ -238,14 +238,14 @@ export default function LoginForm({ toRegister }: LoginFormProps) {