tiefen_space_h5/components/WithAuth/index.js

99 lines
2.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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";
import { Dialog } from "antd-mobile";
import clipboard from "copy-to-clipboard";
export default function WithAuth(WrappedComponent) {
const router = useRouter();
const pathname = usePathname();
const searchParams = useSearchParams();
//保存内容到剪贴板
const copy = (_data) => {
clipboard(_data);
Toast.show({
icon: "success",
content: "已复制到剪贴板请前往Safari打开",
position: "top",
});
};
useEffect(() => {
const ua = navigator.userAgent.toLowerCase();
if (
(ua.indexOf("mqqbrowser") > -1 || ua.indexOf("quark") > -1) &&
ua.indexOf("iphone") > -1
) {
// 夸克浏览器
Dialog.show({
title: "提示",
actions: [
{
key: "redirect",
text: "复制网址",
onClick: () => {
const currentUrl = window.location.href;
copy(currentUrl);
},
},
],
content: "暂时不支持此浏览器请使用Safari访问",
bodyStyle: {
maxHeight: "none",
width: "80vw",
position: "fixed",
top: "200px",
left: "10vw",
"--text-color": "#fff",
color: "#fff",
},
style: {
"--text-color": "#fff",
},
});
}
}, []);
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;
}