This commit is contained in:
liangzai 2024-09-21 12:10:02 +08:00
parent c15bd28871
commit 308ed895a0
4 changed files with 89 additions and 2 deletions

View File

@ -1,5 +1,6 @@
import { useTranslations } from "next-intl"; import { useTranslations } from "next-intl";
import CreateTabs from "@/ui/(console)/create/create-tabs"; import CreateTabs from "@/ui/(console)/create/create-tabs";
import PaypalCallback from "./paypalCallback";
export default function Page() { export default function Page() {
const t = useTranslations("createPage"); const t = useTranslations("createPage");
@ -19,6 +20,7 @@ export default function Page() {
<CreateTabs /> <CreateTabs />
</div> </div>
</div> </div>
<PaypalCallback />
</div> </div>
); );
} }

View File

@ -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;
}

38
src/hooks/useUser.tsx Normal file
View File

@ -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,
};
}

View File

@ -238,14 +238,14 @@ export default function LoginForm({ toRegister }: LoginFormProps) {
<button <button
type="button" type="button"
onClick={handleGetCaptcha} onClick={handleGetCaptcha}
className="btn h-10 btn-sm w-32 ml-2 bg-transparent hover:bg-green-500 hover:text-white text-gray-500 rounded-lg" className="btn h-10 btn-sm w-32 ml-2 bg-transparent hover:bg-green-500 hover:text-white text-gray-200 rounded-lg"
disabled={ disabled={
captchaTimer > 0 || captchaTimer > 0 ||
captchaLoading captchaLoading
} }
> >
{captchaLoading ? ( {captchaLoading ? (
<span className="loading loading-spinner loading-md"></span> <span className="loading loading-spinner loading-md text-gray-200"></span>
) : captchaTimer > 0 ? ( ) : captchaTimer > 0 ? (
`${captchaTimer}s` `${captchaTimer}s`
) : ( ) : (