处理登录验证

This commit is contained in:
al 2024-08-06 16:21:34 +08:00
parent 5296880dbb
commit 577cc7d042
7 changed files with 168 additions and 60 deletions

View File

@ -3,6 +3,18 @@
@tailwind utilities; @tailwind utilities;
body{ body{
color:#fff; color:#fff;
padding-bottom: constant(safe-area-inset-bottom);
padding-bottom: env(safe-area-inset-bottom);
}
footer{
/* Fallback for non-iOS devices */
bottom: 0;
/* iOS 11.0+ */
bottom: constant(safe-area-inset-bottom);
/* iOS 11.2+ */
bottom: env(safe-area-inset-bottom);
} }
/* :root { /* :root {
--foreground-rgb: 0, 0, 0; --foreground-rgb: 0, 0, 0;

View File

@ -46,7 +46,10 @@ export default function RootLayout({ children }) {
type="image/x-icon" type="image/x-icon"
></link> ></link>
<link rel="icon" href="/favicon.png" type="image/png"></link> <link rel="icon" href="/favicon.png" type="image/png"></link>
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/> {/* <meta
name="viewport"
content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"
/> */}
<meta <meta
name="description" name="description"
content={metadata.description} content={metadata.description}
@ -62,9 +65,9 @@ export default function RootLayout({ children }) {
{/* <!-- 全屏设置 --> */} {/* <!-- 全屏设置 --> */}
<meta <meta
name="viewport" name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui,viewport-fit=cover"
/> />
<meta name="apple-touch-fullscreen" content="yes" />
{/* <!-- 网站开启对 web app 程序的支持 具体表现为去除浏览器地址栏和底部导航栏 :先保存为桌面书签,然后通过书签打开即可生效--> */} {/* <!-- 网站开启对 web app 程序的支持 具体表现为去除浏览器地址栏和底部导航栏 :先保存为桌面书签,然后通过书签打开即可生效--> */}
<meta name="apple-mobile-web-app-capable" content="yes" /> <meta name="apple-mobile-web-app-capable" content="yes" />
{/* <!-- 用来定义顶部状态栏的形式默认是default为白色 black为黑色 black-translucent为灰色半透明会占据屏幕的约20px不同的设备可能会有差异--> */} {/* <!-- 用来定义顶部状态栏的形式默认是default为白色 black为黑色 black-translucent为灰色半透明会占据屏幕的约20px不同的设备可能会有差异--> */}
@ -73,34 +76,125 @@ export default function RootLayout({ children }) {
name="apple-mobile-web-app-status-bar-style" name="apple-mobile-web-app-status-bar-style"
content="black-translucent" content="black-translucent"
/> />
{/* <!-- apple-touch-startup-image用来配置启动动画 --> */} {/* <!-- apple-touch-startup-image用来配置启动动画 --> */}
{/* <!-- 这里要注意,这里图片的尺寸要和设备的静态图片显示尺寸完全对应,差一个像素都会导致启动动画无法显示 --> */} {/* <!-- 这里要注意,这里图片的尺寸要和设备的静态图片显示尺寸完全对应,差一个像素都会导致启动动画无法显示 --> */}
{/* <!-- 下面列举了iPhone的所有尺寸ps:为了方便大家就全部贴出来了!!) --> */} {/* <!-- 下面列举了iPhone的所有尺寸ps:为了方便大家就全部贴出来了!!) --> */}
{/* <!-- iPhone 678 startup image @2x--> */} {/* <!-- iPhone 678 startup image @2x--> */}
<link href={process.env.NEXT_PUBLIC_WEB_ASSETS_URL+"/images/launchPage/750_1334.png"} media="(device-width: 375px) and (device-height: 667px) and (-webkit-device-pixel-ratio: 2)" rel="apple-touch-startup-image"/> <link
{/* <!-- iPhone 678p startup image @3x--> */} href={
<link href={process.env.NEXT_PUBLIC_WEB_ASSETS_URL+"/images/launchPage/1242_2208.png"} media="(device-width: 414px) and (device-height: 736px) and (-webkit-device-pixel-ratio: 3)" rel="apple-touch-startup-image"/> process.env.NEXT_PUBLIC_WEB_ASSETS_URL +
{/* <!-- iPhone X Xs startup image @3x--> */} "/images/launchPage/750_1334.png"
<link href={process.env.NEXT_PUBLIC_WEB_ASSETS_URL+"/images/launchPage/1125_2436.png"} media="(device-width: 375px) and (device-height: 812px) and (-webkit-device-pixel-ratio: 3)" rel="apple-touch-startup-image"/> }
{/* <!-- iPhone XR startup image @2X --> */} media="(device-width: 375px) and (device-height: 667px) and (-webkit-device-pixel-ratio: 2)"
<link href={process.env.NEXT_PUBLIC_WEB_ASSETS_URL+"/images/launchPage/828_1792.png"} media="(device-width: 414px) and (device-height: 896px) and (-webkit-device-pixel-ratio: 2)" rel="apple-touch-startup-image"/> rel="apple-touch-startup-image"
{/* <!-- iPhone XR Max startup image @3x--> */} />
<link href={process.env.NEXT_PUBLIC_WEB_ASSETS_URL+"/images/launchPage/640_960.png"} media="(device-width: 320px) and (device-height: 480px) and (-webkit-device-pixel-ratio: 2)" rel="apple-touch-startup-image"/> {/* <!-- iPhone 678p startup image @3x--> */}
<link href={process.env.NEXT_PUBLIC_WEB_ASSETS_URL+"/images/launchPage/640_1136.png"} media="(device-width: 320px) and (device-height: 568) and (-webkit-device-pixel-ratio: 2)" rel="apple-touch-startup-image"/> <link
<link href={process.env.NEXT_PUBLIC_WEB_ASSETS_URL+"/images/launchPage/1080_1920.png"} media="(device-width: 360px) and (device-height: 640px) and (-webkit-device-pixel-ratio: 3)" rel="apple-touch-startup-image"/> href={
<link href={process.env.NEXT_PUBLIC_WEB_ASSETS_URL+"/images/launchPage/1080_2340.png"} media="(device-width: 360px) and (device-height: 780px) and (-webkit-device-pixel-ratio: 3)" rel="apple-touch-startup-image"/> process.env.NEXT_PUBLIC_WEB_ASSETS_URL +
<link href={process.env.NEXT_PUBLIC_WEB_ASSETS_URL+"/images/launchPage/1170_2532.png"} media="(device-width: 390px) and (device-height: 844px) and (-webkit-device-pixel-ratio: 3)" rel="apple-touch-startup-image"/> "/images/launchPage/1242_2208.png"
<link href={process.env.NEXT_PUBLIC_WEB_ASSETS_URL+"/images/launchPage/1179_2556.png"} media="(device-width: 393px) and (device-height: 852px) and (-webkit-device-pixel-ratio: 3)" rel="apple-touch-startup-image"/> }
<link href={process.env.NEXT_PUBLIC_WEB_ASSETS_URL+"/images/launchPage/1242_2688.png"} media="(device-width: 414px) and (device-height: 896px) and (-webkit-device-pixel-ratio: 3)" rel="apple-touch-startup-image"/> media="(device-width: 414px) and (device-height: 736px) and (-webkit-device-pixel-ratio: 3)"
<link href={process.env.NEXT_PUBLIC_WEB_ASSETS_URL+"/images/launchPage/1284_2778.png"} media="(device-width: 428px) and (device-height: 926px) and (-webkit-device-pixel-ratio: 3)" rel="apple-touch-startup-image"/> rel="apple-touch-startup-image"
<link href={process.env.NEXT_PUBLIC_WEB_ASSETS_URL+"/images/launchPage/1290_2796.png"} media="(device-width: 430px) and (device-height: 932px) and (-webkit-device-pixel-ratio: 3)" rel="apple-touch-startup-image"/> />
{/* <!-- iPhone X Xs startup image @3x--> */}
<link
href={
process.env.NEXT_PUBLIC_WEB_ASSETS_URL +
"/images/launchPage/1125_2436.png"
}
media="(device-width: 375px) and (device-height: 812px) and (-webkit-device-pixel-ratio: 3)"
rel="apple-touch-startup-image"
/>
{/* <!-- iPhone XR startup image @2X --> */}
<link
href={
process.env.NEXT_PUBLIC_WEB_ASSETS_URL +
"/images/launchPage/828_1792.png"
}
media="(device-width: 414px) and (device-height: 896px) and (-webkit-device-pixel-ratio: 2)"
rel="apple-touch-startup-image"
/>
{/* <!-- iPhone XR Max startup image @3x--> */}
<link
href={
process.env.NEXT_PUBLIC_WEB_ASSETS_URL +
"/images/launchPage/640_960.png"
}
media="(device-width: 320px) and (device-height: 480px) and (-webkit-device-pixel-ratio: 2)"
rel="apple-touch-startup-image"
/>
<link
href={
process.env.NEXT_PUBLIC_WEB_ASSETS_URL +
"/images/launchPage/640_1136.png"
}
media="(device-width: 320px) and (device-height: 568) and (-webkit-device-pixel-ratio: 2)"
rel="apple-touch-startup-image"
/>
<link
href={
process.env.NEXT_PUBLIC_WEB_ASSETS_URL +
"/images/launchPage/1080_1920.png"
}
media="(device-width: 360px) and (device-height: 640px) and (-webkit-device-pixel-ratio: 3)"
rel="apple-touch-startup-image"
/>
<link
href={
process.env.NEXT_PUBLIC_WEB_ASSETS_URL +
"/images/launchPage/1080_2340.png"
}
media="(device-width: 360px) and (device-height: 780px) and (-webkit-device-pixel-ratio: 3)"
rel="apple-touch-startup-image"
/>
<link
href={
process.env.NEXT_PUBLIC_WEB_ASSETS_URL +
"/images/launchPage/1170_2532.png"
}
media="(device-width: 390px) and (device-height: 844px) and (-webkit-device-pixel-ratio: 3)"
rel="apple-touch-startup-image"
/>
<link
href={
process.env.NEXT_PUBLIC_WEB_ASSETS_URL +
"/images/launchPage/1179_2556.png"
}
media="(device-width: 393px) and (device-height: 852px) and (-webkit-device-pixel-ratio: 3)"
rel="apple-touch-startup-image"
/>
<link
href={
process.env.NEXT_PUBLIC_WEB_ASSETS_URL +
"/images/launchPage/1242_2688.png"
}
media="(device-width: 414px) and (device-height: 896px) and (-webkit-device-pixel-ratio: 3)"
rel="apple-touch-startup-image"
/>
<link
href={
process.env.NEXT_PUBLIC_WEB_ASSETS_URL +
"/images/launchPage/1284_2778.png"
}
media="(device-width: 428px) and (device-height: 926px) and (-webkit-device-pixel-ratio: 3)"
rel="apple-touch-startup-image"
/>
<link
href={
process.env.NEXT_PUBLIC_WEB_ASSETS_URL +
"/images/launchPage/1290_2796.png"
}
media="(device-width: 430px) and (device-height: 932px) and (-webkit-device-pixel-ratio: 3)"
rel="apple-touch-startup-image"
/>
</head> </head>
<body className={`${inter.className} h-full`}> <body className={`${inter.className} h-full`}>
<main className={`w-full bg-deepBg h-full`}> <main className={`w-full bg-deepBg h-full`}>
{withAuth(<Provider store={store}>{children}</Provider>)} {withAuth(<Provider store={store}>{children}</Provider>)}
{/* <Provider store={store}>{children}</Provider> */} {/* <Provider store={store}>{children}</Provider> */}
</main> </main>
<footer className="fixed bottom-0 left-0 w-screen bg-black"> <footer className="fixed left-0 w-screen bg-black">
<div> <div>
<BottomNav /> <BottomNav />
</div> </div>

View File

@ -15,7 +15,7 @@ import { useRouter } from "next/navigation";
import styles from "./index.module.scss"; import styles from "./index.module.scss";
import { JSEncrypt } from "jsencrypt"; import { JSEncrypt } from "jsencrypt";
import { handleLogin } from "@/store/actions"; import { handleLogin } from "@/store/actions";
import { saveUserInfo, removeUserInfo } from "@/utils/storeInfo"; import { saveUserInfo, get } from "@/utils/storeInfo";
import { connect } from "react-redux"; import { connect } from "react-redux";
import { cryptoPassword } from "@/utils/crypto"; import { cryptoPassword } from "@/utils/crypto";
import requireAPI from "@/utils/requireAPI"; import requireAPI from "@/utils/requireAPI";
@ -56,12 +56,11 @@ function Login({ handleLogin }) {
} }
handleLogin({ isSignin: false, userToken: null }); handleLogin({ isSignin: false, userToken: null });
checkAuth().then(res=>{ checkAuth().then(res=>{
if(res){ const account = get("account")
router.push("/") if(res && account){
}else{ router.replace("/")
removeUserInfo();
// signOut();
} }
}) })

View File

@ -4,7 +4,7 @@ import React, { useEffect, useState, useMemo,useCallback } from "react";
import Photos from "../Photos"; import Photos from "../Photos";
import { useRouter } from "next/navigation"; import { useRouter } from "next/navigation";
import PaySpacePost from "../PaySpacePost"; import PaySpacePost from "../PaySpacePost";
import { Image, Popover, Divider } from "antd-mobile"; import { Image, Popover, Divider, Toast } from "antd-mobile";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faAngleRight } from "@fortawesome/free-solid-svg-icons"; import { faAngleRight } from "@fortawesome/free-solid-svg-icons";
import { handleFollow, thumbsUp } from "@/api/public"; import { handleFollow, thumbsUp } from "@/api/public";
@ -27,7 +27,7 @@ export default function PostItem({
const [isCreator, setIsCreator] = useState(false); const [isCreator, setIsCreator] = useState(false);
useEffect(() => { useEffect(() => {
const account = get("account"); const account = get("account");
if (account.mid === data.mid) setIsCreator(true); if (account?.mid === data.mid) setIsCreator(true);
return () => { return () => {
router.prefetch("/profile/" + data.mid); router.prefetch("/profile/" + data.mid);
}; };

View File

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

View File

@ -4,7 +4,11 @@ import { get } from "./storeInfo";
export async function checkAuth() { export async function checkAuth() {
try { try {
const data = await requireAPI("POST", `/api/login/validate`); const data = await requireAPI("POST", `/api/login/validate`);
return data.ret === 1; if(data.ret === 1){
return true;
}else{
return false;
}
} catch (e) { } catch (e) {
console.log(e); console.log(e);
} }

View File

@ -1,11 +1,10 @@
import baseRequest from "./baseRequest"; import baseRequest from "./baseRequest";
import { get } from "./storeInfo"; import { get } from "./storeInfo";
// import { useRouter } from "next/navigation"; // import { useRouter } from "next/navigation";
// import webviewBaseRequest from "@/utils/webviewBaseRequest"; // import webviewBaseRequest from "@/utils/webviewBaseRequest";
// 创建一个封装 fetch 的函数 // 创建一个封装 fetch 的函数
export default function customFetch(method, url, options = {}, mid) { export default function customFetch(method, url, options = {}, needMid) {
const base = baseRequest(); const base = baseRequest();
// 默认选项 // 默认选项
const defaultOptions = { const defaultOptions = {
@ -19,8 +18,9 @@ export default function customFetch(method, url, options = {}, mid) {
// 可以添加其他默认选项 // 可以添加其他默认选项
}; };
let newBody = { ...options?.body }; let newBody = { ...options?.body };
if (mid) { if (needMid) {
let mid = get("account").mid; let mid = get("account")?.mid;
if(!mid) return;
newBody.mid = mid; newBody.mid = mid;
} }
const body = JSON.stringify({ ...base, ...newBody }); const body = JSON.stringify({ ...base, ...newBody });