tiefen_space_h5/components/WithAuth/index.js

49 lines
1.5 KiB
JavaScript
Raw Normal View History

2024-07-03 19:59:39 +08:00
import { checkAuth } from "@/utils/auth";
2024-07-31 18:16:25 +08:00
import { useRouter, usePathname, useSearchParams } from "next/navigation";
2024-07-19 16:22:43 +08:00
import { useEffect } from "react";
import { get } from "@/utils/storeInfo";
2024-07-22 14:38:59 +08:00
import { Toast } from "antd-mobile";
2024-08-02 22:12:54 +08:00
import {save} from "@/utils/storeInfo"
2024-07-03 19:59:39 +08:00
export default function WithAuth(WrappedComponent) {
2024-07-19 16:22:43 +08:00
const router = useRouter();
2024-07-31 18:16:25 +08:00
2024-07-22 14:38:59 +08:00
const pathname = usePathname();
2024-07-31 18:16:25 +08:00
const searchParams = useSearchParams();
2024-07-19 16:22:43 +08:00
useEffect(() => {
2024-08-02 22:12:54 +08:00
if(searchParams.get("inviter")){
save("inviter",Number(searchParams.get("inviter")))
}
2024-07-22 17:55:52 +08:00
if(!pathname.includes("webView") && !pathname.includes("login") ){
checkLogin();
}
// console.log("isLogin",!pathname.includes("webView") && !pathname.includes("login"))
2024-07-22 14:38:59 +08:00
}, [pathname]);
2024-07-19 16:22:43 +08:00
const checkLogin = async () => {
2024-07-22 14:38:59 +08:00
const hasToken = await get("token");
2024-07-24 20:20:09 +08:00
// console.log("hasToken", hasToken);
2024-07-19 16:22:43 +08:00
if (hasToken) {
const hasToken = get("token");
2024-07-24 20:20:09 +08:00
// console.log("hasToken", hasToken);
2024-07-19 16:22:43 +08:00
if (hasToken) {
const currentIsLogin = await checkAuth();
if (!currentIsLogin) {
2024-07-22 14:38:59 +08:00
Toast.show({
icon: "fail",
content: "当前登录失效,请重新登录",
position: "top",
});
router.push("/login");
2024-07-22 17:55:52 +08:00
}else{
2024-08-02 22:12:54 +08:00
2024-07-22 17:55:52 +08:00
if(pathname.includes("login")){
router.replace("/")
}
2024-07-19 16:22:43 +08:00
}
}
2024-07-31 18:16:25 +08:00
} else if(!searchParams.get("forgetPassword")) {
2024-07-22 14:38:59 +08:00
router.push("/login");
2024-07-19 16:22:43 +08:00
}
};
return WrappedComponent;
2024-08-02 22:12:54 +08:00
}