From 7448a6bac9ba136c5710a3963c40e4974508258d Mon Sep 17 00:00:00 2001 From: wibus-wee <1596355173@qq.com> Date: Sun, 22 Jan 2023 18:59:36 +0800 Subject: [PATCH 1/2] feat: `jump` function for built-in console --- src/App.tsx | 21 +++++++------- src/components/widgets/Sidebar/index.tsx | 29 ++++++++++--------- src/pages/Categories/index.tsx | 2 +- src/pages/Comments/index.tsx | 5 ++-- src/pages/Friends/index.tsx | 7 +++-- src/pages/Login/index.tsx | 5 ++-- src/pages/Register/index.tsx | 4 +-- src/pages/Settings/index.tsx | 3 +- src/router/router.tsx | 37 +++++++++++++----------- src/types.d.ts | 3 +- src/utils/path.ts | 4 +++ 11 files changed, 67 insertions(+), 53 deletions(-) create mode 100644 src/utils/path.ts diff --git a/src/App.tsx b/src/App.tsx index 39c459b..2ac140a 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -8,6 +8,7 @@ import { useNavigate } from "react-router-dom"; import { useSnapshot } from "valtio"; import { app, server } from "./states/app"; import { Twindow } from "./components/universal/Twindow"; +import { jump } from "@utils/path"; function App() { const appSnapshot = useSnapshot(app); @@ -18,29 +19,29 @@ function App() { apiClient("/ping") .then(() => {}) .catch(() => { - if (window.location.pathname != "/status") { + if (window.location.pathname != jump("/status")) { Twindow({ title: "通信错误 - 跳转服务状态页", text: "无法与后端通信,请检查服务状态", }); - navigate("/status"); + navigate(jump(jump("/status"))); } }) .then(() => { apiClient("/user/check") .then(() => { - (window.location.pathname == "/" || - window.location.pathname == "/login" || - window.location.pathname == "/register") && - navigate("/dashboard"); + (window.location.pathname == jump("/") || + window.location.pathname == jump("/login") || + window.location.pathname == jump("/register")) && + navigate(jump("/dashboard")); app.authenticated = true; app.showSidebar = true; }) .catch(() => { - window.location.pathname != "/login" && - window.location.pathname != "/register" && - window.location.pathname != "/status" && - navigate("/login"); + window.location.pathname != jump("/login") && + window.location.pathname != jump("/register") && + window.location.pathname != jump("/status") && + navigate(jump("/login")); }); apiClient("/category").then((res) => { server.categories = res.data; diff --git a/src/components/widgets/Sidebar/index.tsx b/src/components/widgets/Sidebar/index.tsx index e7ec399..90664ab 100644 --- a/src/components/widgets/Sidebar/index.tsx +++ b/src/components/widgets/Sidebar/index.tsx @@ -19,7 +19,7 @@ import { Space } from "@components/universal/Space"; import styles from "./index.module.css"; import { SidebarItem } from "./item"; import { motion } from "framer-motion"; -import { getStorage, removeStorage, setStorage } from "@utils/storage"; +import { getStorage, setStorage } from "@utils/storage"; import { useWindowSize } from "react-use"; import itemStyle from "./item/index.module.css"; import { useSnapshot } from "valtio"; @@ -27,6 +27,7 @@ import { app } from "@states/app"; import { useNavigate } from "react-router-dom"; import { apiClient } from "@utils/request"; import { removeCookie } from "@utils/cookie"; +import { jump } from "@utils/path"; const Links = () => { const authenticated = useSnapshot(app).authenticated; @@ -46,39 +47,39 @@ const Links = () => { [styles.disabled]: disabled, })} > - + - + - + - + - + - + - - + + { }).then(() => { removeCookie("token") app.authenticated = false; - navigate("/login") + navigate(jump("/login")) }).catch((e) => { console.log(e) }) diff --git a/src/pages/Categories/index.tsx b/src/pages/Categories/index.tsx index 282bc51..638b376 100644 --- a/src/pages/Categories/index.tsx +++ b/src/pages/Categories/index.tsx @@ -135,7 +135,7 @@ export const CategoriesPage: BasicPage = () => { }); console.log(modalCreateData.id); handleRemoveSelect(); - // navigate("/categories") + // navigate(jump("/categories")) // window.location.reload() }); setLoading(true); diff --git a/src/pages/Comments/index.tsx b/src/pages/Comments/index.tsx index 6261866..92eb2a3 100644 --- a/src/pages/Comments/index.tsx +++ b/src/pages/Comments/index.tsx @@ -16,6 +16,7 @@ import { mailAvatar } from "@utils/avatar"; import { Modal, ModalBody } from "@components/universal/Modal"; import { Input, Textarea } from "@pages/Write/Input"; import { Selects } from "@components/universal/Select"; +import { jump } from "@utils/path"; const tabsList = [{ name: "待审核", @@ -72,7 +73,7 @@ export const CommentsPage: BasicPage = () => { useEffect(() => { // setLoading(true) setInSideLoading(true); - navigate(`/comments?status=${tab}&page=${page}`) + navigate(jump(`/comments?status=${tab}&page=${page}`)) handleRequest(tab, page).then((res) => { setComments(res) setInSideLoading(false); @@ -243,7 +244,7 @@ export const CommentsPage: BasicPage = () => { { - navigate(`/write/post?id=${item.post.id}`) + navigate(jump(`/write/post?id=${item.post.id}`)) }} style={{ cursor: "pointer" }} >{item.post.title} diff --git a/src/pages/Friends/index.tsx b/src/pages/Friends/index.tsx index 148a03a..7d6255a 100644 --- a/src/pages/Friends/index.tsx +++ b/src/pages/Friends/index.tsx @@ -31,6 +31,7 @@ import { import postStyles from "@pages/Posts/Index/index.module.css"; import { Input, Textarea } from "@pages/Write/Input"; import styles from "./index.module.css"; +import { jump } from "@utils/path"; const FriendsStatus = ["Approved", "Pending", "Spam", "Trash"]; const FriendsFormFront = [ @@ -87,7 +88,7 @@ export const FriendsPage: BasicPage = () => { useEffect(() => { if (status !== "0" && status !== "1" && status !== "2" && status !== "3") { setTab(0); - navigate("/friends?status=0"); + navigate(jump("/friends?status=0")); } else { setTab(Number(status)); } @@ -127,7 +128,7 @@ export const FriendsPage: BasicPage = () => { onConfirm={() => { const REQUEST = { method: select[0] ? "PUT" : "POST", - URL: select[0] ? `/friends/${select[0]}` : "/friends", + URL: select[0] ? jump(`/friends/${select[0]}`) : jump("/friends"), title: select[0] ? "修改成功" : "添加成功", }; apiClient(REQUEST.URL, { @@ -564,7 +565,7 @@ export const FriendsPage: BasicPage = () => { defaultIndex={tab} onChange={(index) => { setTab(index); - navigate(`/friends?status=${index}`); + navigate(jump(`/friends?status=${index}`)); }} > diff --git a/src/pages/Login/index.tsx b/src/pages/Login/index.tsx index 9279331..a7af4b6 100644 --- a/src/pages/Login/index.tsx +++ b/src/pages/Login/index.tsx @@ -8,6 +8,7 @@ import { apiClient } from "@utils/request"; import { Twindow } from "@components/universal/Twindow"; import { app } from "@states/app"; import { setCookie } from "@utils/cookie"; +import { jump } from "@utils/path"; export const Login: BasicPage = () => { const [loading, setLoading] = useState(false); @@ -17,7 +18,7 @@ export const Login: BasicPage = () => { useEffect(() => { apiClient("/user/master/info").catch(() => { - navigate("/register"); + navigate(jump("/register")); }) }, []); @@ -46,7 +47,7 @@ export const Login: BasicPage = () => { image: res.avatar, }); app.authenticated = true; - navigate("/dashboard"); + navigate(jump("/dashboard")); window.location.reload(); }) .catch((res) => { diff --git a/src/pages/Register/index.tsx b/src/pages/Register/index.tsx index fbd3def..4e29b85 100644 --- a/src/pages/Register/index.tsx +++ b/src/pages/Register/index.tsx @@ -21,7 +21,7 @@ export const RegisterPage: BasicPage = () => { apiClient("/user/master/info") .then((res) => { if (res) { - navigate("/dashboard"); + navigate(jump("/dashboard")); } }).catch(() => { return; @@ -61,7 +61,7 @@ export const RegisterPage: BasicPage = () => { image: res.avatar, }); app.authenticated = true; - navigate("/dashboard"); + navigate(jump("/dashboard")); window.location.reload(); }) .catch((res) => { diff --git a/src/pages/Settings/index.tsx b/src/pages/Settings/index.tsx index d1c29a2..9f2bd4f 100644 --- a/src/pages/Settings/index.tsx +++ b/src/pages/Settings/index.tsx @@ -16,6 +16,7 @@ import { getQueryVariable } from "@utils/url"; import { useNavigate } from "react-router-dom"; import { Tags } from "@components/universal/Tags"; import { Toggle } from "@components/universal/Toggle"; +import { jump } from "@utils/path"; const tabsAPI = ["/user/master/info", "/configs"] @@ -28,7 +29,7 @@ export const SettingsPage: BasicPage = () => { useEffect(() => { // setLoading(true); - navigate(`/settings?tab=${_tabs}`) + navigate(jump(`/settings?tab=${_tabs}`)) apiClient(tabsAPI[_tabs]).then((res) => { setData(res); }); diff --git a/src/router/router.tsx b/src/router/router.tsx index 85a23df..a35b413 100644 --- a/src/router/router.tsx +++ b/src/router/router.tsx @@ -9,7 +9,9 @@ import { CommentsPage } from "@pages/Comments"; import { RegisterPage } from "@pages/Register"; -import { Route, Routes } from "react-router-dom"; +import { jump } from "@utils/path"; +import { useEffect } from "react"; +import { Route, Routes, useNavigate } from "react-router-dom"; import { CategoriesPage } from "../pages/Categories"; import { FriendsPage } from "../pages/Friends"; import { Home } from "../pages/Home"; @@ -24,33 +26,34 @@ import { EditorPage } from "../pages/Write"; export const AppRouter = () => { return ( - } /> - } /> + } /> + } /> - } /> - } /> + } /> + } /> - } /> - } /> - } /> + } /> + } /> + } /> - } /> - } /> + } /> + } /> - } /> - } /> - } /> + } /> + } /> + } /> - } /> - } /> + } /> + } /> - } /> + } /> + } /> ); }; const NotFoundPage = () => { - window.location.href = "/dashboard"; + window.location.href = jump("/dashboard"); return <>; }; diff --git a/src/types.d.ts b/src/types.d.ts index 1b82366..100fc83 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -4,6 +4,7 @@ declare global { } export interface Window { MOG_API: string; + MOG_BASE: string; } } @@ -15,4 +16,4 @@ interface HTMLInputElement extends HTMLElement { value: string; } -export {}; +export {}; \ No newline at end of file diff --git a/src/utils/path.ts b/src/utils/path.ts new file mode 100644 index 0000000..5cfc596 --- /dev/null +++ b/src/utils/path.ts @@ -0,0 +1,4 @@ +export function jump(p: string){ + const base = window.MOG_BASE || '' + return `${base}${p}` +} \ No newline at end of file From 2583bb6bf365964f9e73ac48d399924d9f16a2c0 Mon Sep 17 00:00:00 2001 From: wibus-wee <1596355173@qq.com> Date: Sun, 22 Jan 2023 19:00:34 +0800 Subject: [PATCH 2/2] fix: bump --- src/pages/Register/index.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/pages/Register/index.tsx b/src/pages/Register/index.tsx index 4e29b85..efb71b4 100644 --- a/src/pages/Register/index.tsx +++ b/src/pages/Register/index.tsx @@ -8,6 +8,7 @@ import { apiClient } from "@utils/request"; import { Twindow } from "@components/universal/Twindow"; import { app } from "@states/app"; import { setCookie } from "@utils/cookie"; +import { jump } from "@utils/path"; // nickname, description, email, avatar, password, username