tiefen_space_h5/components/WithAuth/index.js

51 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-06 16:21:34 +08:00
import { save } from "@/utils/storeInfo";
import { removeUserInfo } 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-06 16:21:34 +08:00
if (searchParams.get("inviter")) {
save("inviter", Number(searchParams.get("inviter")));
2024-08-02 22:12:54 +08:00
}
2024-08-06 16:21:34 +08:00
if (!pathname.includes("webView") && !pathname.includes("login")) {
2024-07-22 17:55:52 +08:00
checkLogin();
}
2024-08-06 16:21:34 +08:00
2024-07-22 17:55:52 +08:00
// 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-08-12 18:13:46 +08:00
if(pathname.includes("wxpay_h5")){
2024-08-12 18:02:42 +08:00
return
}
2024-07-19 16:22:43 +08:00
if (hasToken) {
2024-08-06 16:21:34 +08:00
const currentIsLogin = await checkAuth();
if (!currentIsLogin) {
Toast.show({
icon: "fail",
content: "当前登录失效,请重新登录",
position: "top",
});
removeUserInfo();
router.push("/login");
} else {
if (pathname.includes("login")) {
router.push("/");
2024-07-19 16:22:43 +08:00
}
}
2024-08-06 16:21:34 +08:00
} else if (!searchParams.get("forgetPassword")) {
removeUserInfo();
2024-07-22 14:38:59 +08:00
router.push("/login");
2024-07-19 16:22:43 +08:00
}
};
return WrappedComponent;
2024-08-06 16:21:34 +08:00
}