import { checkAuth } from "@/utils/auth"; import { useRouter, usePathname, useSearchParams } from "next/navigation"; import { useEffect } from "react"; import { get } from "@/utils/storeInfo"; import { Toast } from "antd-mobile"; import { save } from "@/utils/storeInfo"; import { removeUserInfo } from "@/utils/storeInfo"; export default function WithAuth(WrappedComponent) { const router = useRouter(); const pathname = usePathname(); const searchParams = useSearchParams(); useEffect(() => { if (searchParams.get("inviter")) { save("inviter", Number(searchParams.get("inviter"))); } if (!pathname.includes("webView") && !pathname.includes("login")) { checkLogin(); } // console.log("isLogin",!pathname.includes("webView") && !pathname.includes("login")) }, [pathname]); const checkLogin = async () => { const hasToken = await get("token"); if (pathname.includes("wxpay_h5")) { return; } if (hasToken) { const currentIsLogin = await checkAuth(); if (!currentIsLogin) { Toast.show({ icon: "fail", content: "当前登录失效,请重新登录", position: "top", }); removeUserInfo(); router.push("/login"); } else { if (pathname.includes("login")) { router.push("/"); } } } else if (!searchParams.get("forgetPassword")) { removeUserInfo(); router.push("/login"); } }; return WrappedComponent; }