Skip to content

Commit

Permalink
首页
Browse files Browse the repository at this point in the history
  • Loading branch information
xmdhs committed Oct 13, 2023
1 parent ab34676 commit 70ede8d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 39 deletions.
43 changes: 4 additions & 39 deletions frontend/src/Route.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { Routes, Route, createBrowserRouter, RouterProvider, useNavigate, Outlet, Navigate } from "react-router-dom";
import { Routes, Route, createBrowserRouter, RouterProvider, Outlet } from "react-router-dom";
import { ScrollRestoration } from "react-router-dom";
import Login from '@/views/Login'
import Register from '@/views/Register'
import Profile from '@/views/profile/Profile'
import Textures from '@/views/profile/Textures'
import Security from '@/views/profile/Security'
import Layout from '@/views/Layout'
import { useAtomValue } from "jotai";
import { token } from "@/store/store";
import { ApiErr } from "@/apis/error";
import { userInfo } from "@/apis/apis";
import { useRequest } from "ahooks";
import UserAdmin from "@/views/admin/UserAdmin";
import NeedLogin from "@/components/NeedLogin";
import Index from "@/views/Index";

const router = createBrowserRouter([
{ path: "*", Component: Root },
Expand All @@ -22,7 +19,7 @@ function Root() {
<>
<Routes>
<Route path="/" element={<Layout />}>
<Route index />
<Route index element={<Index />} />
<Route path="/*" element={<p>404</p>} />
<Route path="/login" element={<Login />} />
<Route path="/register" element={<Register />} />
Expand Down Expand Up @@ -53,35 +50,3 @@ export function PageRoute() {
)
}


function NeedLogin({ children, needAdmin = false }: { children: JSX.Element, needAdmin?: boolean }) {
const t = useAtomValue(token)
const navigate = useNavigate();
useRequest(() => userInfo(t), {
refreshDeps: [t],
cacheKey: "/api/v1/user" + t,
staleTime: 60000,
onError: e => {
if (e instanceof ApiErr && e.code == 5) {
navigate("/login")
}
console.warn(e)
},
onSuccess: u => {
if (!u) return
if (!u.is_admin && needAdmin) {
navigate("/login")
}
if (u.uuid == "") {
navigate("/login")
}
}
})

if (!localStorage.getItem("token") || localStorage.getItem("token") == '""') {
return <Navigate to="/login" />
}


return <> {children}</>
}
39 changes: 39 additions & 0 deletions frontend/src/components/NeedLogin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { userInfo } from "@/apis/apis";
import { ApiErr } from "@/apis/error";
import { token } from "@/store/store";
import { useRequest } from "ahooks";
import { useAtomValue } from "jotai";
import { useNavigate, Navigate } from "react-router-dom";


export default function NeedLogin({ children, needAdmin = false }: { children: JSX.Element, needAdmin?: boolean }) {
const t = useAtomValue(token)
const navigate = useNavigate();
useRequest(() => userInfo(t), {
refreshDeps: [t],
cacheKey: "/api/v1/user" + t,
staleTime: 60000,
onError: e => {
if (e instanceof ApiErr && e.code == 5) {
navigate("/login")
}
console.warn(e)
},
onSuccess: u => {
if (!u) return
if (!u.is_admin && needAdmin) {
navigate("/login")
}
if (u.uuid == "") {
navigate("/login")
}
}
})

if (!localStorage.getItem("token") || localStorage.getItem("token") == '""') {
return <Navigate to="/login" />
}


return <> {children}</>
}
9 changes: 9 additions & 0 deletions frontend/src/views/Index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Profile from "@/views/profile/Profile"
import Login from "@/views/Login"

export default function Index() {
if (!(localStorage.getItem("token") || localStorage.getItem("token") == '""')) {
return <Profile />
}
return <Login />
}

0 comments on commit 70ede8d

Please sign in to comment.